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
.
Asked
Active
Viewed 374 times
-3
-
Is the byte array being passed to the InputStream a valid, serialized java object? – rodit Feb 20 '17 at 12:25
-
1Have you got any code? – Steve Smith Feb 20 '17 at 12:26
-
Are you sure you haven't confused ObjectOutputStream with ObjectInputStream ? Your question is not being consistent with itself. – Jeremy Grand Feb 20 '17 at 12:27
1 Answers
0
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
![](../../users/profiles/2541560.webp)
Kayaman
- 67,952
- 3
- 69
- 110