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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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:
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?
ASKER
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.getMe ssage());
}
"Main"
I use it like:
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(t1);
}
catch (IOException e) {
System.out.println(e.getMe
}
Seeing the full stack trace might help out in finding the problem.
ASKER
How do you do a full stack trace? I just get:
[Ljava.lang.StackTraceElem ent;@7bb29 0
after calling:
System.out.println(e.getSt ackTrace() );
[Ljava.lang.StackTraceElem
after calling:
System.out.println(e.getSt
Just do e.printStackTrace();
ASKER
ok here it is:
java.io.NotSerializableExc eption: Main
at java.io.ObjectOutputStream .writeObje ct0(Unknow n Source)
at java.io.ObjectOutputStream .defaultWr iteFields( Unknown Source)
at java.io.ObjectOutputStream .writeSeri alData(Unk nown Source)
at java.io.ObjectOutputStream .writeOrdi naryObject (Unknown Source)
at java.io.ObjectOutputStream .writeObje ct0(Unknow n Source)
at java.io.ObjectOutputStream .writeObje ct(Unknown Source)
at Main.test(Main.java:40)
at Main.main(Main.java:26)
java.io.NotSerializableExc
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at Main.test(Main.java:40)
at Main.main(Main.java:26)
ASKER
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.
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.