Created
June 1, 2013 10:08
-
-
Save dawnbreaks/5689896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static byte[] serialize(Object o) throws IOException | |
{ | |
// Kryo kryo = new Kryo(); | |
// kryo.setDefaultSerializer(CompatibleFieldSerializer.class); | |
// Output output = new Output(4096, -1); | |
// kryo.writeClassAndObject(output,o); | |
// output.close(); | |
// return output.toBytes(); | |
ByteArrayOutputStream ostream = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(ostream); | |
oos.writeObject(o); | |
oos.flush(); | |
oos.close(); | |
byte[] buffer = ostream.toByteArray(); | |
return buffer; | |
} | |
public static <T> T deserialize( final byte[] buffer, final Class<T> clazz ) throws IOException, ClassNotFoundException | |
{ | |
// Kryo kryo = new Kryo(); | |
// kryo.setDefaultSerializer(CompatibleFieldSerializer.class); | |
// kryo.setRegistrationRequired(false); | |
// Input input = new Input(buffer); | |
// return (T)kryo.readClassAndObject(input); | |
ByteArrayInputStream instr = new ByteArrayInputStream(buffer); | |
ObjectInputStream ois = new ObjectInputStream(instr); | |
return (T)ois.readObject(); | |
} | |
public static Object deserialize(byte[] buffer) throws IOException, ClassNotFoundException | |
{ | |
// | |
// Kryo kryo = new Kryo(); | |
// kryo.setDefaultSerializer(CompatibleFieldSerializer.class); | |
// kryo.setRegistrationRequired(false); | |
// Input input = new Input(buffer); | |
// return kryo.readClassAndObject(input); | |
ByteArrayInputStream instr = new ByteArrayInputStream(buffer); | |
ObjectInputStream ois = new ObjectInputStream(instr); | |
return ois.readObject(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment