WebObjects 5.2.3

com.webobjects.foundation.xml
Class NSXMLInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.ObjectInputStream
          extended bycom.webobjects.foundation.xml.NSXMLInputStream
All Implemented Interfaces:
DataInput, ObjectInput, ObjectStreamConstants

public final class NSXMLInputStream
extends ObjectInputStream

An NSXMLInputStream deserializes primitive data and objects previously written as XML data using an NSXMLOutputStream.

NSXMLInputStream and NSXMLOutputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. Since the data is in XML data (which is plain text), visually inspection of the data would be easier. NSXMLInputStream is used to recover those objects previously serialized. Other uses include passing objects between hosts using a socket stream or for marshaling and unmarshaling arguments and parameters in a remote communication system.

NSXMLInputStream ensures that the types of all objects in the graph created from the stream match the classes present in the Java Virtual Machine. Classes are loaded as required using the standard mechanisms.

Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams.

The method readObject is used to read an object from the stream. Java's safe casting should be used to get the desired type. In Java, strings and arrays are objects and are treated as objects during serialization. When read they need to be cast to the expected type.

Primitive data types can be read from the stream using the appropriate method on DataInput.

The default deserialization mechanism for objects restores the contents of each field to the value and type it had when it was written. Fields declared as transient or static are ignored by the deserialization process. References to other objects cause those objects to be read from the stream as necessary. Graphs of objects are restored correctly using a reference sharing mechanism. New objects are always allocated when deserializing, which prevents existing objects from being overwritten.

Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL). No-arg constructors are invoked for the non-serializable classes and then the fields of the serializable classes are restored from the stream starting with the serializable class closest to java.lang.object and finishing with the object's most specific class.

For example to read from a stream as written by the corresponding example in NSXMLOutputStream:

	FileInputStream istream = new FileInputStream("t.tmp");
	NSXMLInputStream p = new NSXMLInputStream(istream);

	int i = p.readInt();
	String today = (String)p.readObject();
	Date date = (Date)p.readObject();

	p.close();
 

Classes control how they are serialized by implementing either the java.io.Serializable or java.io.Externalizable interfaces.

Implementing the Serializable interface allows object serialization to save and restore the entire state of the object and it allows classes to evolve between the time the stream is written and the time it is read. It automatically traverses references between objects, saving and restoring entire graphs.

Serializable classes that require special handling during the serialization and deserialization process should implement the following methods:

 private void writeObject(java.io.ObjectOutputStream stream)
     throws IOException;
 private void readObject(java.io.ObjectInputStream stream)
     throws IOException, ClassNotFoundException; 
 

The readObject method is responsible for reading and restoring the state of the object for its particular class using XML data written to the stream by the corresponding writeObject method. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is restored by reading data from the NSXMLInputStream for the individual fields and making assignments to the appropriate fields of the object. Reading primitive data types is supported by DataInput.

Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true. Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions. If there is no corresponding writeObject method, then the end of default serialized data marks the end of the allotted data.

Primitive and object read calls issued from within a readExternal method behave in the same manner--if the stream is already positioned at the end of data written by the corresponding writeExternal method, object reads will throw OptionalDataExceptions with eof set to true, bytewise reads will return -1, and primitive reads will throw EOFExceptions.

Serialization does not read or assign values to the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class. It is frequently the case that the fields of that class are accessible (public, package, or protected) or that there are get and set methods that can be used to restore the state.

Any exception that occurs while deserializing an object will be caught by the NSXMLInputStream and abort the reading process.

Implementing the Externalizable interface allows the object to assume complete control over the contents and format of the object's serialized form. The methods of the Externalizable interface, writeExternal and readExternal, are called to save and restore the objects state. When implemented by a class they can write and read their own state using all of the methods of ObjectOutput and ObjectInput. It is the responsibility of the objects to handle any versioning that occurs.

We do not provide a counterpart interface of NSXMLObjectOutput; the key in the attribute is simply ignored during deserialization.

NSXMLInputStream is made final for performance reasons.

See Also:
ObjectInputStream, DataInput, Serializable, Externalizable

Nested Class Summary
 
Nested classes inherited from class java.io.ObjectInputStream
ObjectInputStream.GetField
 
Field Summary
 
Fields inherited from interface java.io.ObjectStreamConstants
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING
 
Constructor Summary
NSXMLInputStream(InputStream in)
          Creates an ObjectInputStream that reads from the specified InputStream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read without blocking.
 void close()
          Closes the input stream.
 void defaultReadObject()
          Read the non-static and non-transient fields of the current class from this stream.
 int read()
          Reads a byte of data.
 int read(byte[] buf, int off, int len)
          Reads into an array of bytes.
 boolean readBoolean()
          Reads in a boolean.
 byte readByte()
          Reads an 8 bit byte.
 char readChar()
          Reads a 16 bit char.
 double readDouble()
          Reads a 64 bit double.
 ObjectInputStream.GetField readFields()
          Reads the persistent fields from the stream and makes them available by name.
 float readFloat()
          Reads a 32 bit float.
 void readFully(byte[] buf)
          Reads bytes, blocking until all bytes are read.
 void readFully(byte[] buf, int off, int len)
          Reads bytes, blocking until all bytes are read.
 int readInt()
          Reads a 32 bit int.
 String readLine()
          Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.
 long readLong()
          Reads a 64 bit long.
protected  Object readObjectOverride()
          This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor.
 short readShort()
          Reads a 16 bit short.
 int readUnsignedByte()
          Reads an unsigned 8 bit byte.
 int readUnsignedShort()
          Reads an unsigned 16 bit short.
 String readUTF()
          Reads a UTF format String.
 void registerValidation(ObjectInputValidation obj, int prio)
          Register an object to be validated before the graph is returned.
 int skipBytes(int len)
          Skips bytes, block until all bytes are skipped.
 
Methods inherited from class java.io.ObjectInputStream
enableResolveObject, readClassDescriptor, readObject, readStreamHeader, readUnshared, resolveClass, resolveObject, resolveProxyClass
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.io.ObjectInput
read, skip
 

Constructor Detail

NSXMLInputStream

public NSXMLInputStream(InputStream in)
                 throws IOException,
                        StreamCorruptedException
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.

Parameters:
in - input stream to read from
Throws:
StreamCorruptedException - if the stream header is incorrect
IOException - if an I/O error occurs while reading stream header
SecurityException - if untrusted subclass illegally overrides security-sensitive methods
See Also:
readFields(), NSXMLOutputStream.NSXMLOutputStream(OutputStream)
Method Detail

available

public int available()
              throws IOException
Returns the number of bytes that can be read without blocking.

Returns:
the number of available bytes.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream

close

public void close()
           throws IOException
Closes the input stream. Must be called to release any resources associated with the stream.

Throws:
IOException - If an I/O error has occurred.

defaultReadObject

public void defaultReadObject()
                       throws IOException,
                              ClassNotFoundException
Read the non-static and non-transient fields of the current class from this stream. This may only be called from the readObject method of the class being deserialized. It will throw the NotActiveException if it is called otherwise.

Throws:
ClassNotFoundException - if the class of a serialized object could not be found.
IOException - if an I/O error occurs.
NotActiveException - if the stream is not currently reading objects.

read

public int read()
         throws IOException
Reads a byte of data. This method will block if no input is available.

Returns:
the byte read, or -1 if the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.

read

public int read(byte[] buf,
                int off,
                int len)
         throws IOException
Reads into an array of bytes. This method will block until some input is available. Consider using java.io.DataInputStream.readFully to read exactly 'length' bytes.

Parameters:
buf - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the actual number of bytes read, -1 is returned when the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.
See Also:
DataInputStream.readFully(byte[],int,int)

readBoolean

public boolean readBoolean()
                    throws IOException
Reads in a boolean.

Returns:
the boolean read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readByte

public byte readByte()
              throws IOException
Reads an 8 bit byte.

Returns:
the 8 bit byte read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readChar

public char readChar()
              throws IOException
Reads a 16 bit char.

Returns:
the 16 bit char read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readDouble

public double readDouble()
                  throws IOException
Reads a 64 bit double.

Returns:
the 64 bit double read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readFields

public ObjectInputStream.GetField readFields()
                                      throws IOException,
                                             ClassNotFoundException
Reads the persistent fields from the stream and makes them available by name.

Returns:
the GetField object representing the persistent fields of the object being deserialized
Throws:
ClassNotFoundException - if the class of a serialized object could not be found.
IOException - if an I/O error occurs.
NotActiveException - if the stream is not currently reading objects.

readFloat

public float readFloat()
                throws IOException
Reads a 32 bit float.

Returns:
the 32 bit float read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readFully

public void readFully(byte[] buf)
               throws IOException
Reads bytes, blocking until all bytes are read.

Parameters:
buf - the buffer into which the data is read
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readFully

public void readFully(byte[] buf,
                      int off,
                      int len)
               throws IOException
Reads bytes, blocking until all bytes are read.

Parameters:
buf - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes to read
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readInt

public int readInt()
            throws IOException
Reads a 32 bit int.

Returns:
the 32 bit integer read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readLine

public String readLine()
                throws IOException
Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.

Reads in a line that has been terminated by a \n, \r, \r\n or EOF.

Returns:
a String copy of the line.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream

readLong

public long readLong()
              throws IOException
Reads a 64 bit long.

Returns:
the read 64 bit long.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readObjectOverride

protected Object readObjectOverride()
                             throws OptionalDataException,
                                    ClassNotFoundException,
                                    IOException
This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor. The subclass is expected to provide an override method with the modifier "final".

Returns:
the Object read from the stream.
Throws:
ClassNotFoundException - Class definition of a serialized object cannot be found.
OptionalDataException - Primitive data was found in the stream instead of objects.
IOException - if I/O errors occurred while reading from the underlying stream
See Also:
ObjectInputStream.readObject()

readShort

public short readShort()
                throws IOException
Reads a 16 bit short.

Returns:
the 16 bit short read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readUTF

public String readUTF()
               throws IOException
Reads a UTF format String.

Returns:
the String.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream

readUnsignedByte

public int readUnsignedByte()
                     throws IOException
Reads an unsigned 8 bit byte.

Returns:
the 8 bit byte read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
Reads an unsigned 16 bit short.

Returns:
the 16 bit short read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

registerValidation

public void registerValidation(ObjectInputValidation obj,
                               int prio)
                        throws NotActiveException,
                               InvalidObjectException
Register an object to be validated before the graph is returned. While similar to resolveObject these validations are called after the entire graph has been reconstituted. Typically, a readObject method will register the object with the stream so that when all of the objects are restored a final set of validations can be performed.

Parameters:
obj - the object to receive the validation callback.
prio - controls the order of callbacks;zero is a good default. Use higher numbers to be called back earlier, lower numbers for later callbacks. Within a priority, callbacks are processed in no particular order.
Throws:
NotActiveException - The stream is not currently reading objects so it is invalid to register a callback.
InvalidObjectException - The validation object is null.

skipBytes

public int skipBytes(int len)
              throws IOException
Skips bytes, block until all bytes are skipped.

Parameters:
len - the number of bytes to be skipped
Returns:
the actual number of bytes skipped.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

Last updated Thu Oct 21 15:04:16 PDT 2004.

Copyright © 2004 Apple Computer, Inc.