summaryrefslogtreecommitdiff
path: root/java/io/ObjectInputStream.java
blob: 0a46252ad0e3327e3f41ea903f11810a5c666cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*************************************************************************
/* ObjectInputStream.java -- Class used to read serialized objects 
/*
/* Copyright (c) 1998 by Geoffrey C. Berry (gcb@cs.duke.edu)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU Library General Public License as published 
/* by the Free Software Foundation, version 2. (see COPYING.LIB)
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
/*************************************************************************/

package java.io;

import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.Hashtable;

import gnu.java.io.ObjectIdentityWrapper;
import gnu.java.lang.reflect.TypeSignature;


/** 
   An <code>ObjectInputStream</code> can be used to read objects
   as well as primitive data in a platform-independent manner from an
   <code>InputStream</code>.

   The data produced by an <code>ObjectOutputStream</code> can be read
   and reconstituted by an <code>ObjectInputStream</code>.

   <code>readObject(Object)</code> is used to read Objects, the
   <code>read&lt;type&gt;</code> methods are used to read primitive
   data (as in <code>DataInputStream</code>). Strings can be read
   as objects or as primitive data, depending on how they were
   written.

   Example usage:
     <pre>
     Hashtable map = new Hashtable();
     map.put( "one", new Integer( 1 ) );
     map.put( "two", new Integer( 2 ) );

     ObjectOutputStream oos =
       new ObjectOutputStream( new FileOutputStream( "numbers" ) );
     oos.writeObject( map );
     oos.close();

     ObjectInputStream ois =
       new ObjectInputStream( new FileInputStream( "numbers" ) );
     Hashtable newmap = (Hashtable)ois.readObject();
     
     System.out.println( newmap );
     </pre>

   The default deserialization can be overriden in two ways.

   By defining a method <code>private void
   readObject(ObjectInputStream)</code>, a class can dictate exactly
   how information about itself is reconstituted.
   <code>defaultReadObject()</code> may be called from this method to
   carry out default deserialization.  This method is not
   responsible for dealing with fields of super-classes or subclasses.

   By implementing <code>java.io.Externalizable</code>.  This gives
   the class complete control over the way it is read from the
   stream.  If this approach is used the burden of reading superclass
   and subclass data is transfered to the class implementing
   <code>java.io.Externalizable</code>.

   @see java.io.DataInputStream
   @see java.io.Externalizable
   @see java.io.ObjectOutputStream
   @see java.io.Serializable
   @see TODO: java serialization spec
*/
public class ObjectInputStream extends InputStream
  implements ObjectInput, ObjectStreamConstants
{
  /**
     Creates a new <code>ObjectInputStream</code> that will do all of
     its reading from <code>in</code>.  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
  {
    myResolveEnabled = false;
    myIsDeserializing = false;
    myBlockDataPosition = 0;
    myBlockDataBytes = 0;
    myBlockData = new byte[ BUFFER_SIZE ];
    //DEBUG
    myBlockDataInput = new PrintingDataInputStream(new DataInputStream(this));
    myRealInputStream = new PrintingDataInputStream(new DataInputStream(in));
    //eDEBUG
    myNextOID = baseWireHandle;
    myObjectLookupTable = new Hashtable();
    setBlockDataMode( true );
    readStreamHeader();
  }


  /**
     Returns the next deserialized object read from the underlying stream.

     This method can be overriden by a class by implementing
     <code>private void readObject(ObjectInputStream)</code>.

     If an exception is thrown from this method, the stream is left in
     an undefined state.

     @exception ClassNotFoundException The class that an object being
     read in belongs to cannot be found.

     @exception IOException Exception from underlying
     <code>InputStream</code>.
  */
  public final Object readObject() throws ClassNotFoundException, IOException
  {
    if( myUseSubclassMethod )
      return readObjectOverride();
    
    boolean was_deserializing;    
    boolean first_time = true;

    Object ret_val;
    while( true )
    {
      //DEBUG
      if( !first_time )
	throw new RuntimeException( "Help me!! I'm in an infinite loop." );
      
      first_time = false;
      //eDEBUG
      
      was_deserializing = myIsDeserializing;

      if( ! was_deserializing )
	setBlockDataMode( false );

      DEBUG( "MARKER " );
      byte marker = myRealInputStream.readByte();

      if( marker == TC_BLOCKDATA || marker == TC_BLOCKDATALONG )
      {
	readNextBlock( marker );
	throw new BlockDataException( myBlockDataBytes );
      }
    
      if( marker == TC_NULL )
      {
	ret_val = null;
	break;
      }
      

      if( marker == TC_REFERENCE )
      {
	DEBUG( "REFERENCE " );
	Integer oid = new Integer( myRealInputStream.readInt() );
	ret_val = ((ObjectIdentityWrapper)
		   myObjectLookupTable.get(oid)).object;
	break;
      }
    
      if( marker == TC_CLASS )
      {
	ObjectStreamClass osc = (ObjectStreamClass)readObject();
	Class clazz = osc.forClass();
	assignNewHandle( clazz );
	ret_val = clazz;
	break;
      }

      if( marker == TC_CLASSDESC )
      {
	DEBUG( "CLASSDESC NAME " );
	String name = myRealInputStream.readUTF();
	DEBUG( "UID " );
	long uid = myRealInputStream.readLong();
	DEBUG( "FLAGS " );
	byte flags = myRealInputStream.readByte();
	DEBUG( "FIELD COUNT " );
	short field_count = myRealInputStream.readShort();
	OSCField[] fields = new OSCField[ field_count ];

	for( int i=0; i < field_count; i++ )
	{
	  DEBUG( "TYPE CODE " );
	  char type_code = (char)myRealInputStream.readByte();
	  DEBUG( "FIELD NAME " );
	  String field_name = myRealInputStream.readUTF();
	  String class_name;

	  if( type_code == 'L' || type_code == '[' )
	    class_name = (String)readObject();
	  else
	    class_name = new String( new char[] { type_code } );
	
	  fields[i] = new OSCField( field_name,
				    TypeSignature.getClassForEncoding
				    ( class_name ) );
	}
      
	ObjectStreamClass osc = new ObjectStreamClass( name, uid,
						       flags, fields );
	assignNewHandle( osc );
      
	setBlockDataMode( true );
	osc.setClass( resolveClass( osc ) );
	setBlockDataMode( false );
      
	DEBUG( "ENDBLOCKDATA " );
	if( myRealInputStream.readByte() != TC_ENDBLOCKDATA )
	  throw new IOException( "Data annotated to class was not consumed." );
      
	osc.setSuperclass( (ObjectStreamClass)readObject() );
	ret_val = osc;
	break;
      }
    
      if( marker == TC_STRING )
      {
	DEBUG( "STRING " );
	String s = myRealInputStream.readUTF();
	ret_val = processResoultion( s, assignNewHandle( s ) );
	break;
      }
    
      if( marker == TC_ARRAY )
      {
	ObjectStreamClass osc = (ObjectStreamClass)readObject();
	Class componenetType = osc.forClass().getComponentType();
	DEBUG( "ARRAY LENGTH " );
	int length = myRealInputStream.readInt();
	Object array = Array.newInstance( componenetType, length );
	int handle = assignNewHandle( array );
	readArrayElements( array, componenetType );
	ret_val = processResoultion( array, handle );
	break;
      }

      if( marker == TC_OBJECT )
      {
	ObjectStreamClass osc = (ObjectStreamClass)readObject();
	Class clazz = osc.forClass();
    
	if( !Serializable.class.isAssignableFrom( clazz ) )
	  throw new NotSerializableException( clazz + " is not Serializable, and thus cannot be deserialized." );
    
	if( Externalizable.class.isAssignableFrom( clazz ) )
	{
	  Externalizable obj = null;
	
	  try
	  {
	    obj = (Externalizable)clazz.newInstance();
	  }
	  catch( InstantiationException e )
	  {
	    throw new ClassNotFoundException( "Instance of " + clazz +
					      " could not be created" );
	  }
	  catch( IllegalAccessException e )
	  {
	    throw new ClassNotFoundException( "Instance of " + clazz +
					      " could not be created" );
	  }
	  

	  int handle = assignNewHandle( obj );

	  boolean read_from_blocks = ((osc.getFlags() & SC_BLOCK_DATA) != 0);
	
	  if( read_from_blocks )
	    setBlockDataMode( true );

	  obj.readExternal( this );

	  if( read_from_blocks )
	    setBlockDataMode( false );

	  ret_val = processResoultion( obj, handle );
	  break;
	}

	// find the first non-serializable, non-abstract
	// class in clazz's inheritance hierarchy
	Class first_nonserial = clazz.getSuperclass();
	while( Serializable.class.isAssignableFrom( first_nonserial )
	       || Modifier.isAbstract( first_nonserial.getModifiers() ) )
	{
	  first_nonserial = first_nonserial.getSuperclass();
	}
	
	DEBUGln( "Using " + first_nonserial
		 + " as starting point for constructing " + clazz );

	Object obj = null;
	obj = newObject( clazz, first_nonserial );
      
	if( obj == null )
	  throw new ClassNotFoundException( "Instance of " + clazz +
					    " could not be created" );

	int handle = assignNewHandle( obj );
	myCurrentObject = obj;
	ObjectStreamClass[] hierarchy =
	  ObjectStreamClass.getObjectStreamClasses( clazz );

	DEBUGln( "Got class hierarchy of depth " + hierarchy.length );

	// TODO: rewrite this loop natively??
	boolean has_read;
	for( int i=0; i < hierarchy.length; i++ )
	{
	  myCurrentObjectStreamClass = hierarchy[i];

	  DEBUGln( "Reading fields of "
		   + myCurrentObjectStreamClass.getName() );

	  has_read = true;
	
	  try
	  {
	    myCurrentObjectStreamClass.forClass().getDeclaredMethod
	      ( "readObject", new Class[] { ObjectInputStream.class } );
	  }
	  catch( NoSuchMethodException e )
	  {
	    has_read = false;
	  }

	  readFields( obj, myCurrentObjectStreamClass.getFields(),
		      has_read, myCurrentObjectStreamClass );

	  if( has_read )
	  {
	    DEBUG( "ENDBLOCKDATA? " );
	    if( myRealInputStream.readByte() != TC_ENDBLOCKDATA )
	      throw new IOException( "No end of block data seen for class with readObject(ObjectInputStream) method." );
	  }
	}

	myCurrentObject = null;
	myCurrentObjectStreamClass = null;
	ret_val = processResoultion( obj, handle );
	break;
      }

      if( marker == TC_RESET )
      {
	clearHandles();
	ret_val = readObject();
	break;
      }
    
      if( marker == TC_EXCEPTION )
      {
	Exception e = (Exception)readObject();
	clearHandles();
	throw new WriteAbortedException( "Exception thrown during writing of stream", e );
      }

      throw new IOException( "Unknown marker on stream" );
    }
    
    myIsDeserializing = was_deserializing;

    if( ! was_deserializing )
      setBlockDataMode( true );

    return ret_val;
  }
  

  /** 
     Reads the current objects non-transient, non-static fields from
     the current class from the underlying output stream.

     This method is intended to be called from within a object's
     <code>private void readObject(ObjectInputStream)</code>
     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
     <code>private void readObject(ObjectInputStream)</code>
     method.

     @exception IOException Exception from underlying
     <code>OutputStream</code>.
  */
  public final void defaultReadObject()
    throws ClassNotFoundException, IOException, NotActiveException
  {
    if( myCurrentObject == null || myCurrentObjectStreamClass == null )
      throw new NotActiveException( "defaultReadObject called by non-active class and/or object" );

    readFields( myCurrentObject,
		myCurrentObjectStreamClass.getFields(),
		false, myCurrentObjectStreamClass );
  }


  /** 
     Called when a class is being deserialized.  This is a hook to
     allow subclasses to read in information written by the
     <code>annotateClass(Class)</code> method of an
     <code>ObjectOutputStream</code>.  This method must return a class
     with the same name and serial version UID as the class
     represented by <code>osc</code>.

     @exception IOException Exception from underlying
     <code>OutputStream</code>.

     @see java.io.ObjectOutputStream#annotateClass(java.lang.Class)
  */
  protected Class resolveClass( ObjectStreamClass osc )
    throws ClassNotFoundException, IOException
  {
    // TODO: search up stack for class loader
    return Class.forName( osc.getName() );
  }


  /**
     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
     <code>OutputStream</code>.

     @see enableResolveObject(boolean)
  */
  protected Object resolveObject( Object obj ) throws IOException
  {
    return obj;
  }

  
  /**
     If <code>enable</code> is <code>true</code> and this object is
     trusted, then <code>resolveObject(Object)</code> will be called
     in subsequent calls to <code>readObject(Object)</code>.
     Otherwise, <code>resolveObject(Object)</code> will not be called.

     @exception SecurityException This class is not trusted.
  */
  protected final boolean enableResolveObject( boolean enable )
    throws SecurityException
  {
    if( enable )
      if( getClass().getClassLoader() != null )
	throw new SecurityException( "Untrusted ObjectInputStream subclass attempted to enable object resolution" );

    boolean old_val = myResolveEnabled;
    myResolveEnabled = 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
  {
    DEBUG( "STREAM MAGIC " );
    if( myRealInputStream.readShort() != STREAM_MAGIC )
      throw new StreamCorruptedException( "Invalid stream magic number" );
    
    DEBUG( "STREAM VERSION " );
    if( myRealInputStream.readShort() != STREAM_VERSION )
      throw new StreamCorruptedException( "Invalid stream version number" );
  }


  public int read() throws IOException
  {
    if( myReadDataFromBlock )
    {
      if( myBlockDataPosition >= myBlockDataBytes )
	readNextBlock();
      return myBlockData[ myBlockDataPosition++ ];
    }
    else
      return myRealInputStream.read();
  }

  public int read( byte data[], int offset, int length ) throws IOException
  {
    if( myReadDataFromBlock )
    {
      if( myBlockDataPosition + length >= myBlockDataBytes )
	readNextBlock();

      System.arraycopy( myBlockData, myBlockDataPosition,
			data, offset, length );
      return length;
    }
    else
      return myRealInputStream.read( data, offset, length );
  }

  public int available() throws IOException
  {
    if( myReadDataFromBlock )
    {
      if( myBlockDataPosition >= myBlockDataBytes )
	readNextBlock();
      
      return myBlockDataBytes - myBlockDataPosition;
    }
    else
      return myRealInputStream.available();
  }

  public void close() throws IOException
  {
    myDataInputStream.close();
    myRealInputStream.close();
  }

  public boolean readBoolean() throws IOException
  {
    return myDataInputStream.readBoolean();
  }

  public byte readByte() throws IOException
  {
    return myDataInputStream.readByte();
  }

  public int readUnsignedByte() throws IOException
  {
    return myDataInputStream.readUnsignedByte();
  }
  
  public short readShort() throws IOException
  {
    return myDataInputStream.readShort();
  }

  public int readUnsignedShort() throws IOException
  {
    return myDataInputStream.readUnsignedShort();
  }

  public char readChar() throws IOException
  {
    return myDataInputStream.readChar();
  }

  public int readInt() throws IOException
  {
    return myDataInputStream.readInt();
  }

  public long readLong() throws IOException
  {
    return myDataInputStream.readLong();
  }

  public float readFloat() throws IOException
  {
    return myDataInputStream.readFloat();
  }

  public double readDouble() throws IOException
  {
    return myDataInputStream.readDouble();
  }

  public void readFully( byte data[] ) throws IOException
  {
    myDataInputStream.readFully( data );
  }

  public void readFully( byte data[], int offset, int size )
    throws IOException
  {
    myDataInputStream.readFully( data, offset, size );
  }

  public int skipBytes( int len ) throws IOException
  {
    return myDataInputStream.skipBytes( len );
  }

  public String readLine() throws IOException
  {
    return myDataInputStream.readLine();
  }

  public String readUTF() throws IOException
  {
    return myDataInputStream.readUTF();
  }


  /**
     Protected constructor that allows subclasses to override
     deserialization.  This constructor should be called by subclasses
     that wish to override <code>readObject(Object)</code>.  This
     method does a security check <i>NOTE: currently not
     implemented</i>, then sets a flag that informs
     <code>readObject(Object)</code> to call the subclasses
     <code>readObjectOverride(Object)</code> method.

     @see readObjectOverride(Object)
  */
  protected ObjectInputStream()
    throws IOException, StreamCorruptedException
  {
    //TODO: security check
    myUseSubclassMethod = true;
  }


  /**
     This method allows subclasses to override the default
     de serialization mechanism provided by
     <code>ObjectInputStream</code>.  To make this method be used for
     writing objects, subclasses must invoke the 0-argument
     constructor on this class from there constructor.

     @see ObjectInputStream()
  */
  protected Object readObjectOverride()
    throws ClassNotFoundException, IOException, OptionalDataException
  {
    throw new IOException( "Subclass of ObjectInputStream must implement readObjectOverride" );
  }
  

  // assigns the next availible handle to OBJ
  private int assignNewHandle( Object obj )
  {
    myObjectLookupTable.put( new Integer( myNextOID ),
			     new ObjectIdentityWrapper( obj ) );

    try
    {
      DEBUGln( "Assigning handle " + myNextOID + " to " + obj );
    }
    catch( Throwable t ) {}

    return myNextOID++;
  }


  private Object processResoultion( Object obj, int handle )
    throws IOException
  {
    if( obj instanceof Resolvable )
      obj = ((Resolvable)obj).readResolve();
    
    if( myResolveEnabled )
      obj = resolveObject( obj );
    
    myObjectLookupTable.put( new Integer( handle ),
			     new ObjectIdentityWrapper( obj ) );

    return obj;
  }
  

  private void clearHandles()
  {
    myObjectLookupTable.clear();
    myNextOID = baseWireHandle;
  }
  

  private void readNextBlock() throws IOException
  {
    DEBUG( "MARKER " );
    readNextBlock( myRealInputStream.readByte() );
  }


  private void readNextBlock( byte marker ) throws IOException
  {
    if( marker == TC_BLOCKDATA )
    {
      DEBUG( "BLOCK DATA SIZE " );
      myBlockDataBytes = myRealInputStream.readUnsignedByte();
    }
    else if( marker == TC_BLOCKDATALONG )
    {
      DEBUG( "BLOCK DATA LONG SIZE " );
      myBlockDataBytes = myRealInputStream.readInt();
    }
    else
    {
      throw new EOFException( "Attempt to read primitive data, but no data block is active." );
    }

    if( myBlockData.length < myBlockDataBytes )
      myBlockData = new byte[ myBlockDataBytes ];
	
    myRealInputStream.readFully( myBlockData, 0, myBlockDataBytes );
    myBlockDataPosition = 0;
  }


  private void readArrayElements( Object array, Class clazz )
    throws ClassNotFoundException, IOException
  {
    if( clazz.isPrimitive() )
    {
      if( clazz == Boolean.TYPE )
      {
	boolean[] cast_array = (boolean[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readBoolean();
	return;
      }
      if( clazz == Byte.TYPE )
      {
	byte[] cast_array = (byte[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readByte();
	return;
      }
      if( clazz == Character.TYPE )
      {
	char[] cast_array = (char[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readChar();
	return;
      }
      if( clazz == Double.TYPE )
      {
	double[] cast_array = (double[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readDouble();
	return;
      }
      if( clazz == Float.TYPE )
      {
	float[] cast_array = (float[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readFloat();
	return;
      }
      if( clazz == Integer.TYPE )
      {
	int[] cast_array = (int[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readInt();
	return;
      }
      if( clazz == Long.TYPE )
      {
	long[] cast_array = (long[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readLong();
	return;
      }
      if( clazz == Short.TYPE )
      {
	short[] cast_array = (short[])array;
	for( int i=0; i < cast_array.length; i++ )
	  cast_array[i] = myRealInputStream.readShort();
	return;
      }
    }
    else
    {
      Object[] cast_array = (Object[])array;
      for( int i=0; i < cast_array.length; i++ )
 	  cast_array[i] = readObject();
    }
  }
  

  private void readFields( Object obj, OSCField[] fields,
			   boolean call_read_method, ObjectStreamClass osc )
    throws ClassNotFoundException, IOException
  {
    //TODO: handle different class versions
    if( call_read_method )
    {
      setBlockDataMode( true );
      callReadMethod( obj, osc.forClass() );
      setBlockDataMode( false );
      return;
    }

    String field_name;
    Class type;
    for( int i=0; i < fields.length; i++ )
    {
      field_name = fields[i].getName();
      type = fields[i].getType();

      if( type == Boolean.TYPE )
	setBooleanField( obj, field_name, myRealInputStream.readBoolean() );
      else if( type == Byte.TYPE )
	setByteField( obj, field_name, myRealInputStream.readByte() );
      else if( type == Character.TYPE )
	setCharField( obj, field_name, myRealInputStream.readChar() );
      else if( type == Double.TYPE )
	setDoubleField( obj, field_name, myRealInputStream.readDouble() );
      else if( type == Float.TYPE )
	setFloatField( obj, field_name, myRealInputStream.readFloat() );
      else if( type == Integer.TYPE )
	setIntField( obj, field_name, myRealInputStream.readInt() );
      else if( type == Long.TYPE )
	setLongField( obj, field_name, myRealInputStream.readLong() );
      else if( type == Short.TYPE )
	setShortField( obj, field_name, myRealInputStream.readShort() );
      else
	setObjectField( obj, field_name,
			TypeSignature.getEncodingOfClass(type),
			readObject() );
    }
  }
  

  // Toggles writing primitive data to block-data buffer.
  private void setBlockDataMode( boolean on )
  {
    myReadDataFromBlock = on;

    if( on )
      myDataInputStream = myBlockDataInput;
    else
      myDataInputStream = myRealInputStream;
  }


  private Object newObject( Class real_class, Class constructor_class )
  {
    try
    {
      Object obj = allocateObject( real_class );
      //TODO: figure out how to construct an object using a superclass
      // constructor, but have the resulting object be of the subclass
      // e.g. make an Integer by calling the constructor for Object,
      // but still have the class of the new object be Integer
//      callConstructor( obj, constructor_class );
      return obj;
    }
    catch( InstantiationException e )
    {
      return null;
    }
  }
  

  private native void callReadMethod( Object obj, Class clazz );
  private native Object allocateObject( Class clazz )
    throws InstantiationException;
  private native void callConstructor( Object obj, Class clazz );
  private native void setBooleanField( Object obj, String field_name,
				       boolean val );
  private native void setByteField( Object obj, String field_name,
				    byte val );
  private native void setCharField( Object obj, String field_name,
				    char val );
  private native void setDoubleField( Object obj, String field_name,
				      double val );
  private native void setFloatField( Object obj, String field_name,
				     float val );
  private native void setIntField( Object obj, String field_name,
				   int val );
  private native void setLongField( Object obj, String field_name,
				    long val );
  private native void setShortField( Object obj, String field_name,
				     short val );
  private native void setObjectField( Object obj, String field_name,
					String type_code, Object val );


  private static final int BUFFER_SIZE = 1024;
  
  private PrintingDataInputStream myRealInputStream;
  private PrintingDataInputStream myDataInputStream;
  private PrintingDataInputStream myBlockDataInput;
  private int myBlockDataPosition;
  private int myBlockDataBytes;
  private byte[] myBlockData;
  private boolean myUseSubclassMethod;
  private int myNextOID;
  private boolean myResolveEnabled;
  private Hashtable myObjectLookupTable;
  private Object myCurrentObject;
  private ObjectStreamClass myCurrentObjectStreamClass;
  private boolean myReadDataFromBlock;
  private boolean myIsDeserializing;

  static
  {
    //DEBUG
    System.out.println( "Using ObjectInputStream" );
    //eDEBUG

    System.loadLibrary( "java_io_ObjectInputStream" );
  }


  private void DEBUG( String msg )
  {
    System.out.print( msg );
  }
  

  private void DEBUGln( String msg )
  {
    System.out.println( msg );
  }
  
}