Home > Java > javaTutorial > How to Convert Java Serializable Objects to Byte Arrays and Back?

How to Convert Java Serializable Objects to Byte Arrays and Back?

Susan Sarandon
Release: 2025-01-03 04:52:39
Original
908 people have browsed it

How to Convert Java Serializable Objects to Byte Arrays and Back?

Converting Java Serializable Objects to Byte Arrays

In situations where you need to transmit Java objects across a network or store them in a binary format, converting them to byte arrays can be essential. Let's consider a scenario where we have a serializable class AppMessage. To send AppMessage instances over sockets to another machine and reconstruct them from the received bytes, we can follow these steps:

Serialization: Converting an Object to a Byte Array

  1. Prepare the ByteArrayOutputStream: Create a ByteArrayOutputStream to serve as a temporary storage for the serialized object.
  2. ObjectOutputStream: Use an ObjectOutputStream to write the object into the ByteArrayOutputStream.
  3. Flush and Read Bytes: Flush the ObjectOutputStream to ensure all data is written to the byte buffer and retrieve the byte array from ByteArrayOutputStream.

The code snippet for this process:

static byte[] serialize(final Object obj) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(obj);
        out.flush();
        return bos.toByteArray();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Copy after login

Deserialization: Reconstructing an Object from a Byte Array

  1. Create a ByteArrayInputStream: Convert the byte array back into an input stream using ByteArrayInputStream.
  2. ObjectInputStream: Create an ObjectInputStream from ByteArrayInputStream to read the object data.
  3. readObject(): Use the readObject() method to reconstruct the original object from the byte stream.

The corresponding code snippet:

static Object deserialize(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    try (ObjectInput in = new ObjectInputStream(bis)) {
        return in.readObject();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Copy after login

With these methods in place, you can efficiently send serializable objects, such as AppMessage instances, over networks and reconstruct them on the receiving end.

The above is the detailed content of How to Convert Java Serializable Objects to Byte Arrays and Back?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template