-1

I have created an ObjectOutputStream

ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
stream.writeObject(myObject);

but how do I now convert this back into an Object, or even a ByteArray?

I've tried getting an ObjectInputStream like this

ByteArrayOutputStream outputStream = (ByteArrayOutputStream) myProcess.getOutputStream();
            
final ObjectInputStream objectInputStream = new ObjectInputStream(
    new ByteArrayInputStream(outputStream.toByteArray()));

however I get a compile error saying it can't cast the ObjectOutputStream to a ByteArrayOutputStream; yet there seem to be no methods on the ObjectOutputStream to get the data back?

Amal K
  • 4,359
  • 2
  • 22
  • 44
Wayneio
  • 3,466
  • 7
  • 42
  • 73

1 Answers1

1

Here how you do it

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(baos);
stream.writeObject(myObject);

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream inputStream = new ObjectInputStream(bais);
Object o = inputStream.readObject();
talex
  • 17,973
  • 3
  • 29
  • 66
  • Ah, yes, operating on the ByteArrayOutputStream instead of the ObjectOutputStream made the difference – Wayneio Apr 26 '19 at 11:44