0
int[] myIntArray;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new DeflaterOutputStream(byteArrayOutputStream));
objectOutputStream.writeObject(myIntArray);

Now,ObjectOutputStream takes The object and directly serializes it. DeflaterOutputStream compresses the serialized result, then the compressed result is stored in a ByteArrayOutputStream

Can Someone tell me How to Deserialize and get back my original int array back? Plz Share the coding?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Wiki
  • 245
  • 3
  • 13

1 Answers1

1
objectOutputStream.close();
byte[] serialized = byteArrayOutputStream.getBytes();

// and then read back using symmetric constructs as when writing, but using 
// input streams instead of output streams:

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serialized);
ObjectInputStream objectInputStream = 
    new ObjectInputStream(new InflaterInputStream(byteArrayInputStream));
int[] myDesererializedIntArray = (int[]) objectInputStream.readObject();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • You probably need to close or finish the stream before transforming to bytes. I'll edit my answer. – JB Nizet May 01 '13 at 16:06
  • check my complete code here http://stackoverflow.com/questions/16321507/java-deserialization-error-invalid-stream-header – Wiki May 01 '13 at 16:22
  • OK. I finally found the problem. You must use an InflaterInputSTream and not a DeflaterInputStream. Sorry for not having tested my solution sooner. I have now tested it successfully. – JB Nizet May 01 '13 at 16:31