residencevil
residencevil

Reputation: 1

Clone objects using Serialization by byte array java

How to serialize and deserialize objects using ByteArrayOutputStream and ByteArrayInputStream? I need a simple and explicit explanation of this topic.

It's a template of this method:

public class Cloner {

    public <T> T clone(T value) { … }
}

Upvotes: 0

Views: 183

Answers (1)

user207421
user207421

Reputation: 310936

You do just what you said:

  1. Create a ByteArrayOutputStream
  2. Serialize it to that ByteArrayOutputStream, via new ObjectOutputStream(baos)
  3. Get the byte array out of the ByteArrayOutputStream
  4. Wrap a ByteArrayInputStream around it
  5. Wrap an ObjectInputStream around that
  6. De-serialize.

NB your generic signature could usefully be <T extends Serializable>.

Upvotes: 1

Related Questions