Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Re: Weird Integer Serialization Problem


Message #1 by "Matt McHenry" <mmchenry@c...> on Wed, 5 Jun 2002 20:10:11
it sounds like you problem (Matt) is something to do with incorrect
conversion between binary data and specific character encodings or something
= how strange. Did you every find out more info? I remember seeing this
question asked on this list before.

chanoch

----- Original Message -----
From: "Matt McHenry" <mmchenry@c...>
To: "Professional Java" <pro_java@p...>
Sent: Wednesday, June 05, 2002 8:10 PM
Subject: [pro_java] Re: Weird Integer Serialization Problem


> I ran into this problem myself the other day, but couldn't find anything
> helpful on the web (your message was about the only mention of this
problem
> I was able to find on google).  So, I'll describe the workaround I'm
using.
>
> I was serializing to a ByteArrayOutputStream, and then storing the result
> in a string.  This was somehow corrupting the byte stream, so that when I
> tried to create a ByteArrayInputStream from the String.getBytes(), it was
> not identical to the bytes that the serialization call wrote to the
stream.
>
> The workaround is simply to pass around the byte array itself, rather than
> converting it to a String.
>
> The following sample code should illustrate the problem & workaround:
>
> public class Test implements Serializable{
>     Integer n;
>
>     public Test(){
>         n = new Integer(1);
>     }
>
>     public static void main(String[] args){
>         Test t = new Test();
>
>         try{
>             ByteArrayOutputStream outStream = new ByteArrayOutputStream();
>             ObjectOutputStream outObj = new ObjectOutputStream(outStream);
>
>             outObj.writeObject(t);
>
>             String s = outStream.toString();
>
>             byte[] ba1 = s.getBytes();
>             byte[] ba2 = outStream.toByteArray();
>
>             System.out.println("ba1.length = " + ba1.length);
>             System.out.println("ba2.length = " + ba2.length);
>
>             for(int i=0;i<ba1.length;i++){
>                 if(ba1[i] != ba2[i]){
>                     System.out.println("arrs differ: ba1[" + i + "]: " +
> ba1[i] +
>                                        "; ba2[" + i + "}: " + ba2[i]);
>                 }
>             }
>
>             ByteArrayInputStream inStream = new
> ByteArrayInputStream(outStream.toByteArray());
>             // doesn't work:
>             // ByteArrayInputStream inStream = new
> ByteArrayInputStream(s.getBytes());
>             ObjectInputStream inObj = new ObjectInputStream(inStream);
>             t = (Test)inObj.readObject();
>
>
> ---
> I'm having a problem trying to serialize a class which
> basically has:
>
> private HashMap a= new HashMap();
> private HashMap b= new HashMap();
> a few private int variables;
>
> public void addtoA(Integer, Float);
> public void addtoB(Integer, Float);
>
> The constructor leaves a and b untouched, so serializing &
> deserializing an 'empty' instance works fine.
>
> When I call addtoA or addtoB, then serialize, and then try
> to deserialize I get:
>
> java.io.InvalidClassException: java.lang.Integer; Local class not
> compatible: stream classdesc serialVersionUID=1360826667802527544
> local class serialVersionUID=1360826667806852920
>
> This is on the same program, of course using the same
> version of the class (using JDK 1.3.1_02 on W2k).
>


  Return to Index