Skip to content

Instantly share code, notes, and snippets.

@dawnbreaks
Created June 1, 2013 10:08
Show Gist options
  • Save dawnbreaks/5689896 to your computer and use it in GitHub Desktop.
Save dawnbreaks/5689896 to your computer and use it in GitHub Desktop.
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