?
Solved

How to convert Java Serialisable Object to Byte Array

Posted on 2000-03-31
7
Medium Priority
?
591 Views
Last Modified: 2012-06-27
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 !
0
Comment
Question by:NextOne
7 Comments
 
LVL 2

Expert Comment

by:Nevyn
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[].
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2672803
you need
java.awt.image.PixelGrabber

0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 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);
    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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

thanks to everybody
0
 

Author Comment

by:NextOne
ID: 2674275
thanks, i gonna try
0
 
LVL 16

Expert Comment

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

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

Author Comment

by:NextOne
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...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

809 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