This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: How do I serialize a Java object into a byte array?

Let’s say I have a serializable class AppMessage.

I want to transfer it over Sockets as byte[] to another machine, and then rebuild the class on the other machine.

How can I do this?

Answer a

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}}Copy the code

Create objects from byte arrays

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

Answer two

The best way to do this is to use SerializationUtils in the Apachecommons Lang

Serialization first:

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

deserialization

YourObject yourObject = SerializationUtils.deserialize(data)
Copy the code

As mentioned earlier, this requires the use of Commons Lang libraries, which can be imported using Gradle:

compile 'org.apache.com mons: the Commons - lang3:3.5'
Copy the code

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>
Copy the code

Answer three

If you are using Java7 or higher, you can use the following resources to improve the code:

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

Another similar approach

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

Answer four

Another interesting way is to use the com. Fasterxml. Jackson. Databind. ObjectMapper

byte[] data = new ObjectMapper().writeValueAsBytes(JAVA_OBJECT_HERE)
Copy the code

Maven rely on

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
Copy the code

Answer five

If you use the Spring framework, you can use the code under Spring-core to do this simply:

import org.springframework.util.SerializationUtils;

byte[] bytes = SerializationUtils.serialize(anyObject);
Object object = SerializationUtils.deserialize(bytes);
Copy the code

The article translated from Stack Overflow:stackoverflow.com/questions/2…