I have a problem with java.lang.Object stream. InputStream actually is a real Object[]. readObject method reads only one object. How can I read stream objects correctly? Sample code is below.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream stream = request.getInputStream();
byte[] streamBytes = stream.readAllBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(streamBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
System.out.println(o.getClass()); // Prints: class [Ljava.lang.Object;
System.out.println(o.getClass().getSimpleName()); // Prints: Object[]
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Object[]and you're good to go. That said, in general I would recommend you avoid using Java serialization, and instead use XML or JSON style serialization, as that is more portable.Object[] objects = (Object[]) oI am new to Java. Can you help me about syntax?ObjectInputStreamis outdated and can be insecure (code injection into Java app9 if you don't usesetObjectInputFilter(requires Java 9) on it and configure the stream correctly.ois - new ObjectInputStream(request.getInputStream())and a loop callingois.readObject()will do, terminating whenEOFExceptionis caught. You don't needreadAllBytes()or theByteArrayInputStream(): both are just a waste of time and space.