How to convert Java Serialisable Object to Byte Array

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 !
Who is Participating?
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
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
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[].
you need

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

thanks to everybody
NextOneAuthor Commented:
thanks, i gonna try
>> how to convert a Java Object like Image Object into a byte[]

well - java.awt.Image is not Serializable :)
NextOneAuthor Commented:
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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.