Added in API level 1

ObjectInputStream

open class ObjectInputStream : InputStream, ObjectInput, ObjectStreamConstants

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:

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.

Summary

Nested classes
abstract

Provide access to the persistent fields read from the input stream.

Inherited constants
Int PROTOCOL_VERSION_1

A Stream Protocol Version.

All externalizable data is written in JDK 1.1 external data format after calling this method. This version is needed to write streams containing Externalizable data that can be read by pre-JDK 1.1.6 JVMs.

Int PROTOCOL_VERSION_2

A Stream Protocol Version.

This protocol is written by JVM 1.2. Externalizable data is written in block data mode and is terminated with TC_ENDBLOCKDATA. Externalizable class descriptor flags has SC_BLOCK_DATA enabled. JVM 1.1.6 and greater can read this format change. Enables writing a nonSerializable class descriptor into the stream. The serialVersionUID of a nonSerializable class is set to 0L.

Byte SC_BLOCK_DATA

Bit mask for ObjectStreamClass flag. Indicates Externalizable data written in Block Data mode. Added for PROTOCOL_VERSION_2.

Byte SC_ENUM

Bit mask for ObjectStreamClass flag. Indicates class is an enum type.

Byte SC_EXTERNALIZABLE

Bit mask for ObjectStreamClass flag. Indicates class is Externalizable.

Byte SC_SERIALIZABLE

Bit mask for ObjectStreamClass flag. Indicates class is Serializable.

Byte SC_WRITE_METHOD

Bit mask for ObjectStreamClass flag. Indicates a Serializable class defines its own writeObject method.

Short STREAM_MAGIC

Magic number that is written to the stream header.

Short STREAM_VERSION

Version number that is written to the stream header.

Byte TC_ARRAY

new Array.

Byte TC_BASE

First tag value.

Byte TC_BLOCKDATA

Block of optional data. Byte following tag indicates number of bytes in this block data.

Byte TC_BLOCKDATALONG

long Block data. The long following the tag indicates the number of bytes in this block data.

Byte TC_CLASS

Reference to Class.

Byte TC_CLASSDESC

new Class Descriptor.

Byte TC_ENDBLOCKDATA

End of optional block data blocks for an object.

Byte TC_ENUM

new Enum constant.

Byte TC_EXCEPTION

Exception during write.

Byte TC_LONGSTRING

Long string.

Byte TC_MAX

Last tag value.

Byte TC_NULL

Null object reference.

Byte TC_OBJECT

new Object.

Byte TC_PROXYCLASSDESC

new Proxy Class Descriptor.

Byte TC_REFERENCE

Reference to an object already written into the stream.

Byte TC_RESET

Reset stream context. All handles written into stream are reset.

Byte TC_STRING

new String.

Int baseWireHandle

First wire handle to be assigned.

Public constructors

Creates an ObjectInputStream that reads from the specified InputStream.

Protected constructors

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

Public methods
open Int

Returns the number of bytes that can be read without blocking.

open Unit

Closes the input stream.

open Unit

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

open Int

Reads a byte of data.

open Int
read(buf: ByteArray!, off: Int, len: Int)

Reads into an array of bytes.

open Boolean

Reads in a boolean.

open Byte

Reads an 8 bit byte.

open Char

Reads a 16 bit char.

open Double

Reads a 64 bit double.

open ObjectInputStream.GetField!

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

open Float

Reads a 32 bit float.

open Unit

Reads bytes, blocking until all bytes are read.

open Unit
readFully(buf: ByteArray!, off: Int, len: Int)

Reads bytes, blocking until all bytes are read.

open Int

Reads a 32 bit int.

open String!

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

open Long

Reads a 64 bit long.

Any!

Read an object from the ObjectInputStream.

open Short

Reads a 16 bit short.

open String!

Reads a String in modified UTF-8 format.

open Any!

Reads an "unshared" object from the ObjectInputStream.

open Int

Reads an unsigned 8 bit byte.

open Int

Reads an unsigned 16 bit short.

open Unit

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

open Int
skipBytes(len: Int)

Skips bytes.

Protected methods
open Boolean

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

open ObjectStreamClass!

Read a class descriptor from the serialization stream.

open Any!

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

open Unit

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

open Class<*>!

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

open Any!

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

open Class<*>!
resolveProxyClass(interfaces: Array<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.

Inherited functions
Unit mark(readlimit: Int)

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.

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.

InputStream! nullInputStream()

Returns a new InputStream that reads no bytes. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect.

While the stream is open, the available(), read(), read(byte[]), read(byte[], int, int), readAllBytes(), readNBytes(byte[], int, int), readNBytes(int), skip(long), skipNBytes(long), and transferTo() methods all behave as if end of stream has been reached. After the stream has been closed, these methods all throw IOException.

The markSupported() method returns false. The mark() method does nothing, and the reset() method throws IOException.

Int read(b: ByteArray!)

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:

<code>read(b, 0, b.length) </code>

ByteArray! readAllBytes()

Reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and end of stream is detected, or an exception is thrown. This method does not close the input stream.

When this stream reaches end of stream, further invocations of this method will return an empty byte array.

Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading input streams with large amounts of data.

The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.

If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

Int readNBytes(b: ByteArray!, off: Int, len: Int)

Reads the requested number of bytes from the input stream into the given byte array. This method blocks until len bytes of input data have been read, end of stream is detected, or an exception is thrown. The number of bytes actually read, possibly zero, is returned. This method does not close the input stream.

In the case where end of stream is reached before len bytes have been read, then the actual number of bytes read will be returned. When this stream reaches end of stream, further invocations of this method will return zero.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read up to len bytes.

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

The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.

If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes of b have been updated with data from the input stream. Consequently the input stream and b may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

ByteArray! readNBytes(len: Int)

Reads up to a specified number of bytes from the input stream. This method blocks until the requested number of bytes has been read, end of stream is detected, or an exception is thrown. This method does not close the input stream.

The length of the returned array equals the number of bytes read from the stream. If len is zero, then no bytes are read and an empty byte array is returned. Otherwise, up to len bytes are read from the stream. Fewer than len bytes may be read if end of stream is encountered.

When this stream reaches end of stream, further invocations of this method will return an empty byte array.

Note that this method is intended for simple cases where it is convenient to read the specified number of bytes into a byte array. The total amount of memory allocated by this method is proportional to the number of bytes read from the stream which is bounded by len. Therefore, the method may be safely called with very large values of len provided sufficient memory is available.

The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.

If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

Unit reset()

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:

  • If the method markSupported returns true, then:
    • If the method mark has not been called since the stream was created, or the number of bytes read from the stream since mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.
    • If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to mark (or since the start of the file, if mark has not been called) will be resupplied to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of the time of the call to reset.
  • If the method markSupported returns false, then:
    • The call to reset may throw an IOException.
    • If an IOException is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of the read method depend on the particular type of the input stream.

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

Long skip(n: Long)

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, the skip method for class InputStream always returns 0, and no bytes are skipped. Subclasses may handle the negative value differently.

The skip method implementation 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.

Unit skipNBytes(n: Long)

Skips over and discards exactly n bytes of data from this input stream. If n is zero, then no bytes are skipped. If n is negative, then no bytes are skipped. Subclasses may handle the negative value differently.

This method blocks until the requested number of bytes has been skipped, end of file is reached, or an exception is thrown.

If end of stream is reached before the stream is at the desired position, then an EOFException is thrown.

If an I/O error occurs, then the input stream may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

Long transferTo(out: OutputStream!)

Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read. On return, this input stream will be at end of stream. This method does not close either stream.

This method may block indefinitely reading from the input stream, or writing to the output stream. The behavior for the case where the input and/or output stream is asynchronously closed, or the thread interrupted during the transfer, is highly input and output stream specific, and therefore not specified.

If an I/O error occurs reading from the input stream or writing to the output stream, then it may do so after some bytes have been read or written. Consequently the input stream may not be at end of stream and one, or both, streams may be in an inconsistent state. It is strongly recommended that both streams be promptly closed if an I/O error occurs.

Inherited properties
SerializablePermission! SUBCLASS_IMPLEMENTATION_PERMISSION

Enable overriding of readObject and writeObject.

SerializablePermission! SUBSTITUTION_PERMISSION

Enable substitution of one object for another during serialization/deserialization.

Public constructors

ObjectInputStream

Added in API level 1
ObjectInputStream(in: InputStream!)

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 InputStream!: input stream to read from
Exceptions
java.io.StreamCorruptedException if the stream header is incorrect
java.io.IOException if an I/O error occurs while reading stream header
java.lang.SecurityException if untrusted subclass illegally overrides security-sensitive methods
java.lang.NullPointerException if in is null

Protected constructors

ObjectInputStream

Added in API level 1
protected 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.

If there is a security manager installed, this method first calls the security manager's checkPermission method with the SerializablePermission("enableSubclassImplementation") permission to ensure it's ok to enable subclassing.

Exceptions
java.lang.SecurityException if a security manager exists and its checkPermission method denies enabling subclassing.
java.io.IOException if an I/O error occurs while creating this stream

Public methods

available

Added in API level 1
open fun available(): Int

Returns the number of bytes that can be read without blocking.

Return
Int the number of available bytes.
Exceptions
java.io.IOException if there are I/O errors while reading from the underlying InputStream

close

Added in API level 1
open fun close(): Unit

Closes the input stream. Must be called to release any resources associated with the stream.

Exceptions
java.lang.Exception if this resource cannot be closed
java.io.IOException If an I/O error has occurred.

defaultReadObject

Added in API level 1
open fun defaultReadObject(): Unit

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.

Exceptions
java.lang.ClassNotFoundException if the class of a serialized object could not be found.
java.io.IOException if an I/O error occurs.
java.io.NotActiveException if the stream is not currently reading objects.

read

Added in API level 1
open fun read(): Int

Reads a byte of data. This method will block if no input is available.

Return
Int the byte read, or -1 if the end of the stream is reached.
Exceptions
java.io.IOException If an I/O error has occurred.

read

Added in API level 1
open fun read(
    buf: ByteArray!,
    off: Int,
    len: Int
): Int

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
b the buffer into which the data is read
off Int: the start offset of the data
len Int: the maximum number of bytes read
buf ByteArray!: the buffer into which the data is read
Return
Int the actual number of bytes read, -1 is returned when the end of the stream is reached.
Exceptions
java.io.IOException If an I/O error has occurred.
java.lang.NullPointerException If b is null.
java.lang.IndexOutOfBoundsException If off is negative, len is negative, or len is greater than b.length - off

readBoolean

Added in API level 1
open fun readBoolean(): Boolean

Reads in a boolean.

Return
Boolean the boolean read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readByte

Added in API level 1
open fun readByte(): Byte

Reads an 8 bit byte.

Return
Byte the 8 bit byte read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readChar

Added in API level 1
open fun readChar(): Char

Reads a 16 bit char.

Return
Char the 16 bit char read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readDouble

Added in API level 1
open fun readDouble(): Double

Reads a 64 bit double.

Return
Double the 64 bit double read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readFields

Added in API level 1
open fun readFields(): ObjectInputStream.GetField!

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

Return
ObjectInputStream.GetField! the GetField object representing the persistent fields of the object being deserialized
Exceptions
java.lang.ClassNotFoundException if the class of a serialized object could not be found.
java.io.IOException if an I/O error occurs.
java.io.NotActiveException if the stream is not currently reading objects.

readFloat

Added in API level 1
open fun readFloat(): Float

Reads a 32 bit float.

Return
Float the 32 bit float read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readFully

Added in API level 1
open fun readFully(buf: ByteArray!): Unit

Reads bytes, blocking until all bytes are read.

Parameters
b the buffer into which the data is read.
buf ByteArray!: the buffer into which the data is read
Exceptions
java.lang.NullPointerException if b is null.
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readFully

Added in API level 1
open fun readFully(
    buf: ByteArray!,
    off: Int,
    len: Int
): Unit

Reads bytes, blocking until all bytes are read.

Parameters
b the buffer into which the data is read.
off Int: the start offset of the data
len Int: the maximum number of bytes to read
buf ByteArray!: the buffer into which the data is read
Exceptions
java.lang.NullPointerException if b is null.
java.lang.IndexOutOfBoundsException if off is negative, len is negative, or len is greater than b.length - off.
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readInt

Added in API level 1
open fun readInt(): Int

Reads a 32 bit int.

Return
Int the 32 bit integer read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readLine

Added in API level 1
open fun readLine(): String!

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.

Return
String! a String copy of the line.
Exceptions
java.io.IOException if there are I/O errors while reading from the underlying InputStream

readLong

Added in API level 1
open fun readLong(): Long

Reads a 64 bit long.

Return
Long the read 64 bit long.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readObject

Added in API level 1
fun readObject(): Any!

Read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read. Default deserializing for a class can be overridden using the writeObject and readObject methods. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.

The root object is completely restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored.

Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

Return
Any! the object read from the stream
Exceptions
java.lang.ClassNotFoundException Class of a serialized object cannot be found.
java.io.InvalidClassException Something is wrong with a class used by serialization.
java.io.StreamCorruptedException Control information in the stream is inconsistent.
java.io.OptionalDataException Primitive data was found in the stream instead of objects.
java.io.IOException Any of the usual Input/Output related exceptions.

readShort

Added in API level 1
open fun readShort(): Short

Reads a 16 bit short.

Return
Short the 16 bit short read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readUTF

Added in API level 1
open fun readUTF(): String!

Reads a String in modified UTF-8 format.

Return
String! the String.
Exceptions
java.io.IOException if there are I/O errors while reading from the underlying InputStream
java.io.UTFDataFormatException if read bytes do not represent a valid modified UTF-8 encoding of a string

readUnshared

Added in API level 1
open fun readUnshared(): Any!

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:

  • If readUnshared is called to deserialize a back-reference (the stream representation of an object which has been written previously to the stream), an ObjectStreamException will be thrown.
  • If readUnshared returns successfully, then any subsequent attempts to deserialize back-references to the stream handle deserialized by readUnshared will cause an ObjectStreamException to be thrown.
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.

Return
Any! reference to deserialized object
Exceptions
java.lang.ClassNotFoundException if class of an object to deserialize cannot be found
java.io.StreamCorruptedException if control information in the stream is inconsistent
java.io.ObjectStreamException if object to deserialize has already appeared in stream
java.io.OptionalDataException if primitive data is next in stream
java.io.IOException if an I/O error occurs during deserialization

readUnsignedByte

Added in API level 1
open fun readUnsignedByte(): Int

Reads an unsigned 8 bit byte.

Return
Int the 8 bit byte read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

readUnsignedShort

Added in API level 1
open fun readUnsignedShort(): Int

Reads an unsigned 16 bit short.

Return
Int the 16 bit short read.
Exceptions
java.io.EOFException If end of file is reached.
java.io.IOException If other I/O error has occurred.

registerValidation

Added in API level 1
open fun registerValidation(
    obj: ObjectInputValidation!,
    prio: Int
): Unit

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 ObjectInputValidation!: the object to receive the validation callback.
prio Int: 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.
Exceptions
java.io.NotActiveException The stream is not currently reading objects so it is invalid to register a callback.
java.io.InvalidObjectException The validation object is null.

skipBytes

Added in API level 1
open fun skipBytes(len: Int): Int

Skips bytes.

Parameters
n the number of bytes to be skipped.
len Int: the number of bytes to be skipped
Return
Int the actual number of bytes skipped.
Exceptions
java.io.IOException If an I/O error has occurred.

Protected methods

enableResolveObject

Added in API level 1
protected open fun enableResolveObject(enable: Boolean): Boolean

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.

Parameters
enable Boolean: true for enabling use of resolveObject for every object being deserialized
Return
Boolean the previous setting before this method was invoked
Exceptions
java.lang.SecurityException if a security manager exists and its checkPermission method denies enabling the stream to allow objects read from the stream to be replaced.

readClassDescriptor

Added in API level 1
protected open fun readClassDescriptor(): ObjectStreamClass!

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.

Return
ObjectStreamClass! the class descriptor read
Exceptions
java.io.IOException If an I/O error has occurred.
java.lang.ClassNotFoundException If the Class of a serialized object used in the class descriptor representation cannot be found

readObjectOverride

Added in API level 1
protected open fun readObjectOverride(): Any!

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

Return
Any! the Object read from the stream.
Exceptions
java.lang.ClassNotFoundException Class definition of a serialized object cannot be found.
java.io.OptionalDataException Primitive data was found in the stream instead of objects.
java.io.IOException if I/O errors occurred while reading from the underlying stream

readStreamHeader

Added in API level 1
protected open fun readStreamHeader(): Unit

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.

Exceptions
java.io.IOException if there are I/O errors while reading from the underlying InputStream
java.io.StreamCorruptedException if control information in the stream is inconsistent

resolveClass

Added in API level 1
protected open fun resolveClass(desc: ObjectStreamClass!): Class<*>!

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.
Parameters
desc ObjectStreamClass!: an instance of class ObjectStreamClass
Return
Class<*>! a Class object corresponding to desc
Exceptions
java.io.IOException any of the usual Input/Output exceptions.
java.lang.ClassNotFoundException if class of a serialized object cannot be found.

resolveObject

Added in API level 1
protected open fun resolveObject(obj: Any!): Any!

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.

Parameters
obj Any!: object to be substituted
Return
Any! the substituted object
Exceptions
java.io.IOException Any of the usual Input/Output exceptions.

resolveProxyClass

Added in API level 1
protected open fun resolveProxyClass(interfaces: Array<String!>!): Class<*>!

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.
Parameters
interfaces Array<String!>!: the list of interface names that were deserialized in the proxy class descriptor
Return
Class<*>! a proxy class for the specified interfaces
Exceptions
java.io.IOException any exception thrown by the underlying InputStream
java.lang.ClassNotFoundException if the proxy class or any of the named interfaces could not be found