Search code examples
javaamazon-s3serializationdeserializationinputstream

Converting a non-serializable object to input stream in java


I'm trying to upload an object to S3. S3 needs the object in InputStream. But the fields in this object are not serializable and cannot be directly converted into InputStream.

So I thought of converting each field in the object to InputStream and then either append these into one InputStream or add them to a container object.

But the problem with the first approach is I don't know how I can divide the combined stream into individual streams.

And in the second approach I'm not aware of any container InputStream object.

Is there a simpler way to handle this ?

I'm a beginner in java and any help would be appreciated.

Edit :

Saw the post suggested by @magicmn and also referred to this post.

My container class:

public class ContainerClass implements Serializable {
    private String someString;
    private String someOtherString;
    private  transient NonSerializableObject1 object1;
    private  transient List<NonSerializableObject2> object2;
    private  transient NonSerializableObject3 object13;
} 

The problem in my case is there is a way to convert the 3 Non Serializable Objects into byte[].

So I can do something like this in the writeObject :

private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
    objectOutputStream.defaultWriteObject();
    objectOutputStream.write(someMethodToconvertObject1ToByteArray());
    objectOutputStream.write(someMethodToconvertObject2ToByteArray());
    objectOutputStream.write(someMethodToconvertObject3ToByteArray());
 }

But I'm confused as to how I can implement the readObject(). I'm not able to figure out a way where in I can split the InputStream to obtain the 3 byte arrays which I added in the writeObject().


Solution

  • This is how I solved this. It might not be an ideal solution but its working for now.

    public class ContainerClass implements Serializable {
        private String someString;
        private String someOtherString;
        private  transient NonSerializableObject1 object1;
        private  transient List<NonSerializableObject2> object2;
        private  transient NonSerializableObject3 object3;
    
        private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
            objectOutputStream.defaultWriteObject();
            objectOutputStream.write(someMethodToconvertObject1ToByteArray());
            objectOutputStream.write(someMethodToconvertObject2ToByteArray());
            objectOutputStream.write(someMethodToconvertObject3ToByteArray());
        }
    
        private void readObject(final ObjectInputStream objectInputStream) throws ClassNotFoundException, IOException {
            objectInputStream.defaultReadObject();
            this.NonSerializableObject1 = (NonSerializableObject1) someMethodToConvertByteArrayToObject((byte[]) objectInputStream.readObject());
            this.NonSerializableObject2 = (NonSerializableObject1) someMethodToConvertByteArrayToObject((byte[]) objectInputStream.readObject());
            this.NonSerializableObject3 = (NonSerializableObject1) someMethodToConvertByteArrayToObject((byte[]) objectInputStream.readObject());
        }
    
    } 
    

    Note that the order of writing and reading the objects must be same.