de.tsl2.nano.replication.serializer.SerializeBytes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.replication Show documentation
Show all versions of tsl2.nano.replication Show documentation
Provides Database/XML Replication through JPA
package de.tsl2.nano.replication.serializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeBytes implements Serializer {
public static final String KEY = "BYTES";
@Override
public String getKey() {
return KEY;
}
@Override
public String getExtension() {
return "bytes";
}
@Override
public ByteArrayOutputStream serialize(Object obj) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
try(ObjectOutputStream stream = new ObjectOutputStream(bs)) {
stream.writeObject(obj);
} catch (IOException e) {
throw new RuntimeException(e);
}
return bs;
}
@SuppressWarnings("unchecked")
@Override
public T deserialize(InputStream stream, Class type) throws ClassNotFoundException, IOException {
try(ObjectInputStream in = new ObjectInputStream(stream)) {return (T) in.readObject();}
}
}