Commit eb813adf by Guilhem Lavaux Committed by Michael Koch

2003-12-31 Guilhem Lavaux <guilhem@kaffe.org>

	* java/io/ObjectOutputStream.java
	(putFields): Reindented. Fixed behaviour: currentPutField should be
	null
	before calling this method.
	(writeFields): Likewise.
	(markFieldsWritten): Fixed the exception message.
	(callWriteMethod): Ensure currentPutField is null.
	(getBooleanField): Translate IllegalArgumentException into
	InvalidClassException.
	(getByteField): Likewise.
	(getCharField): Likewise.
	(getDoubleField): Likewise.
	(getFloatField): Likewise.
	(getIntField): Likewise.
	(getLongField): Likewise.
	(getShortField): Likewise.
	(getObjectField): Check the type code before returning the object.
	(getField): Translate NoSuchFieldException into InvalidClassException
	directly.

From-SVN: r75265
parent d4158659
2003-12-31 Guilhem Lavaux <guilhem@kaffe.org>
* java/io/ObjectOutputStream.java
(putFields): Reindented. Fixed behaviour: currentPutField should be
null
before calling this method.
(writeFields): Likewise.
(markFieldsWritten): Fixed the exception message.
(callWriteMethod): Ensure currentPutField is null.
(getBooleanField): Translate IllegalArgumentException into
InvalidClassException.
(getByteField): Likewise.
(getCharField): Likewise.
(getDoubleField): Likewise.
(getFloatField): Likewise.
(getIntField): Likewise.
(getLongField): Likewise.
(getShortField): Likewise.
(getObjectField): Check the type code before returning the object.
(getField): Translate NoSuchFieldException into InvalidClassException
directly.
2003-12-31 Guilhem Lavaux <guilhem@kaffe.org> 2003-12-31 Guilhem Lavaux <guilhem@kaffe.org>
* java/net/URL.java * java/net/URL.java
......
...@@ -162,6 +162,9 @@ public class ObjectOutputStream extends OutputStream ...@@ -162,6 +162,9 @@ public class ObjectOutputStream extends OutputStream
* @exception NotSerializableException An attempt was made to * @exception NotSerializableException An attempt was made to
* serialize an <code>Object</code> that is not serializable. * serialize an <code>Object</code> that is not serializable.
* *
* @exception InvalidClassException Somebody tried to serialize
* an object which is wrongly formatted.
*
* @exception IOException Exception from underlying * @exception IOException Exception from underlying
* <code>OutputStream</code>. * <code>OutputStream</code>.
*/ */
...@@ -447,7 +450,7 @@ public class ObjectOutputStream extends OutputStream ...@@ -447,7 +450,7 @@ public class ObjectOutputStream extends OutputStream
if (fieldsAlreadyWritten) if (fieldsAlreadyWritten)
throw new IOException throw new IOException
("Only one of putFields and defaultWriteObject may be called, and it may only be called once"); ("Only one of writeFields and defaultWriteObject may be called, and it may only be called once");
fieldsAlreadyWritten = true; fieldsAlreadyWritten = true;
} }
...@@ -870,142 +873,150 @@ public class ObjectOutputStream extends OutputStream ...@@ -870,142 +873,150 @@ public class ObjectOutputStream extends OutputStream
public PutField putFields() throws IOException public PutField putFields() throws IOException
{ {
if (currentPutField == null) if (currentPutField != null)
return currentPutField;
currentPutField = new PutField()
{ {
currentPutField = new PutField () private byte[] prim_field_data
{ = new byte[currentObjectStreamClass.primFieldSize];
private byte[] prim_field_data = private Object[] objs
new byte[currentObjectStreamClass.primFieldSize]; = new Object[currentObjectStreamClass.objectFieldCount];
private Object[] objs =
new Object[currentObjectStreamClass.objectFieldCount];
public void put (String name, boolean value) private ObjectStreamField getField (String name)
{ {
ObjectStreamField field ObjectStreamField field
= currentObjectStreamClass.getField (name); = currentObjectStreamClass.getField(name);
checkType (field, 'Z');
prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0); if (field == null)
} throw new IllegalArgumentException("no such serializable field " + name);
return field;
}
public void put(String name, boolean value)
{
ObjectStreamField field = getField(name);
public void put (String name, byte value) checkType(field, 'Z');
{ prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0);
ObjectStreamField field }
= currentObjectStreamClass.getField (name);
checkType (field, 'B');
prim_field_data[field.getOffset ()] = value;
}
public void put (String name, char value) public void put(String name, byte value)
{ {
ObjectStreamField field ObjectStreamField field = getField(name);
= currentObjectStreamClass.getField (name);
checkType (field, 'C');
int off = field.getOffset ();
prim_field_data[off++] = (byte)(value >>> 8);
prim_field_data[off] = (byte)value;
}
public void put (String name, double value) checkType(field, 'B');
{ prim_field_data[field.getOffset()] = value;
ObjectStreamField field }
= currentObjectStreamClass.getField (name);
checkType (field, 'D');
int off = field.getOffset ();
long l_value = Double.doubleToLongBits (value);
prim_field_data[off++] = (byte)(l_value >>> 52);
prim_field_data[off++] = (byte)(l_value >>> 48);
prim_field_data[off++] = (byte)(l_value >>> 40);
prim_field_data[off++] = (byte)(l_value >>> 32);
prim_field_data[off++] = (byte)(l_value >>> 24);
prim_field_data[off++] = (byte)(l_value >>> 16);
prim_field_data[off++] = (byte)(l_value >>> 8);
prim_field_data[off] = (byte)l_value;
}
public void put (String name, float value) public void put(String name, char value)
{ {
ObjectStreamField field ObjectStreamField field = getField(name);
= currentObjectStreamClass.getField (name);
checkType (field, 'F');
int off = field.getOffset ();
int i_value = Float.floatToIntBits (value);
prim_field_data[off++] = (byte)(i_value >>> 24);
prim_field_data[off++] = (byte)(i_value >>> 16);
prim_field_data[off++] = (byte)(i_value >>> 8);
prim_field_data[off] = (byte)i_value;
}
public void put (String name, int value) checkType(field, 'C');
{ int off = field.getOffset();
ObjectStreamField field prim_field_data[off++] = (byte)(value >>> 8);
= currentObjectStreamClass.getField (name); prim_field_data[off] = (byte)value;
checkType (field, 'I'); }
int off = field.getOffset ();
prim_field_data[off++] = (byte)(value >>> 24);
prim_field_data[off++] = (byte)(value >>> 16);
prim_field_data[off++] = (byte)(value >>> 8);
prim_field_data[off] = (byte)value;
}
public void put (String name, long value) public void put(String name, double value)
{ {
ObjectStreamField field ObjectStreamField field = getField (name);
= currentObjectStreamClass.getField (name);
checkType (field, 'J'); checkType(field, 'D');
int off = field.getOffset (); int off = field.getOffset();
prim_field_data[off++] = (byte)(value >>> 52); long l_value = Double.doubleToLongBits (value);
prim_field_data[off++] = (byte)(value >>> 48); prim_field_data[off++] = (byte)(l_value >>> 52);
prim_field_data[off++] = (byte)(value >>> 40); prim_field_data[off++] = (byte)(l_value >>> 48);
prim_field_data[off++] = (byte)(value >>> 32); prim_field_data[off++] = (byte)(l_value >>> 40);
prim_field_data[off++] = (byte)(value >>> 24); prim_field_data[off++] = (byte)(l_value >>> 32);
prim_field_data[off++] = (byte)(value >>> 16); prim_field_data[off++] = (byte)(l_value >>> 24);
prim_field_data[off++] = (byte)(value >>> 8); prim_field_data[off++] = (byte)(l_value >>> 16);
prim_field_data[off] = (byte)value; prim_field_data[off++] = (byte)(l_value >>> 8);
} prim_field_data[off] = (byte)l_value;
}
public void put (String name, short value) public void put(String name, float value)
{ {
ObjectStreamField field ObjectStreamField field = getField(name);
= currentObjectStreamClass.getField (name);
checkType (field, 'S'); checkType(field, 'F');
int off = field.getOffset (); int off = field.getOffset();
prim_field_data[off++] = (byte)(value >>> 8); int i_value = Float.floatToIntBits(value);
prim_field_data[off] = (byte)value; prim_field_data[off++] = (byte)(i_value >>> 24);
} prim_field_data[off++] = (byte)(i_value >>> 16);
prim_field_data[off++] = (byte)(i_value >>> 8);
prim_field_data[off] = (byte)i_value;
}
public void put (String name, Object value) public void put(String name, int value)
{ {
ObjectStreamField field ObjectStreamField field = getField(name);
= currentObjectStreamClass.getField (name); checkType(field, 'I');
if (field == null) int off = field.getOffset();
throw new IllegalArgumentException (); prim_field_data[off++] = (byte)(value >>> 24);
if (value != null && prim_field_data[off++] = (byte)(value >>> 16);
! field.getType ().isAssignableFrom (value.getClass ())) prim_field_data[off++] = (byte)(value >>> 8);
throw new IllegalArgumentException (); prim_field_data[off] = (byte)value;
objs[field.getOffset ()] = value; }
}
public void write (ObjectOutput out) throws IOException public void put(String name, long value)
{ {
// Apparently Block data is not used with PutField as per ObjectStreamField field = getField(name);
// empirical evidence against JDK 1.2. Also see Mauve test checkType(field, 'J');
// java.io.ObjectInputOutput.Test.GetPutField. int off = field.getOffset();
boolean oldmode = setBlockDataMode (false); prim_field_data[off++] = (byte)(value >>> 52);
out.write (prim_field_data); prim_field_data[off++] = (byte)(value >>> 48);
for (int i = 0; i < objs.length; ++ i) prim_field_data[off++] = (byte)(value >>> 40);
out.writeObject (objs[i]); prim_field_data[off++] = (byte)(value >>> 32);
setBlockDataMode (oldmode); prim_field_data[off++] = (byte)(value >>> 24);
} prim_field_data[off++] = (byte)(value >>> 16);
prim_field_data[off++] = (byte)(value >>> 8);
prim_field_data[off] = (byte)value;
}
private void checkType (ObjectStreamField field, char type) public void put(String name, short value)
throws IllegalArgumentException {
{ ObjectStreamField field = getField(name);
if (TypeSignature.getEncodingOfClass(field.getType ()).charAt(0) checkType(field, 'S');
!= type) int off = field.getOffset();
throw new IllegalArgumentException (); prim_field_data[off++] = (byte)(value >>> 8);
} prim_field_data[off] = (byte)value;
}; }
}
public void put(String name, Object value)
{
ObjectStreamField field = getField(name);
if (value != null &&
! field.getType().isAssignableFrom(value.getClass ()))
throw new IllegalArgumentException("Class " + value.getClass() +
" cannot be cast to " + field.getType());
objs[field.getOffset()] = value;
}
public void write(ObjectOutput out) throws IOException
{
// Apparently Block data is not used with PutField as per
// empirical evidence against JDK 1.2. Also see Mauve test
// java.io.ObjectInputOutput.Test.GetPutField.
boolean oldmode = setBlockDataMode(false);
out.write(prim_field_data);
for (int i = 0; i < objs.length; ++ i)
out.writeObject(objs[i]);
setBlockDataMode(oldmode);
}
private void checkType(ObjectStreamField field, char type)
throws IllegalArgumentException
{
if (TypeSignature.getEncodingOfClass(field.getType()).charAt(0)
!= type)
throw new IllegalArgumentException();
}
};
// end PutFieldImpl
return currentPutField; return currentPutField;
} }
...@@ -1016,11 +1027,8 @@ public class ObjectOutputStream extends OutputStream ...@@ -1016,11 +1027,8 @@ public class ObjectOutputStream extends OutputStream
if (currentPutField == null) if (currentPutField == null)
throw new NotActiveException("writeFields can only be called after putFields has been called"); throw new NotActiveException("writeFields can only be called after putFields has been called");
// putFields may be called more than once, but not writeFields.
markFieldsWritten(); markFieldsWritten();
currentPutField.write(this); currentPutField.write(this);
currentPutField = null;
} }
...@@ -1210,6 +1218,7 @@ public class ObjectOutputStream extends OutputStream ...@@ -1210,6 +1218,7 @@ public class ObjectOutputStream extends OutputStream
throws IOException throws IOException
{ {
Class klass = osc.forClass(); Class klass = osc.forClass();
currentPutField = null;
try try
{ {
Class classArgs[] = {ObjectOutputStream.class}; Class classArgs[] = {ObjectOutputStream.class};
...@@ -1255,6 +1264,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1255,6 +1264,15 @@ public class ObjectOutputStream extends OutputStream
boolean b = f.getBoolean(obj); boolean b = f.getBoolean(obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1270,6 +1288,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1270,6 +1288,15 @@ public class ObjectOutputStream extends OutputStream
byte b = f.getByte (obj); byte b = f.getByte (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1285,6 +1312,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1285,6 +1312,15 @@ public class ObjectOutputStream extends OutputStream
char b = f.getChar (obj); char b = f.getChar (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1300,6 +1336,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1300,6 +1336,15 @@ public class ObjectOutputStream extends OutputStream
double b = f.getDouble (obj); double b = f.getDouble (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1315,6 +1360,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1315,6 +1360,15 @@ public class ObjectOutputStream extends OutputStream
float b = f.getFloat (obj); float b = f.getFloat (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1330,6 +1384,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1330,6 +1384,15 @@ public class ObjectOutputStream extends OutputStream
int b = f.getInt (obj); int b = f.getInt (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1345,6 +1408,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1345,6 +1408,15 @@ public class ObjectOutputStream extends OutputStream
long b = f.getLong (obj); long b = f.getLong (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1360,6 +1432,15 @@ public class ObjectOutputStream extends OutputStream ...@@ -1360,6 +1432,15 @@ public class ObjectOutputStream extends OutputStream
short b = f.getShort (obj); short b = f.getShort (obj);
return b; return b;
} }
catch (IllegalArgumentException _)
{
throw new InvalidClassException
("invalid requested type for field " + field_name + " in class " + klass.getName());
}
catch (IOException e)
{
throw e;
}
catch (Exception _) catch (Exception _)
{ {
throw new IOException("Unexpected exception " + _); throw new IOException("Unexpected exception " + _);
...@@ -1372,10 +1453,21 @@ public class ObjectOutputStream extends OutputStream ...@@ -1372,10 +1453,21 @@ public class ObjectOutputStream extends OutputStream
try try
{ {
Field f = getField (klass, field_name); Field f = getField (klass, field_name);
ObjectStreamField of = new ObjectStreamField(f.getName(), f.getType());
if (of.getTypeString() == null ||
!of.getTypeString().equals(type_code))
throw new InvalidClassException
("invalid type code for " + field_name + " in class " + klass.getName());
Object o = f.get (obj); Object o = f.get (obj);
// FIXME: We should check the type_code here // FIXME: We should check the type_code here
return o; return o;
} }
catch (IOException e)
{
throw e;
}
catch (Exception e) catch (Exception e)
{ {
throw new IOException (); throw new IOException ();
...@@ -1383,18 +1475,26 @@ public class ObjectOutputStream extends OutputStream ...@@ -1383,18 +1475,26 @@ public class ObjectOutputStream extends OutputStream
} }
private static Field getField (Class klass, String name) private static Field getField (Class klass, String name)
throws java.lang.NoSuchFieldException throws java.io.InvalidClassException
{ {
final Field f = klass.getDeclaredField(name); try
AccessController.doPrivileged(new PrivilegedAction()
{ {
public Object run() final Field f = klass.getDeclaredField(name);
{ AccessController.doPrivileged(new PrivilegedAction()
f.setAccessible(true); {
return null; public Object run()
} {
}); f.setAccessible(true);
return f; return null;
}
});
return f;
}
catch (java.lang.NoSuchFieldException e)
{
throw new InvalidClassException
("no field called " + name + " in class " + klass.getName());
}
} }
private static Method getMethod (Class klass, String name, Class[] args) private static Method getMethod (Class klass, String name, Class[] args)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment