How to convert Java Serialisable Object to Byte Array

Posted on 2000-03-31
Last Modified: 2012-06-27
does anybody know how to convert a Java Object like Image Object into a byte[] to do stuff with it after....

For exemple if i create serialisable object and i want to store them in database in byte []

help !
Question by:NextOne

Expert Comment

ID: 2672685
The is a couple of classes in the io package called ByteArrayOutputStream and ByteArrayInputStream. Wrap these with any OutputStream you desire and you have your byte[].
LVL 16

Expert Comment

ID: 2672803
you need

LVL 19

Accepted Solution

Jim Cakalic earned 50 total points
ID: 2674086
Specifically, to serialize a Serializable object to a byte array:

    Object o = <whatever>;
    ByteArrayOutputStream bst = new ByteArrayOutputStream();
    ObjectOutputStream ost = new ObjectOutputStream(bst);
    byte[] b = bst.toByteArray();

To deserialize from the byte array:

    byte[] b = <whatever>;
    ByteArrayInputStream bst = new ByteArrayInputStream(b);
    ObjectInputStream ost = new ObjectInputStream(bst);
    Object o = ost.readObject();

Best regards,
Jim Cakalic
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.


Author Comment

ID: 2674258
I prefere the Jim cakalic answer that is full of explenation and very interesting... I gonna try

thanks to everybody

Author Comment

ID: 2674275
thanks, i gonna try
LVL 16

Expert Comment

ID: 2675598
>> how to convert a Java Object like Image Object into a byte[]

well - java.awt.Image is not Serializable :)

Author Comment

ID: 2675826
Well yes, but i found a solution, i read directly the file...
But i use a Sybase DataBase and I've a stream problem,
I can only read 32768 bytes...

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
Java Timer (static) 9 43
Java Options in Zimbra Server 2 53
check java version using powershell 13 92
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question