All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.redtea.carcadex.data.schema.file.impl.binary.BinarySchemaStrategy Maven / Gradle / Ivy

There is a newer version: 2.0.1-BETA
Show newest version
package me.redtea.carcadex.data.schema.file.impl.binary;

import lombok.SneakyThrows;
import me.redtea.carcadex.data.schema.file.AbstractFileSchemaStrategy;

import java.io.*;
import java.util.Base64;

public class BinarySchemaStrategy extends AbstractFileSchemaStrategy {

    public BinarySchemaStrategy(File folder) {
        super(folder);
    }

    @SneakyThrows
    @Override
    protected V fromFile(String string) {
        InputStream in = new ByteArrayInputStream(Base64.getDecoder().decode(string));
        ObjectInputStream objectInputStream = new ObjectInputStream(in);
        V result = (V) objectInputStream.readObject();
        objectInputStream.close();
        return result;
    }

    @SneakyThrows
    @Override
    protected String toFile(V value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);
        objectOutputStream.writeObject(value);
        objectOutputStream.flush();
        objectOutputStream.close();
        baos.close();
        return Base64.getEncoder().encodeToString(baos.toByteArray());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy