strangedb/src/main/java/it/cavallium/strangedb/java/database/FSTSerializer.java
2019-04-20 03:46:59 +02:00

119 lines
3.3 KiB
Java

/**
package it.cavallium.strangedb.java.database;
import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
import java.io.*;
public class FSTSerializer implements ISerializer {
private FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();
public FSTSerializer() {
conf.setStructMode(true);
conf.setClassLoader(ClassLoader.getSystemClassLoader());
conf.setCrossPlatform(false);
conf.setPreferSpeed(true);
}
@SuppressWarnings("unchecked")
public <T> T read(InputStream stream, Class<T> type) throws IOException {
try {
FSTObjectInput in = conf.getObjectInput(stream);
T result = (T) in.readObject(type);
// DON'T: in.close(); here prevents reuse and will result in an exception
stream.close();
return result;
} catch (Exception e) {
throw new IOException(e);
}
}
public <T> T readBytes(byte[] input, Class<T> type) throws IOException {
return read(new ByteArrayInputStream(input), type);
}
@SuppressWarnings("unchecked")
public <T> Class<T> readClass(InputStream stream) throws IOException {
try {
FSTObjectInput in = conf.getObjectInput(stream);
Class<T> result = (Class<T>) in.readObject(Class.class);
// DON'T: in.close(); here prevents reuse and will result in an exception
stream.close();
return result;
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public <T> Class<T> readClassBytes(byte[] input) throws IOException {
return readClass(new ByteArrayInputStream(input));
}
public <T> void write(OutputStream stream, T toWrite, Class<?> type) throws IOException {
FSTObjectOutput out = conf.getObjectOutput(stream);
out.writeObject(toWrite, type);
// DON'T out.close() when using factory method;
out.flush();
stream.close();
}
public <T> byte[] writeBytes(T toWrite, Class<?> type) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
write(baos, toWrite, type);
return baos.toByteArray();
}
public void writeClass(OutputStream stream, Class<?> toWrite) throws IOException {
FSTObjectOutput out = conf.getObjectOutput(stream);
out.writeObject(toWrite, Class.class);
// DON'T out.close() when using factory method;
out.flush();
stream.close();
}
@Override
public byte[] writeClassBytes(Class<?> toWrite) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeClass(baos, toWrite);
return baos.toByteArray();
}
public void registerClass(Class<?> type, int id) {
conf.registerClass(type);
}
@Override
public <T> byte[] writeClassAndObjectBytes(T value) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FSTObjectOutput out = conf.getObjectOutput(baos);
out.writeObject(value);
// DON'T out.close() when using factory method;
out.flush();
baos.close();
return baos.toByteArray();
}
@Override
@SuppressWarnings("unchecked")
public <T> T readClassAndObjectBytes(byte[] input) throws IOException {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
FSTObjectInput in = conf.getObjectInput(bais);
T result;
result = (T) in.readObject();
// DON'T: in.close(); here prevents reuse and will result in an exception
bais.close();
return result;
} catch (Exception e) {
throw new IOException(e);
}
}
}
**/