me.redtea.carcadex.data.schema.file.impl.binary.BinarySchemaStrategy Maven / Gradle / Ivy
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());
}
}