/[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.20 by mark, Tue Jan 22 22:26:59 2002 UTC revision 1.21 by cbj, Mon Mar 25 05:12:18 2002 UTC
# Line 168  public class ObjectOutputStream extends Line 168  public class ObjectOutputStream extends
168    public final void writeObject (Object obj) throws IOException    public final void writeObject (Object obj) throws IOException
169    {    {
170      if (useSubclassMethod)      if (useSubclassMethod)
171      {        {
172        writeObjectOverride (obj);          writeObjectOverride (obj);
173        return;          return;
174      }        }
175        
176      boolean was_serializing = isSerializing;      boolean was_serializing = isSerializing;
177        
178      if (! was_serializing)      boolean old_mode = setBlockDataMode (false);
179        setBlockDataMode (false);      
   
180      try      try
     {  
       isSerializing = true;  
       boolean replaceDone = false;  
   
       drain ();  
   
       while (true)  
181        {        {
182          if (obj == null)          isSerializing = true;
183          {          boolean replaceDone = false;
184            realOutput.writeByte (TC_NULL);          
           break;  
         }  
   
         Integer handle = findHandle (obj);  
         if (handle != null)  
         {  
           realOutput.writeByte (TC_REFERENCE);  
           realOutput.writeInt (handle.intValue ());  
           break;  
         }  
   
         if (obj instanceof Class)  
         {  
           realOutput.writeByte (TC_CLASS);  
           writeObject (ObjectStreamClass.lookup ((Class)obj));  
           assignNewHandle (obj);  
           break;  
         }  
   
         if (obj instanceof ObjectStreamClass)  
         {  
           ObjectStreamClass osc = (ObjectStreamClass)obj;  
           realOutput.writeByte (TC_CLASSDESC);  
           realOutput.writeUTF (osc.getName ());  
           realOutput.writeLong (osc.getSerialVersionUID ());  
           assignNewHandle (obj);  
   
           int flags = osc.getFlags ();  
   
           if (protocolVersion == PROTOCOL_VERSION_2  
               && osc.isExternalizable ())  
             flags |= SC_BLOCK_DATA;  
   
           realOutput.writeByte (flags);  
   
           ObjectStreamField[] fields = osc.fields;  
           realOutput.writeShort (fields.length);  
   
           ObjectStreamField field;  
           for (int i=0; i < fields.length; i++)  
           {  
             field = fields[i];  
             realOutput.writeByte (field.getTypeCode ());  
             realOutput.writeUTF (field.getName ());  
   
             if (! field.isPrimitive ())  
               writeObject (field.getTypeString ());  
           }  
   
           setBlockDataMode (true);  
           annotateClass (osc.forClass ());  
           setBlockDataMode (false);  
           realOutput.writeByte (TC_ENDBLOCKDATA);  
   
           if (osc.isSerializable ())  
             writeObject (osc.getSuper ());  
           else  
             writeObject (null);  
           break;  
         }  
   
   
185          Object replacedObject = null;          Object replacedObject = null;
186            
187          if ((replacementEnabled || obj instanceof Serializable)          while (true)
188              && ! replaceDone)            {
189          {              if (obj == null)
           replacedObject = obj;  
   
           if (obj instanceof Serializable)  
             {  
               Method m = null;  
               try  
               {  
                 Class classArgs[] = {};  
                 m = obj.getClass ().getDeclaredMethod ("writeReplace",  
                                                        classArgs);  
                 // m can't be null by definition since an exception would  
                 // have been thrown so a check for null is not needed.  
                 obj = m.invoke (obj, new Object[] {});  
               }  
               catch (NoSuchMethodException ignore)  
190                {                {
191                    realOutput.writeByte (TC_NULL);
192                    break;
193                }                }
194                catch (IllegalAccessException ignore)              
195                Integer handle = findHandle (obj);
196                if (handle != null)
197                {                {
198                    realOutput.writeByte (TC_REFERENCE);
199                    realOutput.writeInt (handle.intValue ());
200                    break;
201                }                }
202                catch (InvocationTargetException ignore)              
203                if (obj instanceof Class)
204                {                {
205                    Class cl = (Class)obj;
206                    ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (cl);
207                    assignNewHandle (obj);
208                    realOutput.writeByte (TC_CLASS);
209                    if (!osc.isProxyClass ())
210                      {
211                        writeObject (osc);
212                      }
213                    else
214                      {
215                        realOutput.writeByte (TC_PROXYCLASSDESC);
216                        Class[] intfs = cl.getInterfaces();
217                        realOutput.writeInt(intfs.length);
218                        for (int i = 0; i < intfs.length; i++)
219                          realOutput.writeUTF(intfs[i].getName());
220                        
221                        boolean oldmode = setBlockDataMode (true);
222                        annotateProxyClass(cl);
223                        setBlockDataMode (oldmode);
224                        realOutput.writeByte(TC_ENDBLOCKDATA);
225                        
226                        writeObject (osc.getSuper());
227                      }
228                    break;
229                }                }
             }  
230    
           if (replacementEnabled)  
             obj = replaceObject (obj);  
231    
232            replaceDone = true;              if (obj instanceof ObjectStreamClass)
233            continue;                {
234          }                  ObjectStreamClass osc = (ObjectStreamClass)obj;
235                    realOutput.writeByte (TC_CLASSDESC);
236                    realOutput.writeUTF (osc.getName ());
237                    realOutput.writeLong (osc.getSerialVersionUID ());
238                    assignNewHandle (obj);
239    
240                    int flags = osc.getFlags ();
241    
242                    if (protocolVersion == PROTOCOL_VERSION_2
243                        && osc.isExternalizable ())
244                      flags |= SC_BLOCK_DATA;
245    
246                    realOutput.writeByte (flags);
247                    
248                    ObjectStreamField[] fields = osc.fields;
249                    realOutput.writeShort (fields.length);
250    
251                    ObjectStreamField field;
252                    for (int i=0; i < fields.length; i++)
253                      {
254                        field = fields[i];
255                        realOutput.writeByte (field.getTypeCode ());
256                        realOutput.writeUTF (field.getName ());
257    
258                        if (! field.isPrimitive ())
259                          writeObject (field.getTypeString ());
260                      }
261    
262                    boolean oldmode = setBlockDataMode (true);
263                    annotateClass (osc.forClass ());
264                    setBlockDataMode (oldmode);
265                    realOutput.writeByte (TC_ENDBLOCKDATA);
266    
267                    if (osc.isSerializable ())
268                      writeObject (osc.getSuper ());
269                    else
270                      writeObject (null);
271                    break;
272                  }
273    
         if (obj instanceof String)  
         {  
           realOutput.writeByte (TC_STRING);  
           assignNewHandle (obj);  
           realOutput.writeUTF ((String)obj);  
           break;  
         }  
274    
275          Class clazz = obj.getClass ();              if ((replacementEnabled || obj instanceof Serializable)
276          ObjectStreamClass osc = ObjectStreamClass.lookup (clazz);                  && ! replaceDone)
277          if (osc == null)                {
278            throw new NotSerializableException (clazz.getName ());                  replacedObject = obj;
279    
280          if (clazz.isArray ())                  if (obj instanceof Serializable)
281          {                    {
282            realOutput.writeByte (TC_ARRAY);                      Method m = null;
283            writeObject (osc);                      try
284            assignNewHandle (obj);                        {
285            writeArraySizeAndElements (obj, clazz.getComponentType ());                          Class classArgs[] = {};
286            break;                          m = obj.getClass ().getDeclaredMethod ("writeReplace",
287          }                                                                 classArgs);
288                            // m can't be null by definition since an exception would
289                            // have been thrown so a check for null is not needed.
290                            obj = m.invoke (obj, new Object[] {});
291                          }
292                        catch (NoSuchMethodException ignore)
293                          {
294                          }
295                        catch (IllegalAccessException ignore)
296                          {
297                          }
298                        catch (InvocationTargetException ignore)
299                          {
300                          }
301                      }
302                    
303                    if (replacementEnabled)
304                      obj = replaceObject (obj);
305                    
306                    replaceDone = true;
307                    continue;
308                  }
309    
310          realOutput.writeByte (TC_OBJECT);              if (obj instanceof String)
311          writeObject (osc);                {
312                    realOutput.writeByte (TC_STRING);
313                    assignNewHandle (obj);
314                    realOutput.writeUTF ((String)obj);
315                    break;
316                  }
317    
318          if (replaceDone)              Class clazz = obj.getClass ();
319            assignNewHandle (replacedObject);              ObjectStreamClass osc = ObjectStreamClass.lookupForClassObject (clazz);
320          else              if (osc == null)
321            assignNewHandle (obj);                throw new NotSerializableException (clazz.getName ());
322    
323          if (obj instanceof Externalizable)              if (clazz.isArray ())
324          {                {
325            if (protocolVersion == PROTOCOL_VERSION_2)                  realOutput.writeByte (TC_ARRAY);
326              setBlockDataMode (true);                  writeObject (osc);
327                    assignNewHandle (obj);
328                    writeArraySizeAndElements (obj, clazz.getComponentType ());
329                    break;
330                  }
331    
332            ((Externalizable)obj).writeExternal (this);              realOutput.writeByte (TC_OBJECT);
333                writeObject (osc);
334    
335            if (protocolVersion == PROTOCOL_VERSION_2)              if (replaceDone)
336            {                assignNewHandle (replacedObject);
337              setBlockDataMode (false);              else
338              drain ();                assignNewHandle (obj);
           }  
339    
340            break;              if (obj instanceof Externalizable)
341          }                {
342                    if (protocolVersion == PROTOCOL_VERSION_2)
343                      setBlockDataMode (true);
344    
345          if (obj instanceof Serializable)                  ((Externalizable)obj).writeExternal (this);
         {  
           currentObject = obj;  
           ObjectStreamClass[] hierarchy =  
             ObjectStreamClass.getObjectStreamClasses (clazz);  
346    
347            boolean has_write;                  if (protocolVersion == PROTOCOL_VERSION_2)
348            for (int i=0; i < hierarchy.length; i++)                    {
349            {                      setBlockDataMode (false);
350              currentObjectStreamClass = hierarchy[i];                      realOutput.writeByte (TC_ENDBLOCKDATA);
351                      }
352                    
353                    break;
354                  }
355    
356              fieldsAlreadyWritten = false;              if (obj instanceof Serializable)
357              has_write = currentObjectStreamClass.hasWriteMethod ();                {
358                    currentObject = obj;
359                    ObjectStreamClass[] hierarchy =
360                      ObjectStreamClass.getObjectStreamClasses (clazz);
361    
362                    boolean has_write;
363                    for (int i=0; i < hierarchy.length; i++)
364                      {
365                        currentObjectStreamClass = hierarchy[i];
366    
367                        fieldsAlreadyWritten = false;
368                        has_write = currentObjectStreamClass.hasWriteMethod ();
369    
370                        writeFields (obj, currentObjectStreamClass.fields,
371                                     has_write);
372    
373    //                  if (has_write)
374    //                    {
375    //                      drain ();
376    //                      realOutput.writeByte (TC_ENDBLOCKDATA);
377    //                    }
378                      }
379    
380                    currentObject = null;
381                    currentObjectStreamClass = null;
382                    currentPutField = null;
383                    break;
384                  }
385    
386              writeFields (obj, currentObjectStreamClass.fields,              throw new NotSerializableException (clazz.getName ());
387                           has_write);            } // end pseudo-loop
388          }
389        catch (IOException e)
390          {
391            realOutput.writeByte (TC_EXCEPTION);
392            reset (true);
393    
394              if (has_write)          setBlockDataMode (false); // ??
395              {          try
396                drain ();            {
397                realOutput.writeByte (TC_ENDBLOCKDATA);              writeObject (e);
398              }            }
399            catch (IOException ioe)
400              {
401                throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception was being written to stream.");
402            }            }
403    
404            currentObject = null;          reset (true);
           currentObjectStreamClass = null;  
           currentPutField = null;  
           break;  
         }  
   
         throw new NotSerializableException (clazz.getName ());  
       } // end pseudo-loop  
405      }      }
406      catch (IOException e)      finally
     {  
       realOutput.writeByte (TC_EXCEPTION);  
       reset (true);  
   
       try  
       {  
         writeObject (e);  
       }  
       catch (IOException ioe)  
407        {        {
408          throw new StreamCorruptedException ("Exception " + ioe + " thrown while exception was being written to stream.");          isSerializing = was_serializing;
409            
410            setBlockDataMode (old_mode);
411        }        }
   
       reset (true);  
     }  
     finally  
     {  
       isSerializing = was_serializing;  
   
       if (! was_serializing)  
         setBlockDataMode (true);  
     }  
412    }    }
413    
414    
# Line 529  public class ObjectOutputStream extends Line 547  public class ObjectOutputStream extends
547    protected void annotateClass (Class cl) throws IOException    protected void annotateClass (Class cl) throws IOException
548    {}    {}
549    
550      protected void annotateProxyClass(Class cl) throws IOException
551      {}
552    
553    /**    /**
554       Allows subclasses to replace objects that are written to the       Allows subclasses to replace objects that are written to the
# Line 703  public class ObjectOutputStream extends Line 723  public class ObjectOutputStream extends
723      if (blockDataCount == 0)      if (blockDataCount == 0)
724        return;        return;
725    
726      writeBlockDataHeader (blockDataCount);      if (writeDataAsBlocks) // ??
727          writeBlockDataHeader (blockDataCount);
728      realOutput.write (blockData, 0, blockDataCount);      realOutput.write (blockData, 0, blockDataCount);
729      blockDataCount = 0;      blockDataCount = 0;
730    }    }
# Line 714  public class ObjectOutputStream extends Line 735  public class ObjectOutputStream extends
735    */    */
736    public void close () throws IOException    public void close () throws IOException
737    {    {
738      drain ();      flush ();
739      realOutput.close ();      realOutput.close ();
740    }    }
741    
# Line 724  public class ObjectOutputStream extends Line 745  public class ObjectOutputStream extends
745    */    */
746    public void writeBoolean (boolean data) throws IOException    public void writeBoolean (boolean data) throws IOException
747    {    {
748      dataOutput.writeBoolean (data);      blockDataOutput.writeBoolean (data);
749    }    }
750    
751    
# Line 733  public class ObjectOutputStream extends Line 754  public class ObjectOutputStream extends
754    */    */
755    public void writeByte (int data) throws IOException    public void writeByte (int data) throws IOException
756    {    {
757      dataOutput.writeByte (data);      blockDataOutput.writeByte (data);
758    }    }
759    
760    
# Line 742  public class ObjectOutputStream extends Line 763  public class ObjectOutputStream extends
763    */    */
764    public void writeShort (int data) throws IOException    public void writeShort (int data) throws IOException
765    {    {
766      dataOutput.writeShort (data);      blockDataOutput.writeShort (data);
767    }    }
768    
769    
# Line 751  public class ObjectOutputStream extends Line 772  public class ObjectOutputStream extends
772    */    */
773    public void writeChar (int data) throws IOException    public void writeChar (int data) throws IOException
774    {    {
775      dataOutput.writeChar (data);      blockDataOutput.writeChar (data);
776    }    }
777    
778    
# Line 760  public class ObjectOutputStream extends Line 781  public class ObjectOutputStream extends
781    */    */
782    public void writeInt (int data) throws IOException    public void writeInt (int data) throws IOException
783    {    {
784      dataOutput.writeInt (data);      blockDataOutput.writeInt (data);
785    }    }
786    
787    
# Line 769  public class ObjectOutputStream extends Line 790  public class ObjectOutputStream extends
790    */    */
791    public void writeLong (long data) throws IOException    public void writeLong (long data) throws IOException
792    {    {
793      dataOutput.writeLong (data);      blockDataOutput.writeLong (data);
794    }    }
795    
796    
# Line 778  public class ObjectOutputStream extends Line 799  public class ObjectOutputStream extends
799    */    */
800    public void writeFloat (float data) throws IOException    public void writeFloat (float data) throws IOException
801    {    {
802      dataOutput.writeFloat (data);      blockDataOutput.writeFloat (data);
803    }    }
804    
805    
# Line 787  public class ObjectOutputStream extends Line 808  public class ObjectOutputStream extends
808    */    */
809    public void writeDouble (double data) throws IOException    public void writeDouble (double data) throws IOException
810    {    {
811      dataOutput.writeDouble (data);      blockDataOutput.writeDouble (data);
812    }    }
813    
814    
# Line 796  public class ObjectOutputStream extends Line 817  public class ObjectOutputStream extends
817    */    */
818    public void writeBytes (String data) throws IOException    public void writeBytes (String data) throws IOException
819    {    {
820      dataOutput.writeBytes (data);      blockDataOutput.writeBytes (data);
821    }    }
822    
823    
# Line 966  public class ObjectOutputStream extends Line 987  public class ObjectOutputStream extends
987            {            {
988              ObjectStreamField field              ObjectStreamField field
989                = currentObjectStreamClass.getField (name);                = currentObjectStreamClass.getField (name);
990                if (field == null)
991                  throw new IllegalArgumentException ();
992                
993              if (value != null &&              if (value != null &&
994                  ! field.getType ().isAssignableFrom (value.getClass ()))                  ! field.getType ().isAssignableFrom (value.getClass ()))
995                throw new IllegalArgumentException ();                throw new IllegalArgumentException ();
# Line 977  public class ObjectOutputStream extends Line 1001  public class ObjectOutputStream extends
1001              // Apparently Block data is not used with PutField as per              // Apparently Block data is not used with PutField as per
1002              // empirical evidence against JDK 1.2.  Also see Mauve test              // empirical evidence against JDK 1.2.  Also see Mauve test
1003              // java.io.ObjectInputOutput.Test.GetPutField.              // java.io.ObjectInputOutput.Test.GetPutField.
1004              setBlockDataMode (false);              boolean oldmode = setBlockDataMode (false);
1005              out.write (prim_field_data);              out.write (prim_field_data);
1006              for (int i = 0; i < objs.length; ++ i)              for (int i = 0; i < objs.length; ++ i)
1007                out.writeObject (objs[i]);                out.writeObject (objs[i]);
1008              setBlockDataMode (true);              setBlockDataMode (oldmode);
1009            }            }
1010    
1011          private void checkType (ObjectStreamField field, char type)          private void checkType (ObjectStreamField field, char type)
# Line 1068  public class ObjectOutputStream extends Line 1092  public class ObjectOutputStream extends
1092        {        {
1093          byte[] cast_array = (byte[])array;          byte[] cast_array = (byte[])array;
1094          realOutput.writeInt (length);          realOutput.writeInt (length);
1095          for (int i=0; i < length; i++)          realOutput.write(cast_array, 0, length);
           realOutput.writeByte (cast_array[i]);  
1096          return;          return;
1097        }        }
1098        if (clazz == Character.TYPE)        if (clazz == Character.TYPE)
# Line 1143  public class ObjectOutputStream extends Line 1166  public class ObjectOutputStream extends
1166        setBlockDataMode (true);        setBlockDataMode (true);
1167        callWriteMethod (obj);        callWriteMethod (obj);
1168        setBlockDataMode (false);        setBlockDataMode (false);
1169          realOutput.writeByte (TC_ENDBLOCKDATA);
1170        return;        return;
1171      }      }
1172    
1173        boolean oldmode = setBlockDataMode (false); //??
1174      String field_name;      String field_name;
1175      Class type;      Class type;
1176      for (int i=0; i < fields.length; i++)      for (int i=0; i < fields.length; i++)
# Line 1171  public class ObjectOutputStream extends Line 1196  public class ObjectOutputStream extends
1196          realOutput.writeShort (getShortField (obj, field_name));          realOutput.writeShort (getShortField (obj, field_name));
1197        else        else
1198          writeObject (getObjectField (obj, field_name,          writeObject (getObjectField (obj, field_name,
1199                                       TypeSignature.getEncodingOfClass (type)));                                       fields[i].getTypeString ()));
1200      }      }
1201        setBlockDataMode (oldmode); // ??
1202    }    }
1203    
1204    
1205    // Toggles writing primitive data to block-data buffer.    // Toggles writing primitive data to block-data buffer.
1206    private void setBlockDataMode (boolean on)    private boolean setBlockDataMode (boolean on) throws IOException
1207    {    {
1208        if (on == writeDataAsBlocks)
1209          return on;
1210        
1211        drain();
1212        boolean oldmode = writeDataAsBlocks;
1213        
1214      writeDataAsBlocks = on;      writeDataAsBlocks = on;
1215    
1216      if (on)      if (on)
1217        dataOutput = blockDataOutput;        dataOutput = blockDataOutput;
1218      else      else
1219        dataOutput = realOutput;        dataOutput = realOutput;
1220    
1221        return oldmode;
1222    }    }
1223    
1224    

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.21

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