Solved

How to convert Java Serialisable Object to Byte Array

Posted on 2000-03-31
7
579 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 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);
    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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now