Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Serialize object to a byte array

Hi,

Can someone show me how to serialize a simple class to a byte array? Something like:

class Test implements Serializable {
    String str = "hello";
}

void main()
{
    Test t = new Test();
    ByteArray b = new ByteArray();
    t.toBytearray(b);

    // later on just recreate the object from the byte array
    Test t2 = new Test();
    t2.fromByteArray(b);
}

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The proposed solution is OK.
Anyhow, last year I have found one interesting post with the class enabling to easy serialize/deserialize JAVA objects (actually the list of objects).

Check next URL (seek for the last post):
http://forum.java.sun.com/thread.jspa?threadID=5226234&tstart=0

You have a very good example there. Hope this helps.
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Hmm not having any luck - I get an IOException as soon as I try to write. Here's the sample I had put together, pretty much exactly like yours, I must be missing something:

public class Main
{
    public class Test1 implements Serializable {
        public static final long serialVersionUID = 0;
        String m_str = "hello";
        public Test1() {}
    }
   
    public static void main(String[] args)
    {
        Main m = new Main();
        m.test();
    }
      
    public void test()
    {
        // Serialize first.
        Test1 t1 = new Test1();
        System.out.println("Test1: " + t1.toString());
              
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(t1);


Yeah right there it throws an IOException, on writeObject().... any idea what I'm missing?

Thanks
Could you provide more info on the error message?
It is pretty useless to me, it just says:

    "Main"

I use it like:

try {
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(t1);
}
catch (IOException e) {
    System.out.println(e.getMessage());
}
Seeing the full stack trace might help out in finding the problem.
How do you do a full stack trace? I just get:

    [Ljava.lang.StackTraceElement;@7bb290

after calling:

    System.out.println(e.getStackTrace());
Just do e.printStackTrace();
ok here it is:

java.io.NotSerializableException: Main
      at java.io.ObjectOutputStream.writeObject0(Unknown Source)
      at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
      at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
      at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
      at java.io.ObjectOutputStream.writeObject0(Unknown Source)
      at java.io.ObjectOutputStream.writeObject(Unknown Source)
      at Main.test(Main.java:40)
      at Main.main(Main.java:26)
it appears it can't work because my definition of Test1 is inside another class - so only top level classes can be serialized I guess. How annoying!
You could make the Test1 class static or make Main serializable as well. The Main class will be an anonymous member of the subclass, so it needs to be serializable as well.