0

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) { … }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125

1 Answers1

1

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

user207421
  • 305,947
  • 44
  • 307
  • 483