Marshalling API version 1.0.0.GA

org.jboss.marshalling
Class MarshallerObjectInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.ObjectInputStream
          extended by org.jboss.marshalling.MarshallerObjectInputStream
All Implemented Interfaces:
Closeable, DataInput, ObjectInput, ObjectStreamConstants

public abstract class MarshallerObjectInputStream
extends ObjectInputStream

A marshaller's object input stream. Used by marshallers for compatibility with Java serialization. Instances of this class may be passed in to the overriden serialization methods for a class implementing Serializable.

This class is not part of the marshalling API; rather it is intended for marshaller implementors to make it easier to develop Java serialization-compatible marshallers.


Nested Class Summary
 
Nested classes/interfaces 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_ENUM, 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_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING
 
Constructor Summary
protected MarshallerObjectInputStream(Unmarshaller input)
          Construct a new instance which delegates to the given unmarshaller.
 
Method Summary
 int available()
          Returns the number of bytes that can be read without blocking.
 void close()
          Closes the input stream.
abstract  void defaultReadObject()
          Read the non-static and non-transient fields of the current class from this stream.
protected  boolean enableResolveObject(boolean enable)
          Enable the stream to allow objects read from the stream to be replaced.
 void mark(int readlimit)
          Marks the current position in this input stream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads a byte of data.
 int read(byte[] buf)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
 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.
protected  ObjectStreamClass readClassDescriptor()
          Read a class descriptor from the serialization stream.
 double readDouble()
          Reads a 64 bit double.
abstract  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. 
 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.
protected  void readStreamHeader()
          The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers.
 Object readUnshared()
          Reads an "unshared" object from the ObjectInputStream.
 int readUnsignedByte()
          Reads an unsigned 8 bit byte.
 int readUnsignedShort()
          Reads an unsigned 16 bit short.
 String readUTF()
          Reads a String in modified UTF-8 format.
abstract  void registerValidation(ObjectInputValidation obj, int prio)
          Register an object to be validated before the graph is returned.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on this input stream.
protected  Class<?> resolveClass(ObjectStreamClass desc)
          Load the local class equivalent of the specified stream class description.
protected  Object resolveObject(Object obj)
          This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization.
protected  Class<?> resolveProxyClass(String[] interfaces)
          Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.
 long skip(long n)
          Skips over and discards n bytes of data from this input stream.
 int skipBytes(int len)
          Skips bytes.
 
Methods inherited from class java.io.ObjectInputStream
readObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MarshallerObjectInputStream

protected MarshallerObjectInputStream(Unmarshaller input)
                               throws IOException,
                                      SecurityException
Construct a new instance which delegates to the given unmarshaller.

Parameters:
input - the delegate unmarshaller
Throws:
IOException - if an I/O error occurs
SecurityException - if the caller does not have permission to construct an instance of this class
Method Detail

readObjectOverride

protected Object readObjectOverride()
                             throws IOException,
                                    ClassNotFoundException
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".

Overrides:
readObjectOverride in class ObjectInputStream
Returns:
the Object read from the stream.
Throws:
IOException - if I/O errors occurred while reading from the underlying stream
ClassNotFoundException - Class definition of a serialized object cannot be found.
See Also:
ObjectInputStream.ObjectInputStream(), ObjectInputStream.readObject()

readUnshared

public Object readUnshared()
                    throws IOException,
                           ClassNotFoundException
Reads an "unshared" object from the ObjectInputStream. This method is identical to readObject, except that it prevents subsequent calls to readObject and readUnshared from returning additional references to the deserialized instance obtained via this call. Specifically: Deserializing an object via readUnshared invalidates the stream handle associated with the returned object. Note that this in itself does not always guarantee that the reference returned by readUnshared is unique; the deserialized object may define a readResolve method which returns an object visible to other parties, or readUnshared may return a Class object or enum constant obtainable elsewhere in the stream or through external means. If the deserialized object defines a readResolve method and the invocation of that method returns an array, then readUnshared returns a shallow clone of that array; this guarantees that the returned array object is unique and cannot be obtained a second time from an invocation of readObject or readUnshared on the ObjectInputStream, even if the underlying data stream has been manipulated.

ObjectInputStream subclasses which override this method can only be constructed in security contexts possessing the "enableSubclassImplementation" SerializablePermission; any attempt to instantiate such a subclass without this permission will cause a SecurityException to be thrown.

Overrides:
readUnshared in class ObjectInputStream
Returns:
reference to deserialized object
Throws:
StreamCorruptedException - if control information in the stream is inconsistent
ObjectStreamException - if object to deserialize has already appeared in stream
IOException - if an I/O error occurs during deserialization
ClassNotFoundException - if class of an object to deserialize cannot be found

read

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

Specified by:
read in interface ObjectInput
Overrides:
read in class ObjectInputStream
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)
         throws IOException
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

The read(b) method for class InputStream has the same effect as:

 read(b, 0, b.length) 

Specified by:
read in interface ObjectInput
Overrides:
read in class InputStream
Parameters:
buf - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
See Also:
InputStream.read(byte[], int, int)

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.

Specified by:
read in interface ObjectInput
Overrides:
read in class ObjectInputStream
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)

available

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

Specified by:
available in interface ObjectInput
Overrides:
available in class ObjectInputStream
Returns:
the number of available bytes.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream

readBoolean

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

Specified by:
readBoolean in interface DataInput
Overrides:
readBoolean in class ObjectInputStream
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.

Specified by:
readByte in interface DataInput
Overrides:
readByte in class ObjectInputStream
Returns:
the 8 bit byte read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readUnsignedByte

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

Specified by:
readUnsignedByte in interface DataInput
Overrides:
readUnsignedByte in class ObjectInputStream
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.

Specified by:
readChar in interface DataInput
Overrides:
readChar in class ObjectInputStream
Returns:
the 16 bit char read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readShort

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

Specified by:
readShort in interface DataInput
Overrides:
readShort in class ObjectInputStream
Returns:
the 16 bit short 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.

Specified by:
readUnsignedShort in interface DataInput
Overrides:
readUnsignedShort in class ObjectInputStream
Returns:
the 16 bit short 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.

Specified by:
readInt in interface DataInput
Overrides:
readInt in class ObjectInputStream
Returns:
the 32 bit integer read.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readLong

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

Specified by:
readLong in interface DataInput
Overrides:
readLong in class ObjectInputStream
Returns:
the read 64 bit long.
Throws:
EOFException - If end of file is reached.
IOException - If other I/O error has occurred.

readFloat

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

Specified by:
readFloat in interface DataInput
Overrides:
readFloat in class ObjectInputStream
Returns:
the 32 bit float 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.

Specified by:
readDouble in interface DataInput
Overrides:
readDouble in class ObjectInputStream
Returns:
the 64 bit double 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.

Specified by:
readFully in interface DataInput
Overrides:
readFully in class ObjectInputStream
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.

Specified by:
readFully in interface DataInput
Overrides:
readFully in class ObjectInputStream
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.

skipBytes

public int skipBytes(int len)
              throws IOException
Skips bytes.

Specified by:
skipBytes in interface DataInput
Overrides:
skipBytes in class ObjectInputStream
Parameters:
len - the number of bytes to be skipped
Returns:
the actual number of bytes skipped.
Throws:
IOException - If an I/O error has occurred.

readLine

@Deprecated
public String readLine()
                throws IOException
Deprecated. 

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

Specified by:
readLine in interface DataInput
Overrides:
readLine in class ObjectInputStream
Returns:
a String copy of the line.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream

readUTF

public String readUTF()
               throws IOException
Reads a String in modified UTF-8 format.

Specified by:
readUTF in interface DataInput
Overrides:
readUTF in class ObjectInputStream
Returns:
the String.
Throws:
IOException - if there are I/O errors while reading from the underlying InputStream
UTFDataFormatException - if read bytes do not represent a valid modified UTF-8 encoding of a string

skip

public long skip(long n)
          throws IOException
Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

The skip method of this class creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. For instance, the implementation may depend on the ability to seek.

Specified by:
skip in interface ObjectInput
Overrides:
skip in class InputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if the stream does not support seek, or if some other I/O error occurs.

mark

public final void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.

Marking a closed stream should not have any effect on the stream.

The mark method of InputStream does nothing.

Overrides:
mark in class InputStream
Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
See Also:
InputStream.reset()

reset

public final void reset()
                 throws IOException
Repositions this stream to the position at the time the mark method was last called on this input stream.

The general contract of reset is:

The method reset for class InputStream does nothing except throw an IOException.

Overrides:
reset in class InputStream
Throws:
IOException - if this stream has not been marked or if the mark has been invalidated.
See Also:
InputStream.mark(int), IOException

markSupported

public final boolean markSupported()
Tests if this input stream supports the mark and reset methods. Whether or not mark and reset are supported is an invariant property of a particular input stream instance. The markSupported method of InputStream returns false.

Overrides:
markSupported in class InputStream
Returns:
true if this stream instance supports the mark and reset methods; false otherwise.
See Also:
InputStream.mark(int), InputStream.reset()

close

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

Specified by:
close in interface Closeable
Specified by:
close in interface ObjectInput
Overrides:
close in class ObjectInputStream
Throws:
IllegalStateException

resolveClass

protected final Class<?> resolveClass(ObjectStreamClass desc)
                               throws IllegalStateException
Load the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source.

The corresponding method in ObjectOutputStream is annotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return a Class object. Once returned, if the class is not an array class, its serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and an InvalidClassException is thrown.

The default implementation of this method in ObjectInputStream returns the result of calling

     Class.forName(desc.getName(), false, loader)
 
where loader is determined as follows: if there is a method on the current thread's stack whose declaring class was defined by a user-defined class loader (and was not a generated to implement reflective invocations), then loader is class loader corresponding to the closest such method to the currently executing frame; otherwise, loader is null. If this call results in a ClassNotFoundException and the name of the passed ObjectStreamClass instance is the Java language keyword for a primitive type or void, then the Class object representing that primitive type or void will be returned (e.g., an ObjectStreamClass with the name "int" will be resolved to Integer.TYPE). Otherwise, the ClassNotFoundException will be thrown to the caller of this method.

Overrides:
resolveClass in class ObjectInputStream
Parameters:
desc - an instance of class ObjectStreamClass
Returns:
a Class object corresponding to desc
Throws:
IllegalStateException

resolveProxyClass

protected final Class<?> resolveProxyClass(String[] interfaces)
                                    throws IllegalStateException
Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.

This method is called exactly once for each unique proxy class descriptor in the stream.

The corresponding method in ObjectOutputStream is annotateProxyClass. For a given subclass of ObjectInputStream that overrides this method, the annotateProxyClass method in the corresponding subclass of ObjectOutputStream must write any data or objects read by this method.

The default implementation of this method in ObjectInputStream returns the result of calling Proxy.getProxyClass with the list of Class objects for the interfaces that are named in the interfaces parameter. The Class object for each interface name i is the value returned by calling

     Class.forName(i, false, loader)
 
where loader is that of the first non-null class loader up the execution stack, or null if no non-null class loaders are on the stack (the same class loader choice used by the resolveClass method). Unless any of the resolved interfaces are non-public, this same value of loader is also the class loader passed to Proxy.getProxyClass; if non-public interfaces are present, their class loader is passed instead (if more than one non-public interface class loader is encountered, an IllegalAccessError is thrown). If Proxy.getProxyClass throws an IllegalArgumentException, resolveProxyClass will throw a ClassNotFoundException containing the IllegalArgumentException.

Overrides:
resolveProxyClass in class ObjectInputStream
Parameters:
interfaces - the list of interface names that were deserialized in the proxy class descriptor
Returns:
a proxy class for the specified interfaces
Throws:
IllegalStateException
See Also:
ObjectOutputStream.annotateProxyClass(Class)

resolveObject

protected final Object resolveObject(Object obj)
                              throws IllegalStateException
This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization. Replacing objects is disabled until enableResolveObject is called. The enableResolveObject method checks that the stream requesting to resolve object can be trusted. Every reference to serializable objects is passed to resolveObject. To insure that the private state of objects is not unintentionally exposed only trusted streams may use resolveObject.

This method is called after an object has been read but before it is returned from readObject. The default resolveObject method just returns the same object.

When a subclass is replacing objects it must insure that the substituted object is compatible with every field where the reference will be stored. Objects whose type is not a subclass of the type of the field or array element abort the serialization by raising an exception and the object is not be stored.

This method is called only once when each object is first encountered. All subsequent references to the object will be redirected to the new object.

Overrides:
resolveObject in class ObjectInputStream
Parameters:
obj - object to be substituted
Returns:
the substituted object
Throws:
IllegalStateException

enableResolveObject

protected final boolean enableResolveObject(boolean enable)
                                     throws IllegalStateException
Enable the stream to allow objects read from the stream to be replaced. When enabled, the resolveObject method is called for every object being deserialized.

If enable is true, and there is a security manager installed, this method first calls the security manager's checkPermission method with the SerializablePermission("enableSubstitution") permission to ensure it's ok to enable the stream to allow objects read from the stream to be replaced.

Overrides:
enableResolveObject in class ObjectInputStream
Parameters:
enable - true for enabling use of resolveObject for every object being deserialized
Returns:
the previous setting before this method was invoked
Throws:
IllegalStateException
See Also:
SecurityManager.checkPermission(java.security.Permission), SerializablePermission

readStreamHeader

protected final void readStreamHeader()
                               throws IllegalStateException
The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers. It reads and verifies the magic number and version number.

Overrides:
readStreamHeader in class ObjectInputStream
Throws:
IllegalStateException

readClassDescriptor

protected final ObjectStreamClass readClassDescriptor()
                                               throws IllegalStateException
Read a class descriptor from the serialization stream. This method is called when the ObjectInputStream expects a class descriptor as the next item in the serialization stream. Subclasses of ObjectInputStream may override this method to read in class descriptors that have been written in non-standard formats (by subclasses of ObjectOutputStream which have overridden the writeClassDescriptor method). By default, this method reads class descriptors according to the format defined in the Object Serialization specification.

Overrides:
readClassDescriptor in class ObjectInputStream
Returns:
the class descriptor read
Throws:
IllegalStateException
See Also:
ObjectOutputStream.writeClassDescriptor(java.io.ObjectStreamClass)

defaultReadObject

public abstract 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.

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

readFields

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

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

registerValidation

public abstract 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.

Overrides:
registerValidation in class ObjectInputStream
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.

Marshalling API version 1.0.0.GA

Copyright © 2008 JBoss, a division of Red Hat, Inc.