javaserializationdeserializationobject-serialization

Object Deserialization- to get back int array from serialized Object


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?


Solution

  • 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();