0

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().

  • https://stackoverflow.com/q/6163872/9712270 has already some answers for you. In every case you need to implement your own (de-)serializitazion logic. – magicmn Mar 10 '21 at 07:10
  • @magicmn Thanks the response, I tried out adding custom serialization and de-serialization logic as suggested. But I'm facing difficulty in the de-serialization part. Any idea how I can get this done? – confuseddev Mar 11 '21 at 03:54
  • Not really. How to do it depends on the object and it's attributes you want to (de-)serialize. – magicmn Mar 11 '21 at 07:23

2 Answers2

0

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.

0

You can use the Jackson library's ObjectMapper class's writeValueAsBytes() to serialize the object to a byte array (example), and then use ByteArrayInputStream to convert the byte array into an InputStream.

bjmd
  • 21
  • 3