public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
Modifier and Type | Class and Description |
---|---|
static class |
ObjectInputStream.GetField
This class allows a class to specify exactly which fields should
be read, and what values should be read for these fields.
|
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
Modifier | Constructor and Description |
---|---|
protected |
ObjectInputStream()
Protected constructor that allows subclasses to override
deserialization.
|
|
ObjectInputStream(InputStream in)
Creates a new
ObjectInputStream that will do all of
its reading from in . |
Modifier and Type | Method and Description |
---|---|
int |
available()
This method returns the number of bytes that can be read from this
stream before a read can block.
|
void |
close()
This method closes the stream.
|
void |
defaultReadObject()
Reads the current objects non-transient, non-static fields from
the current class from the underlying output stream.
|
protected boolean |
enableResolveObject(boolean enable)
If
enable is true and this object is
trusted, then resolveObject (Object) will be called
in subsequent calls to readObject (Object) . |
int |
read()
This method reads an unsigned byte from the input stream and returns it
as an int in the range of 0-255.
|
int |
read(byte[] data,
int offset,
int length)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
boolean |
readBoolean()
This method reads a Java boolean value from an input stream.
|
byte |
readByte()
This method reads a Java byte value from an input stream.
|
char |
readChar()
This method reads a Java
char value from an input stream. |
protected ObjectStreamClass |
readClassDescriptor()
This method reads a class descriptor from the real input stream
and use these data to create a new instance of ObjectStreamClass.
|
double |
readDouble()
This method reads a Java double value from an input stream.
|
ObjectInputStream.GetField |
readFields()
This method should be called by a method called 'readObject' in the
deserializing class (if present).
|
float |
readFloat()
This method reads a Java float value from an input stream.
|
void |
readFully(byte[] data)
This method reads raw bytes into the passed array until the array is
full.
|
void |
readFully(byte[] data,
int offset,
int size)
This method reads raw bytes into the passed array
buf
starting
offset bytes into the buffer. |
int |
readInt()
This method reads a Java
int value from an input stream
It operates by reading four bytes from the stream and converting them to
a single Java int . |
String |
readLine()
Deprecated.
|
long |
readLong()
This method reads a Java
long value from an input stream
It operates by reading eight bytes from the stream and converting them to
a single Java long . |
Object |
readObject()
Returns the next deserialized object read from the underlying stream.
|
protected Object |
readObjectOverride()
This method allows subclasses to override the default
de serialization mechanism provided by
ObjectInputStream . |
short |
readShort()
This method reads a signed 16-bit value into a Java in from the stream.
|
protected void |
readStreamHeader()
Reads stream magic and stream version information from the
underlying stream.
|
Object |
readUnshared()
Returns the next deserialized object read from the
underlying stream in an unshared manner.
|
int |
readUnsignedByte()
This method reads 8 unsigned bits into a Java
int value from
the stream. |
int |
readUnsignedShort()
This method reads 16 unsigned bits into a Java int value from the stream.
|
String |
readUTF()
This method reads a
String from an input stream that is
encoded in a modified UTF-8 format. |
void |
registerValidation(ObjectInputValidation validator,
int priority)
Registers a
ObjectInputValidation to be carried out
on the object graph currently being deserialized before it is
returned to the original caller of readObject () . |
protected Class<?> |
resolveClass(ObjectStreamClass osc)
Called when a class is being deserialized.
|
protected Object |
resolveObject(Object obj)
Allows subclasses to resolve objects that are read from the
stream with other objects to be returned in their place.
|
protected Class<?> |
resolveProxyClass(String[] intfs) |
int |
skipBytes(int len)
This method skips and discards the specified number of bytes in an
input stream.
|
mark, markSupported, read, reset, skip
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
read, skip
public ObjectInputStream(InputStream in) throws IOException, StreamCorruptedException
ObjectInputStream
that will do all of
its reading from in
. This method also checks
the stream by reading the header information (stream magic number
and stream version).IOException
- Reading stream header from underlying
stream cannot be completed.StreamCorruptedException
- An invalid stream magic
number or stream version was read from the stream.readStreamHeader()
protected ObjectInputStream() throws IOException, SecurityException
readObject (Object)
. This
method does a security check NOTE: currently not
implemented, then sets a flag that informs
readObject (Object)
to call the subclasses
readObjectOverride (Object)
method.IOException
SecurityException
readObjectOverride()
public final Object readObject() throws ClassNotFoundException, IOException
private void readObject (ObjectInputStream)
.
If an exception is thrown from this method, the stream is left in
an undefined state. This method can also throw Errors and
RuntimeExceptions if caused by existing readResolve() user code.readObject
in interface ObjectInput
ClassNotFoundException
- The class that an object being
read in belongs to cannot be found.IOException
- Exception from underlying
InputStream
.public Object readUnshared() throws IOException, ClassNotFoundException
Returns the next deserialized object read from the
underlying stream in an unshared manner. Any object
returned by this method will not be returned by
subsequent calls to either this method or readObject()
.
This behaviour is achieved by:
readObject()
or
readUnshared()
will throw an ObjectStreamException
rather than returning the same object reference.ObjectStreamException
if the next
element in the stream is a reference to an earlier object.ClassNotFoundException
- if the class of the object being
deserialized can not be found.StreamCorruptedException
- if information in the stream
is inconsistent.ObjectStreamException
- if the next object has already been
returned by an earlier call to this
method or readObject()
.OptionalDataException
- if primitive data occurs next in the stream.IOException
- if an I/O error occurs from the stream.readObject()
protected ObjectStreamClass readClassDescriptor() throws ClassNotFoundException, IOException
ClassNotFoundException
- if the required class to build the
descriptor has not been found in the system.IOException
- An input/output error occured.InvalidClassException
- If there was a compatibility problem
between the class present in the system and the serialized class.public void defaultReadObject() throws ClassNotFoundException, IOException, NotActiveException
private void readObject (ObjectInputStream)
method.ClassNotFoundException
- The class that an object being
read in belongs to cannot be found.NotActiveException
- This method was called from a
context other than from the current object's and current class's
private void readObject (ObjectInputStream)
method.IOException
- Exception from underlying
OutputStream
.public void registerValidation(ObjectInputValidation validator, int priority) throws InvalidObjectException, NotActiveException
ObjectInputValidation
to be carried out
on the object graph currently being deserialized before it is
returned to the original caller of readObject ()
.
The order of validation for multiple
ObjectInputValidation
s can be controled using
priority
. Validators with higher priorities are
called first.InvalidObjectException
- validator
is
null
NotActiveException
- an attempt was made to add a
validator outside of the readObject
method of the
object currently being deserializedObjectInputValidation
protected Class<?> resolveClass(ObjectStreamClass osc) throws ClassNotFoundException, IOException
annotateClass (Class)
method of an
ObjectOutputStream
.
This implementation looks up the active call stack for a
ClassLoader
; if a ClassLoader
is found,
it is used to load the class associated with osc
,
otherwise, the default system ClassLoader
is used.IOException
- Exception from underlying
OutputStream
.ClassNotFoundException
(java.lang.Class)
protected Object resolveObject(Object obj) throws IOException
IOException
- Exception from underlying
OutputStream
.enableResolveObject(boolean)
protected Class<?> resolveProxyClass(String[] intfs) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
protected boolean enableResolveObject(boolean enable) throws SecurityException
enable
is true
and this object is
trusted, then resolveObject (Object)
will be called
in subsequent calls to readObject (Object)
.
Otherwise, resolveObject (Object)
will not be called.SecurityException
- This class is not trusted.protected void readStreamHeader() throws IOException, StreamCorruptedException
IOException
- Exception from underlying stream.StreamCorruptedException
- An invalid stream magic
number or stream version was read from the stream.public int read() throws IOException
InputStream
This method will block until the byte can be read.
read
in interface ObjectInput
read
in class InputStream
IOException
- If an error occurspublic int read(byte[] data, int offset, int length) throws IOException
InputStream
off
into the buffer and attempts to read
len
bytes. This method can return before reading the
number of bytes requested. The actual number of bytes read is
returned as an int. A -1 is returned to indicate the end of the
stream.
This method will block until some data can be read.
This method operates by calling the single byte read()
method
in a loop until the desired number of bytes are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a bytes fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read
in interface ObjectInput
read
in class InputStream
data
- The array into which the bytes read should be storedoffset
- The offset into the array to start storing byteslength
- The requested number of bytes to readIOException
- If an error occurs.public int available() throws IOException
InputStream
This method always returns 0 in this class
available
in interface ObjectInput
available
in class InputStream
IOException
- If an error occurspublic void close() throws IOException
InputStream
IOException
This method does nothing in this class, but subclasses may override this method in order to provide additional functionality.
close
in interface Closeable
close
in interface ObjectInput
close
in class InputStream
IOException
- If an error occurs, which can only happen
in a subclasspublic boolean readBoolean() throws IOException
DataInput
false
. If the byte is non-zero, then
the value returned is true
.
This method can read a boolean
written by an object
implementing the writeBoolean()
method in the
DataOutput
interface.
readBoolean
in interface DataInput
boolean
value readEOFException
- If end of file is reached before
reading the booleanIOException
- If any other error occursDataOutput.writeBoolean(boolean)
public byte readByte() throws IOException
DataInput
This method can read a byte
written by an object
implementing the
writeByte()
method in the DataOutput
interface.
readByte
in interface DataInput
byte
value readEOFException
- If end of file is reached before reading the byteIOException
- If any other error occursDataOutput.writeByte(int)
public int readUnsignedByte() throws IOException
DataInput
int
value from
the stream. The value returned is in the range of 0 to 255.
This method can read an unsigned byte written by an object
implementing the
writeByte()
method in the DataOutput
interface.
readUnsignedByte
in interface DataInput
int
.EOFException
- If end of file is reached before reading the valueIOException
- If any other error occursDataOutput.writeByte(int)
public short readShort() throws IOException
DataInput
short
. The two bytes are stored most
significant byte first (i.e., "big endian") regardless of the native
host byte ordering.
As an example, if byte1
and byte2
represent the
first and second byte read from the stream respectively, they will be
transformed to a short
in the following manner:
(short)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))
The value returned is in the range of -32768 to 32767.
This method can read a short
written by an object
implementing
the writeShort()
method in the DataOutput
interface.
readShort
in interface DataInput
short
value readEOFException
- If end of file is reached before reading the valueIOException
- If any other error occursDataOutput.writeShort(int)
public int readUnsignedShort() throws IOException
DataInput
int
. The two bytes are stored most
significant byte first (i.e., "big endian") regardless of the native
host byte ordering.
As an example, if byte1
and byte2
represent the
first and second byte read from the stream respectively, they will be
transformed to an int
in the following manner:
(int)(((byte1 0xFF) << 8) + (byte2 & 0xFF))
The value returned is in the range of 0 to 65535.
This method can read an unsigned short written by an object implementing
the writeShort()
method in the
DataOutput
interface.
readUnsignedShort
in interface DataInput
int
.EOFException
- If end of file is reached before reading
the valueIOException
- If any other error occursDataOutput.writeShort(int)
public char readChar() throws IOException
DataInput
char
value from an input stream.
It operates by reading two bytes from the stream and converting them to
a single 16-bit Java char
. The two bytes are stored most
significant byte first (i.e., "big endian") regardless of the native
host byte ordering.
As an example, if byte1
and byte2
represent the
first and second byte read from the stream respectively, they will be
transformed to a char
in the following manner:
(char)((byte1 << 8) + byte2)
This method can read a char
written by an object implementing
the
writeChar()
method in the DataOutput
interface.
readChar
in interface DataInput
char
value readEOFException
- If end of file is reached before reading the charIOException
- If any other error occursDataOutput.writeChar(int)
public int readInt() throws IOException
DataInput
int
value from an input stream
It operates by reading four bytes from the stream and converting them to
a single Java int
. The bytes are stored most
significant byte first (i.e., "big endian") regardless of the native
host byte ordering.
As an example, if byte1
through byte4
represent
the first four bytes read from the stream, they will be
transformed to an int
in the following manner:
(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) +
((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))
The value returned is in the range of -2147483648 to 2147483647.
This method can read an int
written by an object
implementing the writeInt()
method in the
DataOutput
interface.
readInt
in interface DataInput
int
value readEOFException
- If end of file is reached before reading the intIOException
- If any other error occursDataOutput.writeInt(int)
public long readLong() throws IOException
DataInput
long
value from an input stream
It operates by reading eight bytes from the stream and converting them to
a single Java long
. The bytes are stored most
significant byte first (i.e., "big endian") regardless of the native
host byte ordering.
As an example, if byte1
through byte8
represent
the first eight bytes read from the stream, they will be
transformed to an long
in the following manner:
(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) +
((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) +
((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) +
((byte7 & 0xFF) << 8) + (byte8 & 0xFF)))
The value returned is in the range of -9223372036854775808 to 9223372036854775807.
This method can read an long
written by an object
implementing the writeLong()
method in the
DataOutput
interface.
readLong
in interface DataInput
long
value readEOFException
- If end of file is reached before reading the longIOException
- If any other error occursDataOutput.writeLong(long)
public float readFloat() throws IOException
DataInput
int
value from the stream by calling the
readInt()
method in this interface, then converts that
int
to a float
using the
intBitsToFloat
method in the class
java.lang.Float
.
This method can read a float
written by an object
implementing
the writeFloat()
method in the DataOutput
interface.
readFloat
in interface DataInput
float
value readEOFException
- If end of file is reached before reading the
floatIOException
- If any other error occursDataOutput.writeFloat(float)
,
Float.intBitsToFloat(int)
public double readDouble() throws IOException
DataInput
long
value from the stream by calling the
readLong()
method in this interface, then converts that
long
to a double
using the
longBitsToDouble
method in the class
java.lang.Double
.
This method can read a double
written by an object
implementing the writeDouble()
method in the
DataOutput
interface.
readDouble
in interface DataInput
double
value readEOFException
- If end of file is reached before reading the
doubleIOException
- If any other error occursDataOutput.writeDouble(double)
,
Double.longBitsToDouble(long)
public void readFully(byte[] data) throws IOException
DataInput
readFully
in interface DataInput
data
- The buffer into which to read the dataEOFException
- If end of file is reached before filling the
bufferIOException
- If any other error occurspublic void readFully(byte[] data, int offset, int size) throws IOException
DataInput
buf
starting
offset
bytes into the buffer. The number of bytes read
will be
exactly len
. Note that this method blocks until the data is
available and throws an exception if there is not enough data left in
the stream to read len
bytes. Note also that zero length
buffers are permitted. In this case, the method will return immediately
without reading any bytes from the stream.readFully
in interface DataInput
data
- The buffer into which to read the dataoffset
- The offset into the buffer to start storing datasize
- The number of bytes to read into the bufferEOFException
- If end of file is reached before filling the
bufferIOException
- If any other error occurspublic int skipBytes(int len) throws IOException
DataInput
skipBytes
in interface DataInput
len
- The number of bytes to skipnumBytes
EOFException
- If end of file is reached before all bytes can be
skippedIOException
- If any other error occurspublic String readLine() throws IOException
DataInput
char
values by treating the byte read as the low eight bits of the
char
and using 0 as the high eight bits. Because of this,
it does not support the full 16-bit Unicode character set.
The reading of bytes ends when either the end of file or a line terminator
is encountered. The bytes read are then returned as a
String
.
A line terminator is a byte sequence consisting of either
\r
, \n
or \r\n
. These termination
charaters are discarded and are not returned as part of the string.
A line is also terminated by an end of file condition.
readLine
in interface DataInput
String
IOException
- If an error occurs()
public String readUTF() throws IOException
DataInput
String
from an input stream that is
encoded in a modified UTF-8 format. This format has a leading two byte
sequence that contains the remaining number of bytes to read.
This two byte
sequence is read using the readUnsignedShort()
method of this
interface.
After the number of remaining bytes have been determined, these bytes
are read an transformed into char
values. These
char
values are encoded in the stream using either a one,
two, or three byte format.
The particular format in use can be determined by examining the first
byte read.
If the first byte has a high order bit of 0, then
that character consists on only one byte. This character value consists
of seven bits that are at positions 0 through 6 of the byte. As an
example, if byte1
is the byte read from the stream, it would
be converted to a char
like so:
(char)byte1
If the first byte has 110 as its high order bits, then the character consists of two bytes. The bits that make up the character value are in positions 0 through 4 of the first byte and bit positions 0 through 5 of the second byte. (The second byte should have 10 as its high order bits). These values are in most significant byte first (i.e., "big endian") order.
As an example, if byte1
and byte2
are the first
two bytes read respectively, and the high order bits of them match the
patterns which indicate a two byte character encoding, then they would be
converted to a Java char
like so:
(char)(((byte1 & 0x1F) << 6) + (byte2 & 0x3F))
If the first byte has a 1110 as its high order bits, then the character consists of three bytes. The bits that make up the character value are in positions 0 through 3 of the first byte and bit positions 0 through 5 of the other two bytes. (The second and third bytes should have 10 as their high order bits). These values are in most significant byte first (i.e., "big endian") order.
As an example, if byte1
, byte2
, and
byte3
are the three bytes read, and the high order bits of
them match the patterns which indicate a three byte character encoding,
then they would be converted to a Java char
like so:
(char)(((byte1 & 0x0F) << 12) + ((byte2 & 0x3F) + (byte3 & 0x3F))
Note that all characters are encoded in the method that requires the
fewest number of bytes with the exception of the character with the
value of \<llll>u0000
which is encoded as two bytes.
This is a modification of the UTF standard used to prevent C language
style NUL
values from appearing in the byte stream.
This method can read data that was written by an object implementing the
writeUTF()
method in DataOutput
.
readUTF
in interface DataInput
String
readEOFException
- If end of file is reached before reading the
StringUTFDataFormatException
- If the data is not in UTF-8 formatIOException
- If any other error occursDataOutput.writeUTF(java.lang.String)
public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException, NotActiveException
ObjectInputStream.GetField
class. Calling
this method will not alter the deserializing object.IOException
- An input/output exception occured.ClassNotFoundException
NotActiveException
protected Object readObjectOverride() throws ClassNotFoundException, IOException, OptionalDataException
ObjectInputStream
. To make this method be used for
writing objects, subclasses must invoke the 0-argument
constructor on this class from their constructor.ClassNotFoundException
IOException
OptionalDataException
ObjectInputStream()