Skip to content

Instantly share code, notes, and snippets.

@crowjdh
Created November 15, 2015 03:42
Show Gist options
  • Save crowjdh/0603b9173ed6d016006a to your computer and use it in GitHub Desktop.
Save crowjdh/0603b9173ed6d016006a to your computer and use it in GitHub Desktop.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
* Created by Dongheyon Jeong in News Kit from Yooii Studios Co., LTD. on 15. 3. 11.
*
* ObjectByteConverter
* Converts Object to bytes and vice versa
*/
public class ObjectByteConverter {
public static byte[] toByteArray(Object object) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput objectOutput = new ObjectOutputStream(outputStream);
objectOutput.writeObject(object);
byte[] bytes = outputStream.toByteArray();
objectOutput.close();
return bytes;
}
public static Object fromByteArray(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis);
return in.readObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment