How to convert Java Serialisable Object to Byte Array

Hi,
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 !
NextOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NevynCommented:
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[].
0
heyhey_Commented:
you need
java.awt.image.PixelGrabber

0
Jim CakalicSenior Developer/ArchitectCommented:
Specifically, to serialize a Serializable object to a byte array:

    Object o = <whatever>;
    ByteArrayOutputStream bst = new ByteArrayOutputStream();
    ObjectOutputStream ost = new ObjectOutputStream(bst);
    ost.writeObject(o);
    ost.flush();
    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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

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

well - java.awt.Image is not Serializable :)
0
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...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.