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).
*
* @exception IOException Reading stream header from underlying
* stream cannot be completed.
*
* @exception StreamCorruptedException An invalid stream magic
* number or stream version was read from the stream.
*
* @see #readStreamHeader()
*/
public ObjectInputStream(InputStream in)
throws IOException, StreamCorruptedException
{
if (DEBUG)
{
String val = System.getProperty("gcj.dumpobjects");
if (dump == false && val != null && !val.equals(""))
{
dump = true;
System.out.println ("Serialization debugging enabled");
}
else if (dump == true && (val == null || val.equals("")))
{
dump = false;
System.out.println ("Serialization debugging disabled");
}
}
this.resolveEnabled = false;
this.blockDataPosition = 0;
this.blockDataBytes = 0;
this.blockData = new byte[BUFFER_SIZE];
this.blockDataInput = new DataInputStream(this);
this.realInputStream = new DataInputStream(in);
this.nextOID = baseWireHandle;
handles = new HashMapprivate 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.
*
* @return The object read from the underlying stream.
*
* @exception ClassNotFoundException The class that an object being
* read in belongs to cannot be found.
*
* @exception IOException Exception from underlying
* InputStream
.
*/
public final Object readObject()
throws ClassNotFoundException, IOException
{
return readObject(true);
}
/**
* * 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 {@link #readObject()}. *
** This behaviour is achieved by: *
*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.
*
* @param shared true if handles created by this call should be shared
* with later calls.
* @return The object read from the underlying stream.
*
* @exception ClassNotFoundException The class that an object being
* read in belongs to cannot be found.
*
* @exception IOException Exception from underlying
* InputStream
.
*/
private final Object readObject(boolean shared)
throws ClassNotFoundException, IOException
{
if (this.useSubclassMethod)
return readObjectOverride();
Object ret_val;
boolean old_mode = setBlockDataMode(false);
byte marker = this.realInputStream.readByte();
if (DEBUG)
depth += 2;
if(dump) dumpElement("MARKER: 0x" + Integer.toHexString(marker) + " ");
try
{
ret_val = parseContent(marker, shared);
}
finally
{
setBlockDataMode(old_mode);
if (DEBUG)
depth -= 2;
}
return ret_val;
}
/**
* Handles a content block within the stream, which begins with a marker
* byte indicating its type.
*
* @param marker the byte marker.
* @param shared true if handles created by this call should be shared
* with later calls.
* @return an object which represents the parsed content.
* @throws ClassNotFoundException if the class of an object being
* read in cannot be found.
* @throws IOException if invalid data occurs or one is thrown by the
* underlying InputStream
.
*/
private Object parseContent(byte marker, boolean shared)
throws ClassNotFoundException, IOException
{
Object ret_val;
boolean is_consumed = false;
switch (marker)
{
case TC_ENDBLOCKDATA:
{
ret_val = null;
is_consumed = true;
break;
}
case TC_BLOCKDATA:
case TC_BLOCKDATALONG:
{
if (marker == TC_BLOCKDATALONG)
{ if(dump) dumpElementln("BLOCKDATALONG"); }
else
{ if(dump) dumpElementln("BLOCKDATA"); }
readNextBlock(marker);
}
case TC_NULL:
{
if(dump) dumpElementln("NULL");
ret_val = null;
break;
}
case TC_REFERENCE:
{
if(dump) dumpElement("REFERENCE ");
int oid = realInputStream.readInt();
if(dump) dumpElementln(Integer.toHexString(oid));
ret_val = lookupHandle(oid);
if (!shared)
throw new
InvalidObjectException("References can not be read unshared.");
break;
}
case TC_CLASS:
{
if(dump) dumpElementln("CLASS");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class clazz = osc.forClass();
assignNewHandle(clazz,shared);
ret_val = clazz;
break;
}
case TC_PROXYCLASSDESC:
{
if(dump) dumpElementln("PROXYCLASS");
int n_intf = this.realInputStream.readInt();
String[] intfs = new String[n_intf];
for (int i = 0; i < n_intf; i++)
{
intfs[i] = this.realInputStream.readUTF();
}
boolean oldmode = setBlockDataMode(true);
Class cl = resolveProxyClass(intfs);
setBlockDataMode(oldmode);
ObjectStreamClass osc = lookupClass(cl);
if (osc.firstNonSerializableParentConstructor == null)
{
osc.realClassIsSerializable = true;
osc.fields = osc.fieldMapping = new ObjectStreamField[0];
try
{
osc.firstNonSerializableParentConstructor =
Object.class.getConstructor(new Class[0]);
}
catch (NoSuchMethodException x)
{
throw (InternalError)
new InternalError("Object ctor missing").initCause(x);
}
}
assignNewHandle(osc,shared);
if (!is_consumed)
{
byte b = this.realInputStream.readByte();
if (b != TC_ENDBLOCKDATA)
throw new IOException("Data annotated to class was not consumed." + b);
}
else
is_consumed = false;
ObjectStreamClass superosc = (ObjectStreamClass)readObject();
osc.setSuperclass(superosc);
ret_val = osc;
break;
}
case TC_CLASSDESC:
{
ObjectStreamClass osc = readClassDescriptor();
if (!is_consumed)
{
byte b = this.realInputStream.readByte();
if (b != TC_ENDBLOCKDATA)
throw new IOException("Data annotated to class was not consumed." + b);
}
else
is_consumed = false;
osc.setSuperclass ((ObjectStreamClass)readObject());
ret_val = osc;
break;
}
case TC_STRING:
{
if(dump) dumpElement("STRING=");
String s = this.realInputStream.readUTF();
if(dump) dumpElementln(s);
ret_val = processResolution(null, s, assignNewHandle(s,shared),
shared);
break;
}
case TC_LONGSTRING:
{
if(dump) dumpElement("STRING=");
String s = this.realInputStream.readUTFLong();
if(dump) dumpElementln(s);
ret_val = processResolution(null, s, assignNewHandle(s,shared),
shared);
break;
}
case TC_ARRAY:
{
if(dump) dumpElementln("ARRAY");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class componentType = osc.forClass().getComponentType();
if(dump) dumpElement("ARRAY LENGTH=");
int length = this.realInputStream.readInt();
if(dump) dumpElementln (length + "; COMPONENT TYPE=" + componentType);
Object array = Array.newInstance(componentType, length);
int handle = assignNewHandle(array,shared);
readArrayElements(array, componentType);
if(dump)
for (int i = 0, len = Array.getLength(array); i < len; i++)
dumpElementln(" ELEMENT[" + i + "]=", Array.get(array, i));
ret_val = processResolution(null, array, handle, shared);
break;
}
case TC_OBJECT:
{
if(dump) dumpElementln("OBJECT");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class clazz = osc.forClass();
if (!osc.realClassIsSerializable)
throw new NotSerializableException
(clazz + " is not Serializable, and thus cannot be deserialized.");
if (osc.realClassIsExternalizable)
{
Externalizable obj = osc.newInstance();
int handle = assignNewHandle(obj,shared);
boolean read_from_blocks = ((osc.getFlags() & SC_BLOCK_DATA) != 0);
boolean oldmode = this.readDataFromBlock;
if (read_from_blocks)
setBlockDataMode(true);
obj.readExternal(this);
if (read_from_blocks)
{
setBlockDataMode(oldmode);
if (!oldmode)
if (this.realInputStream.readByte() != TC_ENDBLOCKDATA)
throw new IOException("No end of block data seen for class with readExternal (ObjectInputStream) method.");
}
ret_val = processResolution(osc, obj, handle,shared);
break;
} // end if (osc.realClassIsExternalizable)
Object obj = newObject(clazz, osc.firstNonSerializableParentConstructor);
int handle = assignNewHandle(obj,shared);
Object prevObject = this.currentObject;
ObjectStreamClass prevObjectStreamClass = this.currentObjectStreamClass;
TreeSetprivate void readObject (ObjectInputStream)
* method.
*
* @exception ClassNotFoundException The class that an object being
* read in belongs to cannot be found.
*
* @exception NotActiveException This method was called from a
* context other than from the current object's and current class's
* private void readObject (ObjectInputStream)
* method.
*
* @exception IOException Exception from underlying
* OutputStream
.
*/
public void defaultReadObject()
throws ClassNotFoundException, IOException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("defaultReadObject called by non-active"
+ " class and/or object");
if (fieldsAlreadyRead)
throw new NotActiveException("defaultReadObject called but fields "
+ "already read from stream (by "
+ "defaultReadObject or readFields)");
boolean oldmode = setBlockDataMode(false);
readFields(this.currentObject, this.currentObjectStreamClass);
setBlockDataMode(oldmode);
fieldsAlreadyRead = true;
}
/**
* Registers a 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.
*
* @see java.io.ObjectInputValidation
*
* @exception InvalidObjectException validator
is
* null
*
* @exception NotActiveException an attempt was made to add a
* validator outside of the readObject
method of the
* object currently being deserialized
*/
public void registerValidation(ObjectInputValidation validator,
int priority)
throws InvalidObjectException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("registerValidation called by non-active "
+ "class and/or object");
if (validator == null)
throw new InvalidObjectException("attempt to add a null "
+ "ObjectInputValidation object");
if (currentObjectValidators == null)
currentObjectValidators = new TreeSetannotateClass (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.
*
* @exception IOException Exception from underlying
* OutputStream
.
*
* @see java.io.ObjectOutputStream#annotateClass (java.lang.Class)
*/
protected Class> resolveClass(ObjectStreamClass osc)
throws ClassNotFoundException, IOException
{
String name = osc.getName();
try
{
return Class.forName(name, true, currentLoader());
}
catch(ClassNotFoundException x)
{
if (name.equals("void"))
return Void.TYPE;
else if (name.equals("boolean"))
return Boolean.TYPE;
else if (name.equals("byte"))
return Byte.TYPE;
else if (name.equals("char"))
return Character.TYPE;
else if (name.equals("short"))
return Short.TYPE;
else if (name.equals("int"))
return Integer.TYPE;
else if (name.equals("long"))
return Long.TYPE;
else if (name.equals("float"))
return Float.TYPE;
else if (name.equals("double"))
return Double.TYPE;
else
throw x;
}
}
/**
* Returns the most recent user defined ClassLoader on the execution stack
* or null if none is found.
*/
private ClassLoader currentLoader()
{
return VMStackWalker.firstNonNullClassLoader();
}
/**
* Lookup a class stored in the local hashtable. If it is not
* use the global lookup function in ObjectStreamClass to build
* the ObjectStreamClass. This method is requested according to
* the behaviour detected in the JDK by Kaffe's team.
*
* @param clazz Class to lookup in the hash table or for which
* we must build a descriptor.
* @return A valid instance of ObjectStreamClass corresponding
* to the specified class.
*/
private ObjectStreamClass lookupClass(Class clazz)
{
if (clazz == null)
return null;
ObjectStreamClass oclazz;
oclazz = classLookupTable.get(clazz);
if (oclazz == null)
return ObjectStreamClass.lookup(clazz);
else
return oclazz;
}
/**
* Reconstruct class hierarchy the same way {@link
* java.io.ObjectStreamClass#hierarchy} does but using lookupClass
* instead of ObjectStreamClass.lookup.
*
* @param clazz This is the class for which we want the hierarchy.
*
* @return An array of valid {@link java.io.ObjectStreamClass} instances which
* represent the class hierarchy for clazz.
*/
private ObjectStreamClass[] hierarchy(Class clazz)
{
ObjectStreamClass osc = lookupClass(clazz);
return osc == null ? new ObjectStreamClass[0] : osc.hierarchy();
}
/**
* Allows subclasses to resolve objects that are read from the
* stream with other objects to be returned in their place. This
* method is called the first time each object is encountered.
*
* This method must be enabled before it will be called in the
* serialization process.
*
* @exception IOException Exception from underlying
* OutputStream
.
*
* @see #enableResolveObject(boolean)
*/
protected Object resolveObject(Object obj) throws IOException
{
return obj;
}
protected Class> resolveProxyClass(String[] intfs)
throws IOException, ClassNotFoundException
{
ClassLoader cl = currentLoader();
Class>[] clss = new Class>[intfs.length];
if(cl == null)
{
for (int i = 0; i < intfs.length; i++)
clss[i] = Class.forName(intfs[i]);
cl = ClassLoader.getSystemClassLoader();
}
else
for (int i = 0; i < intfs.length; i++)
clss[i] = Class.forName(intfs[i], false, cl);
try
{
return Proxy.getProxyClass(cl, clss);
}
catch (IllegalArgumentException e)
{
throw new ClassNotFoundException(null, e);
}
}
/**
* If 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.
*
* @exception SecurityException This class is not trusted.
*/
protected boolean enableResolveObject (boolean enable)
throws SecurityException
{
if (enable)
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new SerializablePermission("enableSubstitution"));
}
boolean old_val = this.resolveEnabled;
this.resolveEnabled = enable;
return old_val;
}
/**
* Reads stream magic and stream version information from the
* underlying stream.
*
* @exception IOException Exception from underlying stream.
*
* @exception StreamCorruptedException An invalid stream magic
* number or stream version was read from the stream.
*/
protected void readStreamHeader()
throws IOException, StreamCorruptedException
{
if(dump) dumpElement("STREAM MAGIC ");
if (this.realInputStream.readShort() != STREAM_MAGIC)
throw new StreamCorruptedException("Invalid stream magic number");
if(dump) dumpElementln("STREAM VERSION ");
if (this.realInputStream.readShort() != STREAM_VERSION)
throw new StreamCorruptedException("Invalid stream version number");
}
public int read() throws IOException
{
if (this.readDataFromBlock)
{
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock();
return (this.blockData[this.blockDataPosition++] & 0xff);
}
else
return this.realInputStream.read();
}
public int read(byte[] data, int offset, int length) throws IOException
{
if (this.readDataFromBlock)
{
int remain = this.blockDataBytes - this.blockDataPosition;
if (remain == 0)
{
readNextBlock();
remain = this.blockDataBytes - this.blockDataPosition;
}
length = Math.min(length, remain);
System.arraycopy(this.blockData, this.blockDataPosition,
data, offset, length);
this.blockDataPosition += length;
return length;
}
else
return this.realInputStream.read(data, offset, length);
}
public int available() throws IOException
{
if (this.readDataFromBlock)
{
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock ();
return this.blockDataBytes - this.blockDataPosition;
}
else
return this.realInputStream.available();
}
public void close() throws IOException
{
this.realInputStream.close();
}
public boolean readBoolean() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode (true);
boolean value = this.dataInputStream.readBoolean ();
if (switchmode)
setBlockDataMode (oldmode);
return value;
}
public byte readByte() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
byte value = this.dataInputStream.readByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedByte() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public short readShort() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
short value = this.dataInputStream.readShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedShort() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public char readChar() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
char value = this.dataInputStream.readChar();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readInt() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readInt();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public long readLong() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
long value = this.dataInputStream.readLong();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public float readFloat() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
float value = this.dataInputStream.readFloat();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public double readDouble() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
double value = this.dataInputStream.readDouble();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public void readFully(byte data[]) throws IOException
{
this.dataInputStream.readFully(data);
}
public void readFully(byte data[], int offset, int size)
throws IOException
{
this.dataInputStream.readFully(data, offset, size);
}
public int skipBytes(int len) throws IOException
{
return this.dataInputStream.skipBytes(len);
}
/**
* @deprecated
* @see java.io.DataInputStream#readLine ()
*/
public String readLine() throws IOException
{
return this.dataInputStream.readLine();
}
public String readUTF() throws IOException
{
return this.dataInputStream.readUTF();
}
/**
* This class allows a class to specify exactly which fields should
* be read, and what values should be read for these fields.
*
* XXX: finish up comments
*/
public abstract static class GetField
{
public abstract ObjectStreamClass getObjectStreamClass();
public abstract boolean defaulted(String name)
throws IOException, IllegalArgumentException;
public abstract boolean get(String name, boolean defvalue)
throws IOException, IllegalArgumentException;
public abstract char get(String name, char defvalue)
throws IOException, IllegalArgumentException;
public abstract byte get(String name, byte defvalue)
throws IOException, IllegalArgumentException;
public abstract short get(String name, short defvalue)
throws IOException, IllegalArgumentException;
public abstract int get(String name, int defvalue)
throws IOException, IllegalArgumentException;
public abstract long get(String name, long defvalue)
throws IOException, IllegalArgumentException;
public abstract float get(String name, float defvalue)
throws IOException, IllegalArgumentException;
public abstract double get(String name, double defvalue)
throws IOException, IllegalArgumentException;
public abstract Object get(String name, Object defvalue)
throws IOException, IllegalArgumentException;
}
/**
* This method should be called by a method called 'readObject' in the
* deserializing class (if present). It cannot (and should not)be called
* outside of it. Its goal is to read all fields in the real input stream
* and keep them accessible through the {@link GetField} class. Calling
* this method will not alter the deserializing object.
*
* @return A valid freshly created 'GetField' instance to get access to
* the deserialized stream.
* @throws IOException An input/output exception occured.
* @throws ClassNotFoundException
* @throws NotActiveException
*/
public GetField readFields()
throws IOException, ClassNotFoundException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("readFields called by non-active class and/or object");
if (prereadFields != null)
return prereadFields;
if (fieldsAlreadyRead)
throw new NotActiveException("readFields called but fields already read from"
+ " stream (by defaultReadObject or readFields)");
final ObjectStreamClass clazz = this.currentObjectStreamClass;
final byte[] prim_field_data = new byte[clazz.primFieldSize];
final Object[] objs = new Object[clazz.objectFieldCount];
// Apparently Block data is not used with GetField as per
// empirical evidence against JDK 1.2. Also see Mauve test
// java.io.ObjectInputOutput.Test.GetPutField.
boolean oldmode = setBlockDataMode(false);
readFully(prim_field_data);
for (int i = 0; i < objs.length; ++ i)
objs[i] = readObject();
setBlockDataMode(oldmode);
prereadFields = new GetField()
{
public ObjectStreamClass getObjectStreamClass()
{
return clazz;
}
public boolean defaulted(String name)
throws IOException, IllegalArgumentException
{
ObjectStreamField f = clazz.getField(name);
/* First if we have a serialized field use the descriptor */
if (f != null)
{
/* It is in serialPersistentFields but setClass tells us
* it should not be set. This value is defaulted.
*/
if (f.isPersistent() && !f.isToSet())
return true;
return false;
}
/* This is not a serialized field. There should be
* a default value only if the field really exists.
*/
try
{
return (clazz.forClass().getDeclaredField (name) != null);
}
catch (NoSuchFieldException e)
{
throw new IllegalArgumentException(e);
}
}
public boolean get(String name, boolean defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Boolean.TYPE);
if (field == null)
return defvalue;
return prim_field_data[field.getOffset()] == 0 ? false : true;
}
public char get(String name, char defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Character.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (char)(((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public byte get(String name, byte defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Byte.TYPE);
if (field == null)
return defvalue;
return prim_field_data[field.getOffset()];
}
public short get(String name, short defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Short.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (short)(((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public int get(String name, int defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Integer.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF);
}
public long get(String name, long defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Long.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (long)(((prim_field_data[off++] & 0xFFL) << 56)
| ((prim_field_data[off++] & 0xFFL) << 48)
| ((prim_field_data[off++] & 0xFFL) << 40)
| ((prim_field_data[off++] & 0xFFL) << 32)
| ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public float get(String name, float defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Float.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return Float.intBitsToFloat(((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public double get(String name, double defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Double.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return Double.longBitsToDouble
( (long) (((prim_field_data[off++] & 0xFFL) << 56)
| ((prim_field_data[off++] & 0xFFL) << 48)
| ((prim_field_data[off++] & 0xFFL) << 40)
| ((prim_field_data[off++] & 0xFFL) << 32)
| ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF)));
}
public Object get(String name, Object defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field =
getField(name, defvalue == null ? null : defvalue.getClass ());
if (field == null)
return defvalue;
return objs[field.getOffset()];
}
private ObjectStreamField getField(String name, Class type)
throws IllegalArgumentException
{
ObjectStreamField field = clazz.getField(name);
boolean illegal = false;
// XXX This code is horrible and needs to be rewritten!
try
{
try
{
Class field_type = field.getType();
if (type == field_type ||
(type == null && !field_type.isPrimitive()))
{
/* See defaulted */
return field;
}
illegal = true;
throw new IllegalArgumentException
("Field requested is of type "
+ field_type.getName()
+ ", but requested type was "
+ (type == null ? "Object" : type.getName()));
}
catch (NullPointerException _)
{
/* Here we catch NullPointerException, because it may
only come from the call 'field.getType()'. If field
is null, we have to return null and classpath ethic
say we must try to avoid 'if (xxx == null)'.
*/
}
catch (IllegalArgumentException e)
{
throw e;
}
return null;
}
finally
{
/* If this is an unassigned field we should return
* the default value.
*/
if (!illegal && field != null && !field.isToSet() && field.isPersistent())
return null;
/* We do not want to modify transient fields. They should
* be left to 0.
*/
try
{
Field f = clazz.forClass().getDeclaredField(name);
if (Modifier.isTransient(f.getModifiers()))
throw new IllegalArgumentException
("no such field (non transient) " + name);
if (field == null && f.getType() != type)
throw new IllegalArgumentException
("Invalid requested type for field " + name);
}
catch (NoSuchFieldException e)
{
if (field == null)
throw new IllegalArgumentException(e);
}
}
}
};
fieldsAlreadyRead = true;
return prereadFields;
}
/**
* Protected constructor that allows subclasses to override
* deserialization. This constructor should be called by subclasses
* that wish to override 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.
*
* @see #readObjectOverride()
*/
protected ObjectInputStream()
throws IOException, SecurityException
{
SecurityManager sec_man = System.getSecurityManager();
if (sec_man != null)
sec_man.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
this.useSubclassMethod = true;
}
/**
* This method allows subclasses to override the default
* de serialization mechanism provided by
* ObjectInputStream
. To make this method be used for
* writing objects, subclasses must invoke the 0-argument
* constructor on this class from their constructor.
*
* @see #ObjectInputStream()
*/
protected Object readObjectOverride()
throws ClassNotFoundException, IOException, OptionalDataException
{
throw new IOException("Subclass of ObjectInputStream must implement readObjectOverride");
}
/**
* Assigns the next available handle to obj
.
*
* @param obj The object for which we want a new handle.
* @param shared True if the handle should be shared
* with later calls.
* @return A valid handle for the specified object.
*/
private int assignNewHandle(Object obj, boolean shared)
{
int handle = this.nextOID;
this.nextOID = handle + 1;
rememberHandle(obj,shared,handle);
return handle;
}
/**
* Remember the object associated with the given handle.
*
* @param obj an object
* @param shared true if the reference should be shared
* with later calls.
* @param handle a handle, must be >= baseWireHandle
*
* @see #lookupHandle
*/
private void rememberHandle(Object obj, boolean shared,
int handle)
{
handles.put(handle, new Pair