Java Serializable Object to Byte Array

ID : 9427

viewed : 143

Tags : javaobjectserializationjava





Top 5 Answer for Java Serializable Object to Byte Array

vote vote

99

Prepare the byte array to send:

ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = null; try {   out = new ObjectOutputStream(bos);      out.writeObject(yourObject);   out.flush();   byte[] yourBytes = bos.toByteArray();   ... } finally {   try {     bos.close();   } catch (IOException ex) {     // ignore close exception   } } 

Create an object from a byte array:

ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes); ObjectInput in = null; try {   in = new ObjectInputStream(bis);   Object o = in.readObject();    ... } finally {   try {     if (in != null) {       in.close();     }   } catch (IOException ex) {     // ignore close exception   } } 
vote vote

90

The best way to do it is to use SerializationUtils from Apache Commons Lang.

To serialize:

byte[] data = SerializationUtils.serialize(yourObject); 

To deserialize:

YourObject yourObject = SerializationUtils.deserialize(data) 

As mentioned, this requires Commons Lang library. It can be imported using Gradle:

compile 'org.apache.commons:commons-lang3:3.5' 

Maven:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency>     <groupId>org.apache.commons</groupId>     <artifactId>commons-lang3</artifactId>     <version>3.5</version> </dependency> 

Jar file

And more ways mentioned here

Alternatively, the whole collection can be imported. Refer this link

vote vote

70

If you use Java >= 7, you could improve the accepted solution using try with resources:

private byte[] convertToBytes(Object object) throws IOException {     try (ByteArrayOutputStream bos = new ByteArrayOutputStream();          ObjectOutputStream out = new ObjectOutputStream(bos)) {         out.writeObject(object);         return bos.toByteArray();     }  } 

And the other way around:

private Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {     try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);          ObjectInputStream in = new ObjectInputStream(bis)) {         return in.readObject();     }  } 
vote vote

68

Can be done by SerializationUtils, by serialize & deserialize method by ApacheUtils to convert object to byte[] and vice-versa , as stated in @uris answer.

To convert an object to byte[] by serializing:

byte[] data = SerializationUtils.serialize(object); 

To convert byte[] to object by deserializing::

Object object = (Object) SerializationUtils.deserialize(byte[] data) 

Click on the link to Download org-apache-commons-lang.jar

Integrate .jar file by clicking:

FileName -> Open Medule Settings -> Select your module -> Dependencies -> Add Jar file and you are done.

Hope this helps.

vote vote

51

I also recommend to use SerializationUtils tool. I want to make a ajust on a wrong comment by @Abilash. The SerializationUtils.serialize() method is not restricted to 1024 bytes, contrary to another answer here.

public static byte[] serialize(Object object) {     if (object == null) {         return null;     }     ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);     try {         ObjectOutputStream oos = new ObjectOutputStream(baos);         oos.writeObject(object);         oos.flush();     }     catch (IOException ex) {         throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);     }     return baos.toByteArray(); } 

At first sight´╝î you may think that new ByteArrayOutputStream(1024) will only allow a fixed size. But if you take a close look at the ByteArrayOutputStream, you will figure out the the stream will grow if necessary:

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

Top 3 video Explaining Java Serializable Object to Byte Array







Related QUESTION?