Converting Serializable Objects to Byte Arrays in Java
In Java, the Serializable interface allows objects to be converted into a stream of bytes. This feature is crucial when objects need to be transmitted over a network or stored in a database.
Encoding an Object into a Byte Array
To encode an object into a byte array, you can use the following steps:
- Instantiate a ByteArrayOutputStream object.
- Create an ObjectOutputStream object using the ByteArrayOutputStream as its output stream.
- Write the object to the ObjectOutputStream using the writeObject method.
- Flush the ObjectOutputStream to ensure all data is written to the ByteArrayOutputStream.
- Retrieve the byte array from the ByteArrayOutputStream using the toByteArray method.
Decoding an Object from a Byte Array
To decode an object from a byte array, you can do the following:
- Instantiate a ByteArrayInputStream object using the byte array.
- Create an ObjectInputStream object using the ByteArrayInputStream as its input stream.
- Read the object from the ObjectInputStream using the readObject method.
Example Code
Here are utility methods for serialization and deserialization:
Serialization:
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); } }
Deserialization:
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); } }
With these methods, you can easily convert objects to and from byte arrays, enabling you to transmit data over networks or persist it to storage.
The above is the detailed content of How Can I Serialize and Deserialize Java Objects to and from Byte Arrays?. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools