This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: Patch: java.io.ObjectOutputStream


Hi list,


I commited the attached patch to merge java.io.ObjectOutputStream with 
classpath again. The patch was made by Guilhem Lavaux.


Michael
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2504
diff -u -b -B -r1.2504 ChangeLog
--- ChangeLog	31 Dec 2003 10:55:37 -0000	1.2504
+++ ChangeLog	31 Dec 2003 11:03:36 -0000
@@ -1,3 +1,25 @@
+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>
 
 	* java/net/URL.java
Index: java/io/ObjectOutputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ObjectOutputStream.java,v
retrieving revision 1.23
diff -u -b -B -r1.23 ObjectOutputStream.java
--- java/io/ObjectOutputStream.java	30 Dec 2003 15:51:15 -0000	1.23
+++ java/io/ObjectOutputStream.java	31 Dec 2003 11:03:36 -0000
@@ -162,6 +162,9 @@
    * @exception NotSerializableException An attempt was made to
    * 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
    * <code>OutputStream</code>.
    */
@@ -447,7 +450,7 @@
 
     if (fieldsAlreadyWritten)
       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;
   }
@@ -870,47 +873,59 @@
 
   public PutField putFields() throws IOException
   {
-    if (currentPutField == null)
-      {
-	currentPutField = new PutField ()
+    if (currentPutField != null)
+      return currentPutField;
+
+    currentPutField = new PutField()
 	  {
-	    private byte[] prim_field_data =
-	      new byte[currentObjectStreamClass.primFieldSize];
-	    private Object[] objs =
-	      new Object[currentObjectStreamClass.objectFieldCount];
+	private byte[] prim_field_data
+	  = new byte[currentObjectStreamClass.primFieldSize];
+	private Object[] objs
+	  = new Object[currentObjectStreamClass.objectFieldCount];
 
-	    public void put (String name, boolean value)
+	private ObjectStreamField getField (String name)
 	    {
 	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'Z');
+	    = currentObjectStreamClass.getField(name);
+	  
+	  if (field == null)
+	    throw new IllegalArgumentException("no such serializable field " + name);
+	  
+	  return field;
+	}
+	
+	public void put(String name, boolean value)
+	{
+	  ObjectStreamField field = getField(name);
+
+	  checkType(field, 'Z');
 	      prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0);
 	    }
 
-	    public void put (String name, byte value)
+	public void put(String name, byte value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'B');
-	      prim_field_data[field.getOffset ()] = value;
+	  ObjectStreamField field = getField(name);
+
+	  checkType(field, 'B');
+	  prim_field_data[field.getOffset()] = value;
 	    }
 
-	    public void put (String name, char value)
+	public void put(String name, char value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'C');
-	      int off = field.getOffset ();
+	  ObjectStreamField field = 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)
+	public void put(String name, double value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'D');
-	      int off = field.getOffset ();
+	  ObjectStreamField field = 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);
@@ -922,37 +937,35 @@
 	      prim_field_data[off] = (byte)l_value;
 	    }
 
-	    public void put (String name, float value)
+	public void put(String name, float value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'F');
-	      int off = field.getOffset ();
-	      int i_value = Float.floatToIntBits (value);
+	  ObjectStreamField field = 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)
+	public void put(String name, int value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'I');
-	      int off = field.getOffset ();
+	  ObjectStreamField field = getField(name);
+	  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, long value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'J');
-	      int off = field.getOffset ();
+	  ObjectStreamField field = getField(name);
+	  checkType(field, 'J');
+	  int off = field.getOffset();
 	      prim_field_data[off++] = (byte)(value >>> 52);
 	      prim_field_data[off++] = (byte)(value >>> 48);
 	      prim_field_data[off++] = (byte)(value >>> 40);
@@ -963,49 +976,47 @@
 	      prim_field_data[off] = (byte)value;
 	    }
 
-	    public void put (String name, short value)
+	public void put(String name, short value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      checkType (field, 'S');
-	      int off = field.getOffset ();
+	  ObjectStreamField field = getField(name);
+	  checkType(field, 'S');
+	  int off = field.getOffset();
 	      prim_field_data[off++] = (byte)(value >>> 8);
 	      prim_field_data[off] = (byte)value;
 	    }
 
-	    public void put (String name, Object value)
+	public void put(String name, Object value)
 	    {
-	      ObjectStreamField field
-		= currentObjectStreamClass.getField (name);
-	      if (field == null)
-		throw new IllegalArgumentException ();
+	  ObjectStreamField field = getField(name);
+
 	      if (value != null &&
-		  ! field.getType ().isAssignableFrom (value.getClass ()))
-		throw new IllegalArgumentException ();
-	      objs[field.getOffset ()] = value;
+	      ! 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
+	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);
+	  boolean oldmode = setBlockDataMode(false);
+	  out.write(prim_field_data);
 	      for (int i = 0; i < objs.length; ++ i)
-		out.writeObject (objs[i]);
-	      setBlockDataMode (oldmode);
+	    out.writeObject(objs[i]);
+	  setBlockDataMode(oldmode);
 	    }
 
-	    private void checkType (ObjectStreamField field, char type)
+	private void checkType(ObjectStreamField field, char type)
 	      throws IllegalArgumentException
 	    {
-	      if (TypeSignature.getEncodingOfClass(field.getType ()).charAt(0)
+	  if (TypeSignature.getEncodingOfClass(field.getType()).charAt(0)
 		  != type)
-		throw new IllegalArgumentException ();
+	    throw new IllegalArgumentException();
 	    }
 	  };
-      }
+    // end PutFieldImpl
 
     return currentPutField;
   }
@@ -1016,11 +1027,8 @@
     if (currentPutField == null)
       throw new NotActiveException("writeFields can only be called after putFields has been called");
 
-    // putFields may be called more than once, but not writeFields.
     markFieldsWritten();
-
     currentPutField.write(this);
-    currentPutField = null;
   }
 
 
@@ -1210,6 +1218,7 @@
     throws IOException
   {
     Class klass = osc.forClass();
+    currentPutField = null;
     try
       {
 	Class classArgs[] = {ObjectOutputStream.class};
@@ -1255,6 +1264,15 @@
 	boolean b = f.getBoolean(obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1270,6 +1288,15 @@
 	byte b = f.getByte (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1285,6 +1312,15 @@
 	char b = f.getChar (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1300,6 +1336,15 @@
 	double b = f.getDouble (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1315,6 +1360,15 @@
 	float b = f.getFloat (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1330,6 +1384,15 @@
 	int b = f.getInt (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1345,6 +1408,15 @@
 	long b = f.getLong (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1360,6 +1432,15 @@
 	short b = f.getShort (obj);
 	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 _)
       {
 	throw new IOException("Unexpected exception " + _);
@@ -1372,10 +1453,21 @@
     try
       {
 	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);
 	// FIXME: We should check the type_code here
 	return o;
       }
+    catch (IOException e)
+      {
+	throw e;
+      }
     catch (Exception e)
       {
 	throw new IOException ();
@@ -1383,7 +1475,9 @@
   }
 
   private static Field getField (Class klass, String name)
-    throws java.lang.NoSuchFieldException
+    throws java.io.InvalidClassException
+  {
+    try
   {
     final Field f = klass.getDeclaredField(name);
     AccessController.doPrivileged(new PrivilegedAction()
@@ -1395,6 +1489,12 @@
 	}
       });
     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)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]