Vote count:
0
I have a requirement to read a stream of bytes from a remote system. The remote system has its own client API to read the bytes. But at my end, I have to convert the byte array to a POJO. While doing so, I am getting error java.io.StreamCorruptedException: invalid stream header:
.
To test the functionality, I wrote following program to convert a String
to a byte array
and then convert the byte array to an Object
.
public class ByteToObject {
public static void main(String[] args) {
try {
final String str = "Tiger";
System.out.println("\nByte array for string '" + str + "' --> \n" + Arrays.toString(getByteArray(str)));
System.out.println("Object read --> " + getObject(getByteArray(str)));
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] getByteArray(final String str) throws Exception {
return str.getBytes(CharEncoding.UTF_8);
}
private static Object getObject(final byte[] byteArray) throws Exception {
InputStream byteArrayStream = null;
ObjectInputStream inputStream = null;
try {
byteArrayStream = new ByteArrayInputStream(byteArray);
inputStream = new ObjectInputStream(byteArrayStream);
return inputStream.readObject();
} finally {
if(null != byteArrayStream) {
byteArrayStream.close();
}
if(null != inputStream) {
inputStream.close();
}
}
}
}
Appreciate if someone can help what is wrong here?
asked 39 secs ago
Aucun commentaire:
Enregistrer un commentaire