/[classpath]/classpath/java/io/ObjectOutputStream.java
ViewVC logotype

Diff of /classpath/java/io/ObjectOutputStream.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.39 by tromey, Fri Sep 26 05:39:07 2003 UTC revision 1.40 by glavaux, Tue Dec 2 18:35:52 2003 UTC
# Line 162  public class ObjectOutputStream extends Line 162  public class ObjectOutputStream extends
162     * @exception NotSerializableException An attempt was made to     * @exception NotSerializableException An attempt was made to
163     * serialize an <code>Object</code> that is not serializable.     * serialize an <code>Object</code> that is not serializable.
164     *     *
165       * @exception InvalidClassException Somebody tried to serialize
166       * an object which is wrongly formatted.
167       *
168     * @exception IOException Exception from underlying     * @exception IOException Exception from underlying
169     * <code>OutputStream</code>.     * <code>OutputStream</code>.
170     */     */
# Line 279  public class ObjectOutputStream extends Line 282  public class ObjectOutputStream extends
282              ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (clazz);              ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (clazz);
283              if (osc == null)              if (osc == null)
284                throw new NotSerializableException (clazz.getName ());                throw new NotSerializableException (clazz.getName ());
285                
286              if (clazz.isArray ())              if (clazz.isArray ())
287                {                {
288                  realOutput.writeByte (TC_ARRAY);                  realOutput.writeByte (TC_ARRAY);
# Line 301  public class ObjectOutputStream extends Line 304  public class ObjectOutputStream extends
304                {                {
305                  if (protocolVersion == PROTOCOL_VERSION_2)                  if (protocolVersion == PROTOCOL_VERSION_2)
306                    setBlockDataMode (true);                    setBlockDataMode (true);
307                    
308                  ((Externalizable)obj).writeExternal (this);                  ((Externalizable)obj).writeExternal (this);
309                    
310                  if (protocolVersion == PROTOCOL_VERSION_2)                  if (protocolVersion == PROTOCOL_VERSION_2)
311                    {                    {
312                      setBlockDataMode (false);                      setBlockDataMode (false);
# Line 318  public class ObjectOutputStream extends Line 321  public class ObjectOutputStream extends
321                  currentObject = obj;                  currentObject = obj;
322                  ObjectStreamClass[] hierarchy =                  ObjectStreamClass[] hierarchy =
323                    ObjectStreamClass.getObjectStreamClasses (clazz);                    ObjectStreamClass.getObjectStreamClasses (clazz);
324                    
325                  for (int i=0; i < hierarchy.length; i++)                  for (int i=0; i < hierarchy.length; i++)
326                    {                    {
327                      currentObjectStreamClass = hierarchy[i];                      currentObjectStreamClass = hierarchy[i];
328                        
329                      fieldsAlreadyWritten = false;                      fieldsAlreadyWritten = false;
330                      if (currentObjectStreamClass.hasWriteMethod ())                      if (currentObjectStreamClass.hasWriteMethod ())
331                        {                        {
# Line 361  public class ObjectOutputStream extends Line 364  public class ObjectOutputStream extends
364            }            }
365          catch (IOException ioe)          catch (IOException ioe)
366            {            {
367              throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception ("+e+") was being written to stream.");              throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception was being written to stream.");
368            }            }
369    
370          reset (true);          reset (true);
371            
372        }        }
373      finally      finally
374        {        {
# Line 406  public class ObjectOutputStream extends Line 410  public class ObjectOutputStream extends
410      annotateClass (osc.forClass ());      annotateClass (osc.forClass ());
411      setBlockDataMode (oldmode);      setBlockDataMode (oldmode);
412      realOutput.writeByte (TC_ENDBLOCKDATA);      realOutput.writeByte (TC_ENDBLOCKDATA);
413        
414      if (osc.isSerializable ())      if (osc.isSerializable ())
415        writeObject (osc.getSuper ());        writeObject (osc.getSuper ());
416      else      else
# Line 443  public class ObjectOutputStream extends Line 447  public class ObjectOutputStream extends
447        throw new NotActiveException ("defaultWriteObject called by non-active class and/or object");        throw new NotActiveException ("defaultWriteObject called by non-active class and/or object");
448    
449      if (fieldsAlreadyWritten)      if (fieldsAlreadyWritten)
450        throw new IOException ("Only one of writeFields and defaultWriteObject may be called, and it may only be called once");        throw new IOException ("Only one of putFields and defaultWriteObject may be called, and it may only be called once");
451    
452      fieldsAlreadyWritten = true;      fieldsAlreadyWritten = true;
453    }    }
# Line 505  public class ObjectOutputStream extends Line 509  public class ObjectOutputStream extends
509    {    {
510      if (version != PROTOCOL_VERSION_1 && version != PROTOCOL_VERSION_2)      if (version != PROTOCOL_VERSION_1 && version != PROTOCOL_VERSION_2)
511        throw new IOException ("Invalid protocol version requested.");        throw new IOException ("Invalid protocol version requested.");
512        
513      protocolVersion = version;      protocolVersion = version;
514    }    }
515    
# Line 866  public class ObjectOutputStream extends Line 870  public class ObjectOutputStream extends
870    
871    public PutField putFields () throws IOException    public PutField putFields () throws IOException
872    {    {
873      if (currentPutField == null)      if (currentPutField != null)
874          return currentPutField;
875    
876        markFieldsWritten ();
877    
878        currentPutField = new PutField ()
879        {        {
880          currentPutField = new PutField ()          private byte[] prim_field_data
881            {            = new byte[currentObjectStreamClass.primFieldSize];
882              private byte[] prim_field_data =          private Object[] objs
883                new byte[currentObjectStreamClass.primFieldSize];            = new Object[currentObjectStreamClass.objectFieldCount];
             private Object[] objs =  
               new Object[currentObjectStreamClass.objectFieldCount];  
884    
885              public void put (String name, boolean value)          private ObjectStreamField getField (String name)
886              {          {
887                ObjectStreamField field            ObjectStreamField field
888                  = currentObjectStreamClass.getField (name);              = currentObjectStreamClass.getField (name);
889                checkType (field, 'Z');            
890                prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0);            if (field == null)
891              }              throw new IllegalArgumentException("no such serializable field " + name);
892              
893              return field;
894            }
895    
896              public void put (String name, byte value)          public void put (String name, boolean value)
897              {          {
898                ObjectStreamField field            ObjectStreamField field = getField (name);
                 = currentObjectStreamClass.getField (name);  
               checkType (field, 'B');  
               prim_field_data[field.getOffset ()] = value;  
             }  
899    
900              public void put (String name, char value)            checkType (field, 'Z');
901              {            prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0);
902                ObjectStreamField field          }
                 = currentObjectStreamClass.getField (name);  
               checkType (field, 'C');  
               int off = field.getOffset ();  
               prim_field_data[off++] = (byte)(value >>> 8);  
               prim_field_data[off] = (byte)value;  
             }  
903    
904              public void put (String name, double value)          public void put (String name, byte value)
905              {          {
906                ObjectStreamField field            ObjectStreamField field = getField (name);
                 = 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;  
             }  
907    
908              public void put (String name, float value)            checkType (field, 'B');
909              {            prim_field_data[field.getOffset ()] = value;
910                ObjectStreamField field          }
                 = 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;  
             }  
911    
912              public void put (String name, int value)          public void put (String name, char value)
913              {          {
914                ObjectStreamField field            ObjectStreamField field = getField (name);
                 = currentObjectStreamClass.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;  
             }  
915    
916              public void put (String name, long value)            checkType (field, 'C');
917              {            int off = field.getOffset ();
918                ObjectStreamField field            prim_field_data[off++] = (byte)(value >>> 8);
919                  = currentObjectStreamClass.getField (name);            prim_field_data[off] = (byte)value;
920                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);  
               prim_field_data[off++] = (byte)(value >>> 32);  
               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;  
             }  
921    
922              public void put (String name, short value)          public void put (String name, double value)
923              {          {
924                ObjectStreamField field            ObjectStreamField field = getField (name);
                 = currentObjectStreamClass.getField (name);  
               checkType (field, 'S');  
               int off = field.getOffset ();  
               prim_field_data[off++] = (byte)(value >>> 8);  
               prim_field_data[off] = (byte)value;  
             }  
925    
926              public void put (String name, Object value)            checkType (field, 'D');
927              {            int off = field.getOffset ();
928                ObjectStreamField field            long l_value = Double.doubleToLongBits (value);
929                  = currentObjectStreamClass.getField (name);            prim_field_data[off++] = (byte)(l_value >>> 52);
930                if (field == null)            prim_field_data[off++] = (byte)(l_value >>> 48);
931                  throw new IllegalArgumentException ();            prim_field_data[off++] = (byte)(l_value >>> 40);
932                if (value != null &&            prim_field_data[off++] = (byte)(l_value >>> 32);
933                    ! field.getType ().isAssignableFrom (value.getClass ()))            prim_field_data[off++] = (byte)(l_value >>> 24);
934                  throw new IllegalArgumentException ();            prim_field_data[off++] = (byte)(l_value >>> 16);
935                objs[field.getOffset ()] = value;            prim_field_data[off++] = (byte)(l_value >>> 8);
936              }            prim_field_data[off] = (byte)l_value;
937            }
938    
939              public void write (ObjectOutput out) throws IOException          public void put (String name, float value)
940              {          {
941                // Apparently Block data is not used with PutField as per            ObjectStreamField field = getField (name);
               // 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);  
             }  
942    
943              private void checkType (ObjectStreamField field, char type)            checkType (field, 'F');
944                throws IllegalArgumentException            int off = field.getOffset ();
945              {            int i_value = Float.floatToIntBits (value);
946                if (TypeSignature.getEncodingOfClass(field.getType ()).charAt(0)            prim_field_data[off++] = (byte)(i_value >>> 24);
947                    != type)            prim_field_data[off++] = (byte)(i_value >>> 16);
948                  throw new IllegalArgumentException ();            prim_field_data[off++] = (byte)(i_value >>> 8);
949              }            prim_field_data[off] = (byte)i_value;
950            };          }
951        }  
952            public void put (String name, int value)
953            {
954              ObjectStreamField field = getField (name);
955              checkType (field, 'I');
956              int off = field.getOffset ();
957              prim_field_data[off++] = (byte)(value >>> 24);
958              prim_field_data[off++] = (byte)(value >>> 16);
959              prim_field_data[off++] = (byte)(value >>> 8);
960              prim_field_data[off] = (byte)value;
961            }
962    
963            public void put (String name, long value)
964            {
965              ObjectStreamField field = getField (name);
966              checkType (field, 'J');
967              int off = field.getOffset ();
968              prim_field_data[off++] = (byte)(value >>> 52);
969              prim_field_data[off++] = (byte)(value >>> 48);
970              prim_field_data[off++] = (byte)(value >>> 40);
971              prim_field_data[off++] = (byte)(value >>> 32);
972              prim_field_data[off++] = (byte)(value >>> 24);
973              prim_field_data[off++] = (byte)(value >>> 16);
974              prim_field_data[off++] = (byte)(value >>> 8);
975              prim_field_data[off] = (byte)value;
976            }
977    
978            public void put (String name, short value)
979            {
980              ObjectStreamField field = getField (name);
981              checkType (field, 'S');
982              int off = field.getOffset ();
983              prim_field_data[off++] = (byte)(value >>> 8);
984              prim_field_data[off] = (byte)value;
985            }
986    
987            public void put (String name, Object value)
988            {
989              ObjectStreamField field = getField (name);
990    
991              if (value != null &&
992                  ! field.getType ().isAssignableFrom (value.getClass ()))
993                throw new IllegalArgumentException ();
994              objs[field.getOffset ()] = value;
995            }
996    
997            public void write (ObjectOutput out) throws IOException
998            {
999              // Apparently Block data is not used with PutField as per
1000              // empirical evidence against JDK 1.2.  Also see Mauve test
1001              // java.io.ObjectInputOutput.Test.GetPutField.
1002              boolean oldmode = setBlockDataMode (false);
1003              out.write (prim_field_data);
1004              for (int i = 0; i < objs.length; ++ i)
1005                out.writeObject (objs[i]);
1006              setBlockDataMode (oldmode);
1007            }
1008    
1009            private void checkType (ObjectStreamField field, char type)
1010              throws IllegalArgumentException
1011            {
1012              if (TypeSignature.getEncodingOfClass (field.getType ()).charAt (0)
1013                  != type)
1014                throw new IllegalArgumentException ();
1015            }
1016          };
1017        // end PutFieldImpl
1018    
1019      return currentPutField;      return currentPutField;
1020    }    }
# Line 1012  public class ObjectOutputStream extends Line 1025  public class ObjectOutputStream extends
1025      if (currentPutField == null)      if (currentPutField == null)
1026        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");
1027    
     // putFields may be called more than once, but not writeFields.  
     markFieldsWritten();  
   
1028      currentPutField.write (this);      currentPutField.write (this);
     currentPutField = null;  
1029    }    }
1030    
1031    
# Line 1205  public class ObjectOutputStream extends Line 1214  public class ObjectOutputStream extends
1214      throws IOException      throws IOException
1215    {    {
1216      Class klass = osc.forClass();      Class klass = osc.forClass();
1217        currentPutField = null;
1218      try      try
1219        {        {
1220          Class classArgs[] = {ObjectOutputStream.class};          Class classArgs[] = {ObjectOutputStream.class};
# Line 1250  public class ObjectOutputStream extends Line 1260  public class ObjectOutputStream extends
1260          boolean b = f.getBoolean (obj);          boolean b = f.getBoolean (obj);
1261          return b;          return b;
1262        }        }
1263        catch (IllegalArgumentException _)
1264          {
1265            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1266          }
1267        catch (IOException e)
1268          {
1269            throw e;
1270          }
1271      catch (Exception _)      catch (Exception _)
1272        {        {
1273          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1274        }            }
1275    }    }
1276    
1277    private byte getByteField (Object obj, Class klass, String field_name)    private byte getByteField (Object obj, Class klass, String field_name)
# Line 1265  public class ObjectOutputStream extends Line 1283  public class ObjectOutputStream extends
1283          byte b = f.getByte (obj);          byte b = f.getByte (obj);
1284          return b;          return b;
1285        }        }
1286        catch (IllegalArgumentException _)
1287          {
1288            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1289          }
1290        catch (IOException e)
1291          {
1292            throw e;
1293          }
1294      catch (Exception _)      catch (Exception _)
1295        {        {
1296          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1297        }            }    
1298    }    }
1299    
# Line 1280  public class ObjectOutputStream extends Line 1306  public class ObjectOutputStream extends
1306          char b = f.getChar (obj);          char b = f.getChar (obj);
1307          return b;          return b;
1308        }        }
1309        catch (IllegalArgumentException _)
1310          {
1311            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1312          }
1313        catch (IOException e)
1314          {
1315            throw e;
1316          }
1317      catch (Exception _)      catch (Exception _)
1318        {        {
1319          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1320        }            }    
1321    }    }
1322    
# Line 1295  public class ObjectOutputStream extends Line 1329  public class ObjectOutputStream extends
1329          double b = f.getDouble (obj);          double b = f.getDouble (obj);
1330          return b;          return b;
1331        }        }
1332        catch (IllegalArgumentException _)
1333          {
1334            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1335          }
1336        catch (IOException e)
1337          {
1338            throw e;
1339          }
1340      catch (Exception _)      catch (Exception _)
1341        {        {
1342          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1343        }            }    
1344    }    }
1345    
# Line 1310  public class ObjectOutputStream extends Line 1352  public class ObjectOutputStream extends
1352          float b = f.getFloat (obj);          float b = f.getFloat (obj);
1353          return b;          return b;
1354        }        }
1355        catch (IllegalArgumentException _)
1356          {
1357            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1358          }
1359        catch (IOException e)
1360          {
1361            throw e;
1362          }
1363      catch (Exception _)      catch (Exception _)
1364        {        {
1365          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1366        }            }    
1367    }    }
1368    
# Line 1325  public class ObjectOutputStream extends Line 1375  public class ObjectOutputStream extends
1375          int b = f.getInt (obj);          int b = f.getInt (obj);
1376          return b;          return b;
1377        }        }
1378        catch (IllegalArgumentException _)
1379          {
1380            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1381          }
1382        catch (IOException e)
1383          {
1384            throw e;
1385          }
1386      catch (Exception _)      catch (Exception _)
1387        {        {
1388          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1389        }            }    
1390    }    }
1391    
# Line 1340  public class ObjectOutputStream extends Line 1398  public class ObjectOutputStream extends
1398          long b = f.getLong (obj);          long b = f.getLong (obj);
1399          return b;          return b;
1400        }        }
1401        catch (IllegalArgumentException _)
1402          {
1403            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1404          }
1405        catch (IOException e)
1406          {
1407            throw e;
1408          }
1409      catch (Exception _)      catch (Exception _)
1410        {        {
1411          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1412        }            }    
1413    }    }
1414    
# Line 1355  public class ObjectOutputStream extends Line 1421  public class ObjectOutputStream extends
1421          short b = f.getShort (obj);          short b = f.getShort (obj);
1422          return b;          return b;
1423        }        }
1424        catch (IllegalArgumentException _)
1425          {
1426            throw new InvalidClassException("invalid requested type for field " + field_name + " in class " + klass.getName());
1427          }
1428        catch (IOException e)
1429          {
1430           throw e;
1431          }
1432      catch (Exception _)      catch (Exception _)
1433        {        {
1434          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1435        }            }    
1436    }    }
1437    
# Line 1367  public class ObjectOutputStream extends Line 1441  public class ObjectOutputStream extends
1441      try      try
1442        {        {
1443          Field f = getField (klass, field_name);          Field f = getField (klass, field_name);
1444            ObjectStreamField of = new ObjectStreamField(f.getName(), f.getType());
1445    
1446            if (of.getTypeString() == null ||
1447                !of.getTypeString().equals(type_code))
1448              throw new InvalidClassException("invalid type code for " + field_name + " in class " + klass.getName());
1449    
1450          Object o = f.get (obj);          Object o = f.get (obj);
1451          // FIXME: We should check the type_code here          // FIXME: We should check the type_code here
1452          return o;          return o;
1453        }        }
1454      catch (Exception _)      catch (IOException e)
1455          {
1456            throw e;
1457          }
1458        catch (Exception e)
1459        {        {
1460          throw new IOException ("Unexpected Exception "+_);          throw new IOException ();
1461        }            }    
1462    }    }
1463    
1464    private static Field getField (Class klass, String name)    private static Field getField (Class klass, String name)
1465      throws java.lang.NoSuchFieldException      throws java.io.InvalidClassException
1466    {    {
1467      final Field f = klass.getDeclaredField(name);      try
     AccessController.doPrivileged(new PrivilegedAction()  
1468        {        {
1469          public Object run()          final Field f = klass.getDeclaredField(name);
1470          {          AccessController.doPrivileged(new PrivilegedAction()
1471            f.setAccessible(true);            {
1472            return null;              public Object run()
1473          }              {
1474        });                f.setAccessible(true);
1475      return f;                return null;
1476                }
1477              });
1478            return f;
1479          }
1480        catch (java.lang.NoSuchFieldException e)
1481          {
1482            throw new InvalidClassException ("no field called " + name + " in class " + klass.getName());
1483          }
1484    }    }
1485    
1486    private static Method getMethod (Class klass, String name, Class[] args)    private static Method getMethod (Class klass, String name, Class[] args)

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.40

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26