The read methods ObjectInputStream

JVM running the applet could be anything, from Java 1.0.2 through the latest JVM. Most servers, on the other hand, are written using JDK1.2.2 or later. [ 3] If you pass serialized objects between an applet and a server, you should specify the serialization protocol. [ 3] The main exception is EJB containers that require earlier versions of Java. At this writing, for example, Oracle 8 is EJB container uses JDK 1.1.6.

10.2.1.3 Methods that customize the serialization mechanism

The last group of methods consists mostly of protected methods that provide hooks that allow the serialization mechanism itself, rather than the data associated to a particular class, to be customized. These methods are: public ObjectOutputStream.PutField putFields ; protected void annotateClassClass cl; protected void annotateProxyClassClass cl; protected boolean enableReplaceObjectboolean enable; protected Object replaceObjectObject obj; protected void drain ; protected void writeObjectOverrideObject obj; protected void writeClassDescriptorObjectStreamClass classdesc; protected void writeStreamHeader ; These methods are more important to people who tailor the serialization algorithm to a particular use or develop their own implementation of serialization. As such, they require a deeper understanding of the serialization algorithm. Well discuss these methods in more detail later, after weve gone over the actual algorithm used by the serialization mechanism.

10.2.2 ObjectInputStream

ObjectInputStream , defined in the java.io package, implements the reading-in part of the serialization algorithm. It is the companion to ObjectOutputStream ™objects serialized using ObjectOutputStream can be deserialized using ObjectInputStream . Like ObjectOutputStream , the methods implemented by ObjectInputStream can be grouped into three categories: methods that read information from the stream, methods that are used to control the streams behavior, and methods that are used to customize the serialization algorithm.

10.2.2.1 The read methods

The first, and most intuitive, category consists of the read methods: public int read ; public int readbyte[] b, int off, int len; public boolean readBoolean ; public byte readByte ; public char readChar ; public double readDouble ; public float readFloat ; public intreadInt ; public long readLong ; public Object readObject ; public short readShort ; public byte readUnsignedByte ; public short readUnsignedShort ; public String readUTF ; void defaultReadObject ; Just as with ObjectOutputStream s write methods, these methods should be familiar. readFloat , for example, works exactly as you would expect after reading Chapt er 1 : it reads four bytes from the stream and converts them into a single floating-point number, which is returned by the method call. And, again as with ObjectOutputStream , there are two new methods here: readObject and defaultReadObject . Just as writeObject serializes an object, readObject deserializes it. Deserializing an object involves doing two things: creating an ObjectInputStream and then calling readObject . The following code snippet shows the entire process, creating a copy of an object and all the objects to which it refers from a file: FileInputStream underlyingStream = new FileInputStreamC:\\temp\\test; ObjectInputStream deserializer = new ObjectInputStreamunderlyingStream; Object deserializedObject = deserializer.readObject ; This code is exactly inverse to the code we used for serializing the object in the first place. If we wanted to make a deep copy of a serializable object, we could first serialize the object and then deserialize it, as in the following code example: ByteArrayOutputStream memoryOutputStream = new ByteArrayOutputStream ; ObjectOutputStream serializer = new ObjectOutputStreammemoryOutputStream; serializer.writeObjectserializableObject; serializer.flush ; ByteArrayInputStream memoryInputStream = new ByteArrayInputStreammemoryOutputStream. toByteArray ; ObjectInputStream deserializer = new ObjectInputStreammemoryInputStrea m; Object deepCopyOfOriginalObject = deserializer.readObject ; This code simply places an output stream into memory, serializes the object to the memory stream, creates an input stream based on the same piece of memory, and runs the deserializer on the input stream. The end result is a deep copy of the object with which we started.

10.2.2.2 The stream manipulation methods