sakana
sakana

Reputation: 4481

Cannot Serialize/Deserialize ArrayList

I'm trying to serialize and deserialize an array list with a object inside:

HairBirt param = new HairBirt();
param.setName("name");
param.setValue(2.3f);

HairBirt param2 = new HairBirt();
param2.setName("name2");
param2.setValue(2.4f);

ArrayList<HairBirt> list = new ArrayList<HairBirt>();

list.add(param);

list.add(param2);

ByteArrayOutputStream bos = null;
try {
    bos = new ByteArrayOutputStream();
    ObjectOutputStream obj_out = new ObjectOutputStream(bos);
    obj_out.writeObject(list);
} catch (IOException e) {
    e.printStackTrace();
}

String encoded = bos.toString();
try {
    encoded = URLEncoder.encode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
System.out.print("The serialized output is: " + encoded);   

//DECODE

ArrayList<HairBirt> paramDecoded;

String myParam = null;
try {
    myParam = URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
System.out.println("Got parameters");
ByteArrayInputStream bis = new ByteArrayInputStream(myParam.getBytes());

try {
    ObjectInputStream obj_in = new ObjectInputStream(bis);

    paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
} catch (IOException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

The HairList object is also a serializable object.

This code execution is returning the following error:

java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189

in line paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();

I don't know what i'm doing wrong. Can you give a tip?

Update:

Resolved: Just used a native array of HairBirt instead of a ArrayList and it works:

HairBirt[] list = new HairBirt[x];

instead of

ArrayList<HairBirt> list = new ArrayList<HairBirt>();

Thank you all for your help.

Upvotes: 5

Views: 19734

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1499800

Don't use ByteArrayOutputStream.toString() - instead, use toByteArray() and base64-encode that binary data to convert it into a string without losing information.

I strongly suspect that's the main problem - that you were losing the data after serialization. You should probably also close or at least flush the ObjectOutputStream. I don't know offhand whether that actually does anything in this case, but it would seem to be a good idea.

I don't believe there's any base64 support directly in Java (in a public class, anyway) but there are various 3rd party libraries you can use, such as the one in the Apache Commons Codec library.

Upvotes: 9

Steve B.
Steve B.

Reputation: 57274

Have you tried overriding this behavior by declaring your own serialVersionUID in your custom class?

Do you have a specific reason for doing the extra step of serializing through a string? Normally you would just deserialize through an ObjectOutputStream.

Upvotes: 1

Related Questions