I have initialised InputStreamReader
with a byte Array and then initialized ObjectOutputStream
passing it to it's constructor. But it shows error: invalid stream Header
. Please help on how to give some value to ObjectInputStream
.
ObjectStreams
have a very specific format, so you can't just create a byte array and expect it to be in the correct format. You can write objects to a byte array by using ObjectOutputStream
and that will ensure the correct format.
// Write an object to a ByteArrayOutputStream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(someObject);
oout.close();
// Read the object from the resulting array
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
oin.readObject(); // Read the object we wrote in