info.hargrave.configuration.Serializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of configurate Show documentation
Show all versions of configurate Show documentation
Configuration API that attempts to facilitate strongly-typed configuration
The newest version!
package info.hargrave.configuration;
import java.io.*;
/**
* Provides an interface for object serialization methods
* Date: 10/24/13
* Time: 10:22 PM
*/
public interface Serializer {
/**
* The one-and-only instance of the java serializer
*/
public static final Serializer JAVA_SERIALIZER = new JavaSerializer();
/**
* Stream an object as a binary array
* @param object object to serialize
* @return binary data
* @throws IOException (fall-through from underlying serializer implementation)
*/
public byte[] serialize(Object object) throws IOException;
/**
* Deserialize the contents of a byte array
* Note that due to the nature of this function, a byte array should contain no more than one object in series, not counting objects inside the object
* @param serial serial object
* @param expectedType the class of the object
* @param generic type representing the class-type of the object
* @return deserialized object
* @throws IOException (fall-through from underlying serializer library)
*/
public A deSerialize(byte[] serial, Class expectedType) throws IOException, ClassNotFoundException;
/**
* Implementation of Serializer to provide access to the Java serialization system
*/
final class JavaSerializer implements Serializer {
JavaSerializer(){}
@Override
public byte[] serialize(Object object) throws IOException {
ByteArrayOutputStream _bas = new ByteArrayOutputStream();
ObjectOutputStream _oos = new ObjectOutputStream(_bas);
_oos.writeObject(object);
return _bas.toByteArray();
}
@Override
public A deSerialize(byte[] serial, Class expectedType) throws IOException, ClassNotFoundException {
ByteArrayInputStream _bis = new ByteArrayInputStream(serial);
ObjectInputStream _ois = new ObjectInputStream(_bis);
Object _toCast = _ois.readObject();
return expectedType.cast(_toCast);
}
}
}