that is able to read (deserialize) Java
objects as well as primitive data types (int, byte, char etc.). The data has
typically been saved using an ObjectOutputStream.
| Method from java.io.ObjectInputStream Detail: |
public int available() throws IOException {
// returns 0 if next data is an object, or N if reading primitive types
checkReadPrimitiveTypes();
return primitiveData.available();
}
Returns the number of bytes of primitive data that can be read from this
stream without blocking. This method should not be used at any arbitrary
position; just when reading primitive data types (int, char etc). |
public void close() throws IOException {
input.close();
}
Closes this stream. This implementation closes the source stream. |
public void defaultReadObject() throws IOException, ClassNotFoundException, NotActiveException {
// We can't be called from just anywhere. There are rules.
if (currentObject != null || !mustResolve) {
readFieldValues(currentObject, currentClass);
} else {
throw new NotActiveException();
}
}
Default method to read objects from this stream. Serializable fields
defined in the object's class and superclasses are read from the source
stream. |
protected boolean enableResolveObject(boolean enable) throws SecurityException {
if (enable) {
// The Stream has to be trusted for this feature to be enabled.
// trusted means the stream's classloader has to be null
SecurityManager currentManager = System.getSecurityManager();
if (currentManager != null) {
currentManager.checkPermission(SUBSTITUTION_PERMISSION);
}
}
boolean originalValue = enableResolve;
enableResolve = enable;
return originalValue;
}
Enables object replacement for this stream. By default this is not
enabled. Only trusted subclasses (loaded with system class loader) are
allowed to change this status. |
public int read() throws IOException {
checkReadPrimitiveTypes();
return primitiveData.read();
}
Reads a single byte from the source stream and returns it as an integer
in the range from 0 to 255. Returns -1 if the end of the source stream
has been reached. Blocks if no input is available. |
public int read(byte[] buffer,
int offset,
int length) throws IOException {
// Force buffer null check first!
if (offset > buffer.length || offset < 0) {
// luni.12=Offset out of bounds \: {0}
throw new ArrayIndexOutOfBoundsException(Messages.getString("luni.12", offset)); //$NON-NLS-1$
}
if (length < 0 || length > buffer.length - offset) {
// luni.18=Length out of bounds \: {0}
throw new ArrayIndexOutOfBoundsException(Messages.getString("luni.18", length)); //$NON-NLS-1$
}
if (length == 0) {
return 0;
}
checkReadPrimitiveTypes();
return primitiveData.read(buffer, offset, length);
}
Reads at most {@code length} bytes from the source stream and stores them
in byte array {@code buffer} starting at offset {@code count}. Blocks
until {@code count} bytes have been read, the end of the source stream is
detected or an exception is thrown. |
public boolean readBoolean() throws IOException {
return primitiveTypes.readBoolean();
}
Reads a boolean from the source stream. |
public byte readByte() throws IOException {
return primitiveTypes.readByte();
}
Reads a byte (8 bit) from the source stream. |
public char readChar() throws IOException {
return primitiveTypes.readChar();
}
Reads a character (16 bit) from the source stream. |
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass newClassDesc = new ObjectStreamClass();
String name = input.readUTF();
if (name.length() == 0) {
// luni.07 = The stream is corrupted
throw new IOException(Messages.getString("luni.07")); //$NON-NLS-1$
}
newClassDesc.setName(name);
newClassDesc.setSerialVersionUID(input.readLong());
newClassDesc.setFlags(input.readByte());
/*
* We must register the class descriptor before reading field
* descriptors. If called outside of readObject, the descriptorHandle
* might be null.
*/
descriptorHandle = (null == descriptorHandle ? nextHandle() : descriptorHandle);
registerObjectRead(newClassDesc, descriptorHandle, false);
readFieldDescriptors(newClassDesc);
return newClassDesc;
}
Reads a class descriptor from the source stream. |
public double readDouble() throws IOException {
return primitiveTypes.readDouble();
}
Reads a double (64 bit) from the source stream. |
public GetField readFields() throws IOException, ClassNotFoundException, NotActiveException {
// We can't be called from just anywhere. There are rules.
if (currentObject == null) {
throw new NotActiveException();
}
EmulatedFieldsForLoading result = new EmulatedFieldsForLoading(
currentClass);
readFieldValues(result);
return result;
}
Reads the persistent fields of the object that is currently being read
from the source stream. The values read are stored in a GetField object
that provides access to the persistent fields. This GetField object is
then returned. |
public float readFloat() throws IOException {
return primitiveTypes.readFloat();
}
Reads a float (32 bit) from the source stream. |
public void readFully(byte[] buffer) throws IOException {
primitiveTypes.readFully(buffer);
}
Reads bytes from the source stream into the byte array {@code buffer}.
This method will block until {@code buffer.length} bytes have been read. |
public void readFully(byte[] buffer,
int offset,
int length) throws IOException {
primitiveTypes.readFully(buffer, offset, length);
}
Reads bytes from the source stream into the byte array {@code buffer}.
This method will block until {@code length} number of bytes have been
read. |
public int readInt() throws IOException {
return primitiveTypes.readInt();
}
Reads an integer (32 bit) from the source stream. |
public String readLine() throws IOException {
return primitiveTypes.readLine();
} Deprecated! Use - BufferedReader
Reads the next line from the source stream. Lines are terminated by
{@code '\r'}, {@code '\n'}, {@code "\r\n"} or an {@code EOF}. |
public long readLong() throws IOException {
return primitiveTypes.readLong();
}
Reads a long (64 bit) from the source stream. |
public final Object readObject() throws OptionalDataException, ClassNotFoundException, IOException {
return readObject(false);
}
Reads the next object from the source stream. |
protected Object readObjectOverride() throws OptionalDataException, ClassNotFoundException, IOException {
if (input == null) {
return null;
}
// Subclasses must override.
throw new IOException();
}
Method to be overriden by subclasses to read the next object from the
source stream. |
public short readShort() throws IOException {
return primitiveTypes.readShort();
}
Reads a short (16 bit) from the source stream. |
protected void readStreamHeader() throws IOException, StreamCorruptedException {
if (input.readShort() == STREAM_MAGIC
&& input.readShort() == STREAM_VERSION) {
return;
}
throw new StreamCorruptedException();
}
Reads and validates the ObjectInputStream header from the source stream. |
public String readUTF() throws IOException {
return primitiveTypes.readUTF();
}
|
public Object readUnshared() throws IOException, ClassNotFoundException {
return readObject(true);
}
Reads the next unshared object from the source stream. |
public int readUnsignedByte() throws IOException {
return primitiveTypes.readUnsignedByte();
}
Reads an unsigned byte (8 bit) from the source stream. |
public int readUnsignedShort() throws IOException {
return primitiveTypes.readUnsignedShort();
}
Reads an unsigned short (16 bit) from the source stream. |
public synchronized void registerValidation(ObjectInputValidation object,
int priority) throws NotActiveException, InvalidObjectException {
// Validation can only be registered when inside readObject calls
Object instanceBeingRead = this.currentObject;
// We can't be called from just anywhere. There are rules.
if (instanceBeingRead == null && nestedLevels == 0) {
throw new NotActiveException();
}
if (object == null) {
throw new InvalidObjectException(Messages.getString("luni.C6")); //$NON-NLS-1$
}
// From now on it is just insertion in a SortedCollection. Since
// the Java class libraries don't provide that, we have to
// implement it from scratch here.
InputValidationDesc desc = new InputValidationDesc();
desc.validator = object;
desc.priority = priority;
// No need for this, validateObject does not take a parameter
// desc.toValidate = instanceBeingRead;
if (validations == null) {
validations = new InputValidationDesc[1];
validations[0] = desc;
} else {
int i = 0;
for (; i < validations.length; i++) {
InputValidationDesc validation = validations[i];
// Sorted, higher priority first.
if (priority >= validation.priority) {
break; // Found the index where to insert
}
}
InputValidationDesc[] oldValidations = validations;
int currentSize = oldValidations.length;
validations = new InputValidationDesc[currentSize + 1];
System.arraycopy(oldValidations, 0, validations, 0, i);
System.arraycopy(oldValidations, i, validations, i + 1, currentSize
- i);
validations[i] = desc;
}
}
Registers a callback for post-deserialization validation of objects. It
allows to perform additional consistency checks before the {@code
readObject()} method of this class returns its result to the caller. This
method can only be called from within the {@code readObject()} method of
a class that implements "special" deserialization rules. It can be called
multiple times. Validation callbacks are then done in order of decreasing
priority, defined by {@code priority}. |
protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundException {
// fastpath: obtain cached value
Class< ? > cls = osClass.forClass();
if (null == cls) {
// slowpath: resolve the class
String className = osClass.getName();
// if it is primitive class, for example, long.class
cls = PRIMITIVE_CLASSES.get(className);
if (null == cls) {
// not primitive class
// Use the first non-null ClassLoader on the stack. If null, use
// the system class loader
cls = Class.forName(className, true, callerClassLoader);
}
}
return cls;
}
Loads the Java class corresponding to the class descriptor {@code
osClass} that has just been read from the source stream. |
protected Object resolveObject(Object object) throws IOException {
// By default no object replacement. Subclasses can override
return object;
}
Allows trusted subclasses to substitute the specified original {@code
object} with a new object. Object substitution has to be activated first
with calling {@code enableResolveObject(true)}. This implementation just
returns {@code object}. |
protected Class<?> resolveProxyClass(String[] interfaceNames) throws IOException, ClassNotFoundException {
// TODO: This method is opportunity for performance enhancement
// We can cache the classloader and recently used interfaces.
ClassLoader loader = VM.getNonBootstrapClassLoader();
Class< ? >[] interfaces = new Class< ? >[interfaceNames.length];
for (int i = 0; i < interfaceNames.length; i++) {
interfaces[i] = Class.forName(interfaceNames[i], false, loader);
}
try {
return Proxy.getProxyClass(loader, interfaces);
} catch (IllegalArgumentException e) {
throw new ClassNotFoundException(e.toString(), e);
}
}
Creates the proxy class that implements the interfaces specified in
{@code interfaceNames}. |
public int skipBytes(int length) throws IOException {
// To be used with available. Ok to call if reading primitive buffer
if (input == null) {
throw new NullPointerException();
}
int offset = 0;
while (offset < length) {
checkReadPrimitiveTypes();
long skipped = primitiveData.skip(length - offset);
if (skipped == 0) {
return offset;
}
offset += (int) skipped;
}
return length;
}
Skips {@code length} bytes on the source stream. This method should not
be used to skip bytes at any arbitrary position, just when reading
primitive data types (int, char etc). |