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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

719 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