Compartir a través de


ObjectInputStream Class

Definition

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

[Android.Runtime.Register("java/io/ObjectInputStream", DoNotGenerateAcw=true)]
public class ObjectInputStream : Java.IO.InputStream, IDisposable, Java.Interop.IJavaPeerable, Java.IO.IObjectInput
[<Android.Runtime.Register("java/io/ObjectInputStream", DoNotGenerateAcw=true)>]
type ObjectInputStream = class
    inherit InputStream
    interface IObjectInput
    interface IDataInput
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
ObjectInputStream
Attributes
Implements

Remarks

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. ObjectInputStream 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.

ObjectInputStream 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 example in ObjectOutputStream: <br>

FileInputStream fis = new FileInputStream("t.tmp");
                 ObjectInputStream ois = new ObjectInputStream(fis);

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

                 ois.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;
            private void readObjectNoData()
                throws ObjectStreamException;

The readObject method is responsible for reading and restoring the state of the object for its particular class using 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 ObjectInputStream 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. Note that this behavior does not hold for streams written with the old ObjectStreamConstants.PROTOCOL_VERSION_1 protocol, in which the end of data written by writeExternal methods is not demarcated, and hence cannot be detected.

The readObjectNoData method is responsible for initializing the state of the object for its particular class in the event that the serialization stream does not list the given class as a superclass of the object being deserialized. This may occur in cases where the receiving party uses a different version of the deserialized instance's class than the sending party, and the receiver's version extends classes that are not extended by the sender's version. This may also occur if the serialization stream has been tampered; hence, readObjectNoData is useful for initializing deserialized objects properly despite a "hostile" or incomplete source stream.

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 ObjectInputStream 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.

Enum constants are deserialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not transmitted. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the static method Enum.valueOf(Class, String) with the enum constant's base type and the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream. The process by which enum constants are deserialized cannot be customized: any class-specific readObject, readObjectNoData, and readResolve methods defined by enum types are ignored during deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L.

Added in JDK1.1.

Java documentation for java.io.ObjectInputStream.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ObjectInputStream()

Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream.

ObjectInputStream(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

ObjectInputStream(Stream)

Creates an ObjectInputStream that reads from the specified InputStream.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Available()

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

(Inherited from InputStream)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this input stream and releases any system resources associated with the stream.

(Inherited from InputStream)
DefaultReadObject()

Read the non-static and non-transient fields of the current class from this stream.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
EnableResolveObject(Boolean)

Enable the stream to allow objects read from the stream to be replaced.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Mark(Int32)

Marks the current position in this input stream.

(Inherited from InputStream)
MarkSupported()

Tests if this input stream supports the mark and reset methods.

(Inherited from InputStream)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Read()

Reads a byte of data.

Read(Byte[], Int32, Int32)

Reads up to len bytes of data from the input stream into an array of bytes.

(Inherited from InputStream)
Read(Byte[])

Reads some number of bytes from the input stream and stores them into the buffer array b.

(Inherited from InputStream)
ReadAllBytes()

Reads all remaining bytes from the input stream.

(Inherited from InputStream)
ReadAsync() (Inherited from InputStream)
ReadAsync(Byte[], Int32, Int32) (Inherited from InputStream)
ReadAsync(Byte[]) (Inherited from InputStream)
ReadBoolean()

Reads in a boolean.

ReadByte()

Reads an 8 bit byte.

ReadChar()

Reads a 16 bit char.

ReadClassDescriptor()

Read a class descriptor from the serialization stream.

ReadDouble()

Reads a 64 bit double.

ReadFields()

Reads the persistent fields from the stream and makes them available by name.

ReadFloat()

Reads a 32 bit float.

ReadFully(Byte[], Int32, Int32)

Reads bytes, blocking until all bytes are read.

ReadFully(Byte[])

Reads bytes, blocking until all bytes are read.

ReadInt()

Reads a 32 bit int.

ReadLine()
Obsolete.

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

ReadLong()

Reads a 64 bit long.

ReadNBytes(Byte[], Int32, Int32)

Reads the requested number of bytes from the input stream into the given byte array.

(Inherited from InputStream)
ReadNBytes(Int32)

Reads up to a specified number of bytes from the input stream.

(Inherited from InputStream)
ReadObject()

Read an object from the ObjectInputStream.

ReadObjectOverride()

This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor.

ReadShort()

Reads a 16 bit short.

ReadStreamHeader()

The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers.

ReadUnshared()

Reads an "unshared" object from the ObjectInputStream.

ReadUnsignedByte()

Reads an unsigned 8 bit byte.

ReadUnsignedShort()

Reads an unsigned 16 bit short.

ReadUTF()

Reads a String in modified UTF-8 format.

RegisterValidation(IObjectInputValidation, Int32)

Register an object to be validated before the graph is returned.

Reset()

Repositions this stream to the position at the time the mark method was last called on this input stream.

(Inherited from InputStream)
ResolveClass(ObjectStreamClass)

Load the local class equivalent of the specified stream class description.

ResolveObject(Object)

This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization.

ResolveProxyClass(String[])

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.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Skip(Int64)

Skips over and discards n bytes of data from this input stream.

(Inherited from InputStream)
SkipAsync(Int64) (Inherited from InputStream)
SkipBytes(Int32)

Skips bytes.

SkipNBytes(Int64)

Skips over and discards exactly n bytes of data from this input stream.

(Inherited from InputStream)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
TransferTo(Stream)

Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.

(Inherited from InputStream)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
ReadBooleanAsync(IDataInput)
ReadByteAsync(IDataInput)
ReadCharAsync(IDataInput)
ReadDoubleAsync(IDataInput)
ReadFloatAsync(IDataInput)
ReadFullyAsync(IDataInput, Byte[], Int32, Int32)
ReadFullyAsync(IDataInput, Byte[])
ReadIntAsync(IDataInput)
ReadLineAsync(IDataInput)
ReadLongAsync(IDataInput)
ReadShortAsync(IDataInput)
ReadUnsignedByteAsync(IDataInput)
ReadUnsignedShortAsync(IDataInput)
ReadUTFAsync(IDataInput)
SkipBytesAsync(IDataInput, Int32)
ReadAsync(IObjectInput, Byte[], Int32, Int32)
ReadAsync(IObjectInput, Byte[])
ReadAsync(IObjectInput)
ReadObjectAsync(IObjectInput)
SkipAsync(IObjectInput, Int64)

Applies to