Convert any object to byte array in java

I have an object of type X that I want to convert to an array of bytes before sending it for storage in S3. Can someone tell me how to do this? I appreciate your help.

+48
java serialization bytearray
Apr 29 2018-11-21T00:
source share
5 answers

What you want to do is called serialization . There are several ways to do this, but if you don't need anything interesting, I think using standard Java serialization would be very good.

Perhaps you could use something like this?

package com.example; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Serializer { public static byte[] serialize(Object obj) throws IOException { try(ByteArrayOutputStream b = new ByteArrayOutputStream()){ try(ObjectOutputStream o = new ObjectOutputStream(b)){ o.writeObject(obj); } return b.toByteArray(); } } public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { try(ByteArrayInputStream b = new ByteArrayInputStream(bytes)){ try(ObjectInputStream o = new ObjectInputStream(b)){ return o.readObject(); } } } } 

There are several improvements that can be made. Last but not least, the fact that you can read or write only one object per array of bytes, which may or may not be what you want.

Please note: "Only those objects that support the java.io.Serializable interface can be written to streams" (see java.io.ObjectOutputStream ).

As you may encounter this, the continuous distribution and resizing of java.io.ByteArrayOutputStream may turn out to be a pretty neck of the bottle. Depending on your stream model, you might consider reusing some objects.

To serialize objects that do not implement the Serializable interface, you need to either write your own serializer, for example, using the read * / write * java.io.DataOutputStream methods and the get * / put * java.nio.ByteBuffer methods, possibly together with reflection or draw in third party addiction.

This site lists and compares the performance of some serialization frameworks. Looking at the API, it seems Kryo might fit what you need.

+101
Apr 29 2018-11-21T00:
source share
— -

Use serialize and deserialize in SerializationUtils from commons-lang .

+4
Mar 31 '14 at 5:16
source share

Yes. Just use binary serialization . You should use every implements Serializable object implements Serializable , but it is right from there.

Another option, if you want to avoid using the Serializable interface, is to use reflection and reading and writing data to / from the buffer using the following process:

 /** * Sets all int fields in an object to 0. * * @param obj The object to operate on. * * @throws RuntimeException If there is a reflection problem. */ public static void initPublicIntFields(final Object obj) { try { Field[] fields = obj.getClass().getFields(); for (int idx = 0; idx < fields.length; idx++) { if (fields[idx].getType() == int.class) { fields[idx].setInt(obj, 0); } } } catch (final IllegalAccessException ex) { throw new RuntimeException(ex); } } 

Source

+3
Apr 29 '11 at 21:45
source share

As I mentioned in other similar questions, you might consider compressing the data, since Java serialization is a bit detailed by default. you do this by putting a GZIPInput / OutputStream between object streams and byte streams.

+3
Apr 30 '11 at 0:19
source share

To convert an object to an array of bytes , use the concept of Serialization and De-serialization .

The complete conversion from an array of objects to byte is explained in the manual.

http://javapapers.com/core-java/java-serialization/

 Q. How can we convert object into byte array? Q. How can we serialize a object? Q. How can we De-serialize a object? Q. What is the need of serialization and de-serialization? 
0
May 6 '16 at 5:43
source share



All Articles