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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
|
/* ObjectInputStream.java -- Class used to read serialized objects
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008
Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.io;
import gnu.classpath.Pair;
import gnu.classpath.VMStackWalker;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
/**
* @author Tom Tromey (tromey@redhat.com)
* @author Jeroen Frijters (jeroen@frijters.net)
* @author Guilhem Lavaux (guilhem@kaffe.org)
* @author Michael Koch (konqueror@gmx.de)
* @author Andrew John Hughes (gnu_andrew@member.fsf.org)
*/
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
{
if (DEBUG)
{
String val = System.getProperty("gcj.dumpobjects");
if (dump == false && val != null && !val.equals(""))
{
dump = true;
System.out.println ("Serialization debugging enabled");
}
else if (dump == true && (val == null || val.equals("")))
{
dump = false;
System.out.println ("Serialization debugging disabled");
}
}
this.resolveEnabled = false;
this.blockDataPosition = 0;
this.blockDataBytes = 0;
this.blockData = new byte[BUFFER_SIZE];
this.blockDataInput = new DataInputStream(this);
this.realInputStream = new DataInputStream(in);
this.nextOID = baseWireHandle;
handles = new HashMap<Integer,Pair<Boolean,Object>>();
this.classLookupTable = new Hashtable<Class,ObjectStreamClass>();
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. This method can also throw Errors and
* RuntimeExceptions if caused by existing readResolve() user code.
*
* @return The object read from the underlying stream.
*
* @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
{
return readObject(true);
}
/**
* <p>
* Returns the next deserialized object read from the
* underlying stream in an unshared manner. Any object
* returned by this method will not be returned by
* subsequent calls to either this method or {@link #readObject()}.
* </p>
* <p>
* This behaviour is achieved by:
* </p>
* <ul>
* <li>Marking the handles created by successful calls to this
* method, so that future calls to {@link #readObject()} or
* {@link #readUnshared()} will throw an {@link ObjectStreamException}
* rather than returning the same object reference.</li>
* <li>Throwing an {@link ObjectStreamException} if the next
* element in the stream is a reference to an earlier object.</li>
* </ul>
*
* @return a reference to the deserialized object.
* @throws ClassNotFoundException if the class of the object being
* deserialized can not be found.
* @throws StreamCorruptedException if information in the stream
* is inconsistent.
* @throws ObjectStreamException if the next object has already been
* returned by an earlier call to this
* method or {@link #readObject()}.
* @throws OptionalDataException if primitive data occurs next in the stream.
* @throws IOException if an I/O error occurs from the stream.
* @since 1.4
* @see #readObject()
*/
public Object readUnshared()
throws IOException, ClassNotFoundException
{
return readObject(false);
}
/**
* 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. This method can also throw Errors and
* RuntimeExceptions if caused by existing readResolve() user code.
*
* @param shared true if handles created by this call should be shared
* with later calls.
* @return The object read from the underlying stream.
*
* @exception ClassNotFoundException The class that an object being
* read in belongs to cannot be found.
*
* @exception IOException Exception from underlying
* <code>InputStream</code>.
*/
private final Object readObject(boolean shared)
throws ClassNotFoundException, IOException
{
if (this.useSubclassMethod)
return readObjectOverride();
Object ret_val;
boolean old_mode = setBlockDataMode(false);
byte marker = this.realInputStream.readByte();
if (DEBUG)
depth += 2;
if(dump) dumpElement("MARKER: 0x" + Integer.toHexString(marker) + " ");
try
{
ret_val = parseContent(marker, shared);
}
finally
{
setBlockDataMode(old_mode);
if (DEBUG)
depth -= 2;
}
return ret_val;
}
/**
* Handles a content block within the stream, which begins with a marker
* byte indicating its type.
*
* @param marker the byte marker.
* @param shared true if handles created by this call should be shared
* with later calls.
* @return an object which represents the parsed content.
* @throws ClassNotFoundException if the class of an object being
* read in cannot be found.
* @throws IOException if invalid data occurs or one is thrown by the
* underlying <code>InputStream</code>.
*/
private Object parseContent(byte marker, boolean shared)
throws ClassNotFoundException, IOException
{
Object ret_val;
boolean is_consumed = false;
switch (marker)
{
case TC_ENDBLOCKDATA:
{
ret_val = null;
is_consumed = true;
break;
}
case TC_BLOCKDATA:
case TC_BLOCKDATALONG:
{
if (marker == TC_BLOCKDATALONG)
{ if(dump) dumpElementln("BLOCKDATALONG"); }
else
{ if(dump) dumpElementln("BLOCKDATA"); }
readNextBlock(marker);
}
case TC_NULL:
{
if(dump) dumpElementln("NULL");
ret_val = null;
break;
}
case TC_REFERENCE:
{
if(dump) dumpElement("REFERENCE ");
int oid = realInputStream.readInt();
if(dump) dumpElementln(Integer.toHexString(oid));
ret_val = lookupHandle(oid);
if (!shared)
throw new
InvalidObjectException("References can not be read unshared.");
break;
}
case TC_CLASS:
{
if(dump) dumpElementln("CLASS");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class clazz = osc.forClass();
assignNewHandle(clazz,shared);
ret_val = clazz;
break;
}
case TC_PROXYCLASSDESC:
{
if(dump) dumpElementln("PROXYCLASS");
/* GCJ LOCAL */
// The grammar at this point is
// TC_PROXYCLASSDESC newHandle proxyClassDescInfo
// i.e. we have to assign the handle immediately after
// reading the marker.
int handle = assignNewHandle("Dummy proxy",shared);
/* END GCJ LOCAL */
int n_intf = this.realInputStream.readInt();
String[] intfs = new String[n_intf];
for (int i = 0; i < n_intf; i++)
{
intfs[i] = this.realInputStream.readUTF();
}
boolean oldmode = setBlockDataMode(true);
Class cl = resolveProxyClass(intfs);
setBlockDataMode(oldmode);
ObjectStreamClass osc = lookupClass(cl);
if (osc.firstNonSerializableParentConstructor == null)
{
osc.realClassIsSerializable = true;
osc.fields = osc.fieldMapping = new ObjectStreamField[0];
try
{
osc.firstNonSerializableParentConstructor =
Object.class.getConstructor(new Class[0]);
}
catch (NoSuchMethodException x)
{
throw (InternalError)
new InternalError("Object ctor missing").initCause(x);
}
}
/* GCJ LOCAL */
rememberHandle(osc,shared,handle);
/* END GCJ LOCAL */
if (!is_consumed)
{
byte b = this.realInputStream.readByte();
if (b != TC_ENDBLOCKDATA)
throw new IOException("Data annotated to class was not consumed." + b);
}
else
is_consumed = false;
ObjectStreamClass superosc = (ObjectStreamClass)readObject();
osc.setSuperclass(superosc);
ret_val = osc;
break;
}
case TC_CLASSDESC:
{
ObjectStreamClass osc = readClassDescriptor();
if (!is_consumed)
{
byte b = this.realInputStream.readByte();
if (b != TC_ENDBLOCKDATA)
throw new IOException("Data annotated to class was not consumed." + b);
}
else
is_consumed = false;
osc.setSuperclass ((ObjectStreamClass)readObject());
ret_val = osc;
break;
}
case TC_STRING:
{
if(dump) dumpElement("STRING=");
String s = this.realInputStream.readUTF();
if(dump) dumpElementln(s);
ret_val = processResolution(null, s, assignNewHandle(s,shared),
shared);
break;
}
case TC_LONGSTRING:
{
if(dump) dumpElement("STRING=");
String s = this.realInputStream.readUTFLong();
if(dump) dumpElementln(s);
ret_val = processResolution(null, s, assignNewHandle(s,shared),
shared);
break;
}
case TC_ARRAY:
{
if(dump) dumpElementln("ARRAY");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class componentType = osc.forClass().getComponentType();
if(dump) dumpElement("ARRAY LENGTH=");
int length = this.realInputStream.readInt();
if(dump) dumpElementln (length + "; COMPONENT TYPE=" + componentType);
Object array = Array.newInstance(componentType, length);
int handle = assignNewHandle(array,shared);
readArrayElements(array, componentType);
if(dump)
for (int i = 0, len = Array.getLength(array); i < len; i++)
dumpElementln(" ELEMENT[" + i + "]=", Array.get(array, i));
ret_val = processResolution(null, array, handle, shared);
break;
}
case TC_OBJECT:
{
if(dump) dumpElementln("OBJECT");
ObjectStreamClass osc = (ObjectStreamClass)readObject();
Class clazz = osc.forClass();
if (!osc.realClassIsSerializable)
throw new NotSerializableException
(clazz + " is not Serializable, and thus cannot be deserialized.");
if (osc.realClassIsExternalizable)
{
Externalizable obj = osc.newInstance();
int handle = assignNewHandle(obj,shared);
boolean read_from_blocks = ((osc.getFlags() & SC_BLOCK_DATA) != 0);
boolean oldmode = this.readDataFromBlock;
if (read_from_blocks)
setBlockDataMode(true);
obj.readExternal(this);
if (read_from_blocks)
{
setBlockDataMode(oldmode);
if (!oldmode)
if (this.realInputStream.readByte() != TC_ENDBLOCKDATA)
throw new IOException("No end of block data seen for class with readExternal (ObjectInputStream) method.");
}
ret_val = processResolution(osc, obj, handle,shared);
break;
} // end if (osc.realClassIsExternalizable)
Object obj = newObject(clazz, osc.firstNonSerializableParentConstructor);
int handle = assignNewHandle(obj,shared);
Object prevObject = this.currentObject;
ObjectStreamClass prevObjectStreamClass = this.currentObjectStreamClass;
TreeSet<ValidatorAndPriority> prevObjectValidators =
this.currentObjectValidators;
this.currentObject = obj;
this.currentObjectValidators = null;
ObjectStreamClass[] hierarchy = hierarchy(clazz);
for (int i = 0; i < hierarchy.length; i++)
{
this.currentObjectStreamClass = hierarchy[i];
if(dump) dumpElementln("Reading fields of " + this.currentObjectStreamClass.getName ());
// XXX: should initialize fields in classes in the hierarchy
// that aren't in the stream
// should skip over classes in the stream that aren't in the
// real classes hierarchy
Method readObjectMethod = this.currentObjectStreamClass.readObjectMethod;
if (readObjectMethod != null)
{
fieldsAlreadyRead = false;
boolean oldmode = setBlockDataMode(true);
callReadMethod(readObjectMethod, this.currentObjectStreamClass.forClass(), obj);
setBlockDataMode(oldmode);
}
else
{
readFields(obj, currentObjectStreamClass);
}
if (this.currentObjectStreamClass.hasWriteMethod())
{
if(dump) dumpElement("ENDBLOCKDATA? ");
try
{
/* Read blocks until an end marker */
byte writeMarker = this.realInputStream.readByte();
while (writeMarker != TC_ENDBLOCKDATA)
{
parseContent(writeMarker, shared);
writeMarker = this.realInputStream.readByte();
}
if(dump) dumpElementln("yes");
}
catch (EOFException e)
{
throw (IOException) new IOException
("No end of block data seen for class with readObject (ObjectInputStream) method.").initCause(e);
}
}
}
this.currentObject = prevObject;
this.currentObjectStreamClass = prevObjectStreamClass;
ret_val = processResolution(osc, obj, handle, shared);
if (currentObjectValidators != null)
invokeValidators();
this.currentObjectValidators = prevObjectValidators;
break;
}
case TC_RESET:
if(dump) dumpElementln("RESET");
clearHandles();
ret_val = readObject();
break;
case TC_EXCEPTION:
{
if(dump) dumpElement("EXCEPTION=");
Exception e = (Exception)readObject();
if(dump) dumpElementln(e.toString());
clearHandles();
throw new WriteAbortedException("Exception thrown during writing of stream", e);
}
case TC_ENUM:
{
/* TC_ENUM classDesc newHandle enumConstantName */
if (dump)
dumpElementln("ENUM=");
ObjectStreamClass osc = (ObjectStreamClass) readObject();
String constantName = (String) readObject();
if (dump)
dumpElementln("CONSTANT NAME = " + constantName);
Class clazz = osc.forClass();
Enum instance = Enum.valueOf(clazz, constantName);
assignNewHandle(instance,shared);
ret_val = instance;
break;
}
default:
throw new IOException("Unknown marker on stream: " + marker);
}
return ret_val;
}
/**
* This method makes a partial check of types for the fields
* contained given in arguments. It checks primitive types of
* fields1 against non primitive types of fields2. This method
* assumes the two lists has already been sorted according to
* the Java specification.
*
* @param name Name of the class owning the given fields.
* @param fields1 First list to check.
* @param fields2 Second list to check.
* @throws InvalidClassException if a field in fields1, which has a primitive type, is a present
* in the non primitive part in fields2.
*/
private void checkTypeConsistency(String name, ObjectStreamField[] fields1, ObjectStreamField[] fields2)
throws InvalidClassException
{
int nonPrimitive = 0;
for (nonPrimitive = 0;
nonPrimitive < fields1.length
&& fields1[nonPrimitive].isPrimitive(); nonPrimitive++)
{
}
if (nonPrimitive == fields1.length)
return;
int i = 0;
ObjectStreamField f1;
ObjectStreamField f2;
while (i < fields2.length
&& nonPrimitive < fields1.length)
{
f1 = fields1[nonPrimitive];
f2 = fields2[i];
if (!f2.isPrimitive())
break;
int compVal = f1.getName().compareTo (f2.getName());
if (compVal < 0)
{
nonPrimitive++;
}
else if (compVal > 0)
{
i++;
}
else
{
throw new InvalidClassException
("invalid field type for " + f2.getName() +
" in class " + name);
}
}
}
/**
* This method reads a class descriptor from the real input stream
* and use these data to create a new instance of ObjectStreamClass.
* Fields are sorted and ordered for the real read which occurs for
* each instance of the described class. Be aware that if you call that
* method you must ensure that the stream is synchronized, in the other
* case it may be completely desynchronized.
*
* @return A new instance of ObjectStreamClass containing the freshly
* created descriptor.
* @throws ClassNotFoundException if the required class to build the
* descriptor has not been found in the system.
* @throws IOException An input/output error occured.
* @throws InvalidClassException If there was a compatibility problem
* between the class present in the system and the serialized class.
*/
protected ObjectStreamClass readClassDescriptor()
throws ClassNotFoundException, IOException
{
if(dump) dumpElement("CLASSDESC NAME=");
String name = this.realInputStream.readUTF();
if(dump) dumpElement(name + "; UID=");
long uid = this.realInputStream.readLong ();
if(dump) dumpElement(Long.toHexString(uid) + "; FLAGS=");
byte flags = this.realInputStream.readByte ();
if(dump) dumpElement(Integer.toHexString(flags) + "; FIELD COUNT=");
short field_count = this.realInputStream.readShort();
if(dump) dumpElementln(Short.toString(field_count));
ObjectStreamField[] fields = new ObjectStreamField[field_count];
ObjectStreamClass osc = new ObjectStreamClass(name, uid,
flags, fields);
assignNewHandle(osc,true);
for (int i = 0; i < field_count; i++)
{
if(dump) dumpElement(" TYPE CODE=");
char type_code = (char)this.realInputStream.readByte();
if(dump) dumpElement(type_code + "; FIELD NAME=");
String field_name = this.realInputStream.readUTF();
if(dump) dumpElementln(field_name);
String class_name;
// If the type code is an array or an object we must
// decode a String here. In the other case we convert
// the type code and pass it to ObjectStreamField.
// Type codes are decoded by gnu.java.lang.reflect.TypeSignature.
if (type_code == 'L' || type_code == '[')
class_name = (String)readObject();
else
class_name = String.valueOf(type_code);
fields[i] =
new ObjectStreamField(field_name, class_name);
}
/* Now that fields have been read we may resolve the class
* (and read annotation if needed). */
Class clazz = resolveClass(osc);
ClassLoader loader = clazz.getClassLoader();
for (int i = 0; i < field_count; i++)
{
fields[i].resolveType(loader);
}
boolean oldmode = setBlockDataMode(true);
osc.setClass(clazz, lookupClass(clazz.getSuperclass()));
classLookupTable.put(clazz, osc);
setBlockDataMode(oldmode);
// find the first non-serializable class in clazz's inheritance hierarchy
Class first_nonserial = clazz.getSuperclass();
// Maybe it is a primitive class, those don't have a super class,
// or Object itself. Otherwise we can keep getting the superclass
// till we hit the Object class, or some other non-serializable class.
if (first_nonserial == null)
first_nonserial = clazz;
else
while (Serializable.class.isAssignableFrom(first_nonserial))
first_nonserial = first_nonserial.getSuperclass();
final Class local_constructor_class = first_nonserial;
osc.firstNonSerializableParentConstructor =
(Constructor)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
Constructor c = local_constructor_class.
getDeclaredConstructor(new Class[0]);
if (Modifier.isPrivate(c.getModifiers()))
return null;
return c;
}
catch (NoSuchMethodException e)
{
// error will be reported later, in newObject()
return null;
}
}
});
osc.realClassIsSerializable = Serializable.class.isAssignableFrom(clazz);
osc.realClassIsExternalizable = Externalizable.class.isAssignableFrom(clazz);
ObjectStreamField[] stream_fields = osc.fields;
ObjectStreamField[] real_fields = ObjectStreamClass.lookupForClassObject(clazz).fields;
ObjectStreamField[] fieldmapping = new ObjectStreamField[2 * Math.max(stream_fields.length, real_fields.length)];
int stream_idx = 0;
int real_idx = 0;
int map_idx = 0;
/*
* Check that there is no type inconsistencies between the lists.
* A special checking must be done for the two groups: primitive types and
* not primitive types.
*/
checkTypeConsistency(name, real_fields, stream_fields);
checkTypeConsistency(name, stream_fields, real_fields);
while (stream_idx < stream_fields.length
|| real_idx < real_fields.length)
{
ObjectStreamField stream_field = null;
ObjectStreamField real_field = null;
if (stream_idx == stream_fields.length)
{
real_field = real_fields[real_idx++];
}
else if (real_idx == real_fields.length)
{
stream_field = stream_fields[stream_idx++];
}
else
{
int comp_val =
real_fields[real_idx].compareTo (stream_fields[stream_idx]);
if (comp_val < 0)
{
real_field = real_fields[real_idx++];
}
else if (comp_val > 0)
{
stream_field = stream_fields[stream_idx++];
}
else
{
stream_field = stream_fields[stream_idx++];
real_field = real_fields[real_idx++];
if (stream_field.getType() != real_field.getType())
throw new InvalidClassException
("invalid field type for " + real_field.getName() +
" in class " + name);
}
}
/* If some of stream_fields does not correspond to any of real_fields,
* or the opposite, then fieldmapping will go short.
*/
if (map_idx == fieldmapping.length)
{
ObjectStreamField[] newfieldmapping =
new ObjectStreamField[fieldmapping.length + 2];
System.arraycopy(fieldmapping, 0,
newfieldmapping, 0, fieldmapping.length);
fieldmapping = newfieldmapping;
}
fieldmapping[map_idx++] = stream_field;
fieldmapping[map_idx++] = real_field;
}
osc.fieldMapping = fieldmapping;
return osc;
}
/**
* 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 void defaultReadObject()
throws ClassNotFoundException, IOException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("defaultReadObject called by non-active"
+ " class and/or object");
if (fieldsAlreadyRead)
throw new NotActiveException("defaultReadObject called but fields "
+ "already read from stream (by "
+ "defaultReadObject or readFields)");
boolean oldmode = setBlockDataMode(false);
readFields(this.currentObject, this.currentObjectStreamClass);
setBlockDataMode(oldmode);
fieldsAlreadyRead = true;
}
/**
* Registers a <code>ObjectInputValidation</code> to be carried out
* on the object graph currently being deserialized before it is
* returned to the original caller of <code>readObject ()</code>.
* The order of validation for multiple
* <code>ObjectInputValidation</code>s can be controled using
* <code>priority</code>. Validators with higher priorities are
* called first.
*
* @see java.io.ObjectInputValidation
*
* @exception InvalidObjectException <code>validator</code> is
* <code>null</code>
*
* @exception NotActiveException an attempt was made to add a
* validator outside of the <code>readObject</code> method of the
* object currently being deserialized
*/
public void registerValidation(ObjectInputValidation validator,
int priority)
throws InvalidObjectException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("registerValidation called by non-active "
+ "class and/or object");
if (validator == null)
throw new InvalidObjectException("attempt to add a null "
+ "ObjectInputValidation object");
if (currentObjectValidators == null)
currentObjectValidators = new TreeSet<ValidatorAndPriority>();
currentObjectValidators.add(new ValidatorAndPriority(validator, priority));
}
/**
* 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 implementation looks up the active call stack for a
* <code>ClassLoader</code>; if a <code>ClassLoader</code> is found,
* it is used to load the class associated with <code>osc</code>,
* otherwise, the default system <code>ClassLoader</code> is used.
*
* @exception IOException Exception from underlying
* <code>OutputStream</code>.
*
* @see java.io.ObjectOutputStream#annotateClass (java.lang.Class)
*/
protected Class<?> resolveClass(ObjectStreamClass osc)
throws ClassNotFoundException, IOException
{
String name = osc.getName();
try
{
return Class.forName(name, true, currentLoader());
}
catch(ClassNotFoundException x)
{
if (name.equals("void"))
return Void.TYPE;
else if (name.equals("boolean"))
return Boolean.TYPE;
else if (name.equals("byte"))
return Byte.TYPE;
else if (name.equals("char"))
return Character.TYPE;
else if (name.equals("short"))
return Short.TYPE;
else if (name.equals("int"))
return Integer.TYPE;
else if (name.equals("long"))
return Long.TYPE;
else if (name.equals("float"))
return Float.TYPE;
else if (name.equals("double"))
return Double.TYPE;
else
throw x;
}
}
/**
* Returns the most recent user defined ClassLoader on the execution stack
* or null if none is found.
*/
private ClassLoader currentLoader()
{
return VMStackWalker.firstNonNullClassLoader();
}
/**
* Lookup a class stored in the local hashtable. If it is not
* use the global lookup function in ObjectStreamClass to build
* the ObjectStreamClass. This method is requested according to
* the behaviour detected in the JDK by Kaffe's team.
*
* @param clazz Class to lookup in the hash table or for which
* we must build a descriptor.
* @return A valid instance of ObjectStreamClass corresponding
* to the specified class.
*/
private ObjectStreamClass lookupClass(Class clazz)
{
if (clazz == null)
return null;
ObjectStreamClass oclazz;
oclazz = classLookupTable.get(clazz);
if (oclazz == null)
return ObjectStreamClass.lookup(clazz);
else
return oclazz;
}
/**
* Reconstruct class hierarchy the same way {@link
* java.io.ObjectStreamClass#hierarchy} does but using lookupClass
* instead of ObjectStreamClass.lookup.
*
* @param clazz This is the class for which we want the hierarchy.
*
* @return An array of valid {@link java.io.ObjectStreamClass} instances which
* represent the class hierarchy for clazz.
*/
private ObjectStreamClass[] hierarchy(Class clazz)
{
ObjectStreamClass osc = lookupClass(clazz);
return osc == null ? new ObjectStreamClass[0] : osc.hierarchy();
}
/**
* 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;
}
protected Class<?> resolveProxyClass(String[] intfs)
throws IOException, ClassNotFoundException
{
ClassLoader cl = currentLoader();
Class<?>[] clss = new Class<?>[intfs.length];
if(cl == null)
{
for (int i = 0; i < intfs.length; i++)
clss[i] = Class.forName(intfs[i]);
cl = ClassLoader.getSystemClassLoader();
}
else
for (int i = 0; i < intfs.length; i++)
clss[i] = Class.forName(intfs[i], false, cl);
try
{
return Proxy.getProxyClass(cl, clss);
}
catch (IllegalArgumentException e)
{
throw new ClassNotFoundException(null, e);
}
}
/**
* 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 boolean enableResolveObject (boolean enable)
throws SecurityException
{
if (enable)
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new SerializablePermission("enableSubstitution"));
}
boolean old_val = this.resolveEnabled;
this.resolveEnabled = 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
{
if(dump) dumpElement("STREAM MAGIC ");
if (this.realInputStream.readShort() != STREAM_MAGIC)
throw new StreamCorruptedException("Invalid stream magic number");
if(dump) dumpElementln("STREAM VERSION ");
if (this.realInputStream.readShort() != STREAM_VERSION)
throw new StreamCorruptedException("Invalid stream version number");
}
public int read() throws IOException
{
if (this.readDataFromBlock)
{
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock();
return (this.blockData[this.blockDataPosition++] & 0xff);
}
else
return this.realInputStream.read();
}
public int read(byte[] data, int offset, int length) throws IOException
{
if (this.readDataFromBlock)
{
int remain = this.blockDataBytes - this.blockDataPosition;
if (remain == 0)
{
readNextBlock();
remain = this.blockDataBytes - this.blockDataPosition;
}
length = Math.min(length, remain);
System.arraycopy(this.blockData, this.blockDataPosition,
data, offset, length);
this.blockDataPosition += length;
return length;
}
else
return this.realInputStream.read(data, offset, length);
}
public int available() throws IOException
{
if (this.readDataFromBlock)
{
if (this.blockDataPosition >= this.blockDataBytes)
readNextBlock ();
return this.blockDataBytes - this.blockDataPosition;
}
else
return this.realInputStream.available();
}
public void close() throws IOException
{
this.realInputStream.close();
}
public boolean readBoolean() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode (true);
boolean value = this.dataInputStream.readBoolean ();
if (switchmode)
setBlockDataMode (oldmode);
return value;
}
public byte readByte() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
byte value = this.dataInputStream.readByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedByte() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedByte();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public short readShort() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
short value = this.dataInputStream.readShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readUnsignedShort() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readUnsignedShort();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public char readChar() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
char value = this.dataInputStream.readChar();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public int readInt() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
int value = this.dataInputStream.readInt();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public long readLong() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
long value = this.dataInputStream.readLong();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public float readFloat() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
float value = this.dataInputStream.readFloat();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public double readDouble() throws IOException
{
boolean switchmode = true;
boolean oldmode = this.readDataFromBlock;
if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
switchmode = false;
if (switchmode)
oldmode = setBlockDataMode(true);
double value = this.dataInputStream.readDouble();
if (switchmode)
setBlockDataMode(oldmode);
return value;
}
public void readFully(byte data[]) throws IOException
{
this.dataInputStream.readFully(data);
}
public void readFully(byte data[], int offset, int size)
throws IOException
{
this.dataInputStream.readFully(data, offset, size);
}
public int skipBytes(int len) throws IOException
{
return this.dataInputStream.skipBytes(len);
}
/**
* @deprecated
* @see java.io.DataInputStream#readLine ()
*/
public String readLine() throws IOException
{
return this.dataInputStream.readLine();
}
public String readUTF() throws IOException
{
return this.dataInputStream.readUTF();
}
/**
* This class allows a class to specify exactly which fields should
* be read, and what values should be read for these fields.
*
* XXX: finish up comments
*/
public abstract static class GetField
{
public abstract ObjectStreamClass getObjectStreamClass();
public abstract boolean defaulted(String name)
throws IOException, IllegalArgumentException;
public abstract boolean get(String name, boolean defvalue)
throws IOException, IllegalArgumentException;
public abstract char get(String name, char defvalue)
throws IOException, IllegalArgumentException;
public abstract byte get(String name, byte defvalue)
throws IOException, IllegalArgumentException;
public abstract short get(String name, short defvalue)
throws IOException, IllegalArgumentException;
public abstract int get(String name, int defvalue)
throws IOException, IllegalArgumentException;
public abstract long get(String name, long defvalue)
throws IOException, IllegalArgumentException;
public abstract float get(String name, float defvalue)
throws IOException, IllegalArgumentException;
public abstract double get(String name, double defvalue)
throws IOException, IllegalArgumentException;
public abstract Object get(String name, Object defvalue)
throws IOException, IllegalArgumentException;
}
/**
* This method should be called by a method called 'readObject' in the
* deserializing class (if present). It cannot (and should not)be called
* outside of it. Its goal is to read all fields in the real input stream
* and keep them accessible through the {@link GetField} class. Calling
* this method will not alter the deserializing object.
*
* @return A valid freshly created 'GetField' instance to get access to
* the deserialized stream.
* @throws IOException An input/output exception occured.
* @throws ClassNotFoundException
* @throws NotActiveException
*/
public GetField readFields()
throws IOException, ClassNotFoundException, NotActiveException
{
if (this.currentObject == null || this.currentObjectStreamClass == null)
throw new NotActiveException("readFields called by non-active class and/or object");
if (prereadFields != null)
return prereadFields;
if (fieldsAlreadyRead)
throw new NotActiveException("readFields called but fields already read from"
+ " stream (by defaultReadObject or readFields)");
final ObjectStreamClass clazz = this.currentObjectStreamClass;
final byte[] prim_field_data = new byte[clazz.primFieldSize];
final Object[] objs = new Object[clazz.objectFieldCount];
// Apparently Block data is not used with GetField as per
// empirical evidence against JDK 1.2. Also see Mauve test
// java.io.ObjectInputOutput.Test.GetPutField.
boolean oldmode = setBlockDataMode(false);
readFully(prim_field_data);
for (int i = 0; i < objs.length; ++ i)
objs[i] = readObject();
setBlockDataMode(oldmode);
prereadFields = new GetField()
{
public ObjectStreamClass getObjectStreamClass()
{
return clazz;
}
public boolean defaulted(String name)
throws IOException, IllegalArgumentException
{
ObjectStreamField f = clazz.getField(name);
/* First if we have a serialized field use the descriptor */
if (f != null)
{
/* It is in serialPersistentFields but setClass tells us
* it should not be set. This value is defaulted.
*/
if (f.isPersistent() && !f.isToSet())
return true;
return false;
}
/* This is not a serialized field. There should be
* a default value only if the field really exists.
*/
try
{
return (clazz.forClass().getDeclaredField (name) != null);
}
catch (NoSuchFieldException e)
{
throw new IllegalArgumentException(e);
}
}
public boolean get(String name, boolean defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Boolean.TYPE);
if (field == null)
return defvalue;
return prim_field_data[field.getOffset()] == 0 ? false : true;
}
public char get(String name, char defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Character.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (char)(((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public byte get(String name, byte defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Byte.TYPE);
if (field == null)
return defvalue;
return prim_field_data[field.getOffset()];
}
public short get(String name, short defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Short.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (short)(((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public int get(String name, int defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Integer.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF);
}
public long get(String name, long defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Long.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return (long)(((prim_field_data[off++] & 0xFFL) << 56)
| ((prim_field_data[off++] & 0xFFL) << 48)
| ((prim_field_data[off++] & 0xFFL) << 40)
| ((prim_field_data[off++] & 0xFFL) << 32)
| ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public float get(String name, float defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Float.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return Float.intBitsToFloat(((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF));
}
public double get(String name, double defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field = getField(name, Double.TYPE);
if (field == null)
return defvalue;
int off = field.getOffset();
return Double.longBitsToDouble
( (long) (((prim_field_data[off++] & 0xFFL) << 56)
| ((prim_field_data[off++] & 0xFFL) << 48)
| ((prim_field_data[off++] & 0xFFL) << 40)
| ((prim_field_data[off++] & 0xFFL) << 32)
| ((prim_field_data[off++] & 0xFF) << 24)
| ((prim_field_data[off++] & 0xFF) << 16)
| ((prim_field_data[off++] & 0xFF) << 8)
| (prim_field_data[off] & 0xFF)));
}
public Object get(String name, Object defvalue)
throws IOException, IllegalArgumentException
{
ObjectStreamField field =
getField(name, defvalue == null ? null : defvalue.getClass ());
if (field == null)
return defvalue;
return objs[field.getOffset()];
}
private ObjectStreamField getField(String name, Class type)
throws IllegalArgumentException
{
ObjectStreamField field = clazz.getField(name);
boolean illegal = false;
// XXX This code is horrible and needs to be rewritten!
try
{
try
{
Class field_type = field.getType();
if (type == field_type ||
(type == null && !field_type.isPrimitive()))
{
/* See defaulted */
return field;
}
illegal = true;
throw new IllegalArgumentException
("Field requested is of type "
+ field_type.getName()
+ ", but requested type was "
+ (type == null ? "Object" : type.getName()));
}
catch (NullPointerException _)
{
/* Here we catch NullPointerException, because it may
only come from the call 'field.getType()'. If field
is null, we have to return null and classpath ethic
say we must try to avoid 'if (xxx == null)'.
*/
}
catch (IllegalArgumentException e)
{
throw e;
}
return null;
}
finally
{
/* If this is an unassigned field we should return
* the default value.
*/
if (!illegal && field != null && !field.isToSet() && field.isPersistent())
return null;
/* We do not want to modify transient fields. They should
* be left to 0.
*/
try
{
Field f = clazz.forClass().getDeclaredField(name);
if (Modifier.isTransient(f.getModifiers()))
throw new IllegalArgumentException
("no such field (non transient) " + name);
if (field == null && f.getType() != type)
throw new IllegalArgumentException
("Invalid requested type for field " + name);
}
catch (NoSuchFieldException e)
{
if (field == null)
throw new IllegalArgumentException(e);
}
}
}
};
fieldsAlreadyRead = true;
return prereadFields;
}
/**
* 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()
*/
protected ObjectInputStream()
throws IOException, SecurityException
{
SecurityManager sec_man = System.getSecurityManager();
if (sec_man != null)
sec_man.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
this.useSubclassMethod = 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 their constructor.
*
* @see #ObjectInputStream()
*/
protected Object readObjectOverride()
throws ClassNotFoundException, IOException, OptionalDataException
{
throw new IOException("Subclass of ObjectInputStream must implement readObjectOverride");
}
/**
* Assigns the next available handle to <code>obj</code>.
*
* @param obj The object for which we want a new handle.
* @param shared True if the handle should be shared
* with later calls.
* @return A valid handle for the specified object.
*/
private int assignNewHandle(Object obj, boolean shared)
{
int handle = this.nextOID;
this.nextOID = handle + 1;
rememberHandle(obj,shared,handle);
return handle;
}
/**
* Remember the object associated with the given handle.
*
* @param obj an object
* @param shared true if the reference should be shared
* with later calls.
* @param handle a handle, must be >= baseWireHandle
*
* @see #lookupHandle
*/
private void rememberHandle(Object obj, boolean shared,
int handle)
{
handles.put(handle, new Pair<Boolean,Object>(shared, obj));
}
/**
* Look up the object associated with a given handle.
*
* @param handle a handle, must be >= baseWireHandle
* @return the object remembered for handle or null if none.
* @throws StreamCorruptedException if the handle is invalid.
* @throws InvalidObjectException if the reference is not shared.
* @see #rememberHandle
*/
private Object lookupHandle(int handle)
throws ObjectStreamException
{
Pair<Boolean,Object> result = handles.get(handle);
if (result == null)
throw new StreamCorruptedException("The handle, " +
Integer.toHexString(handle) +
", is invalid.");
if (!result.getLeft())
throw new InvalidObjectException("The handle, " +
Integer.toHexString(handle) +
", is not shared.");
return result.getRight();
}
private Object processResolution(ObjectStreamClass osc, Object obj, int handle,
boolean shared)
throws IOException
{
if (osc != null && obj instanceof Serializable)
{
try
{
Method m = osc.readResolveMethod;
if(m != null)
{
obj = m.invoke(obj, new Object[] {});
}
}
catch (IllegalAccessException ignore)
{
}
catch (InvocationTargetException exception)
{
Throwable cause = exception.getCause();
if (cause instanceof ObjectStreamException)
throw (ObjectStreamException) cause;
else if (cause instanceof RuntimeException)
throw (RuntimeException) cause;
else if (cause instanceof Error)
throw (Error) cause;
}
}
if (this.resolveEnabled)
obj = resolveObject(obj);
rememberHandle(obj, shared, handle);
if (!shared)
{
if (obj instanceof byte[])
return ((byte[]) obj).clone();
if (obj instanceof short[])
return ((short[]) obj).clone();
if (obj instanceof int[])
return ((int[]) obj).clone();
if (obj instanceof long[])
return ((long[]) obj).clone();
if (obj instanceof char[])
return ((char[]) obj).clone();
if (obj instanceof boolean[])
return ((boolean[]) obj).clone();
if (obj instanceof float[])
return ((float[]) obj).clone();
if (obj instanceof double[])
return ((double[]) obj).clone();
if (obj instanceof Object[])
return ((Object[]) obj).clone();
}
return obj;
}
private void clearHandles()
{
handles.clear();
this.nextOID = baseWireHandle;
}
private void readNextBlock() throws IOException
{
byte marker = this.realInputStream.readByte();
while (marker == TC_RESET)
{
if(dump) dumpElementln("RESET");
clearHandles();
marker = this.realInputStream.readByte();
}
readNextBlock(marker);
}
private void readNextBlock(byte marker) throws IOException
{
if (marker == TC_BLOCKDATA)
{
if(dump) dumpElement("BLOCK DATA SIZE=");
this.blockDataBytes = this.realInputStream.readUnsignedByte();
if(dump) dumpElementln (Integer.toString(this.blockDataBytes));
}
else if (marker == TC_BLOCKDATALONG)
{
if(dump) dumpElement("BLOCK DATA LONG SIZE=");
this.blockDataBytes = this.realInputStream.readInt();
if(dump) dumpElementln (Integer.toString(this.blockDataBytes));
}
else
{
throw new EOFException("Attempt to read primitive data, but no data block is active.");
}
if (this.blockData.length < this.blockDataBytes)
this.blockData = new byte[this.blockDataBytes];
this.realInputStream.readFully (this.blockData, 0, this.blockDataBytes);
this.blockDataPosition = 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] = this.realInputStream.readBoolean();
return;
}
if (clazz == Byte.TYPE)
{
byte[] cast_array = (byte[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readByte();
return;
}
if (clazz == Character.TYPE)
{
char[] cast_array = (char[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readChar();
return;
}
if (clazz == Double.TYPE)
{
double[] cast_array = (double[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readDouble();
return;
}
if (clazz == Float.TYPE)
{
float[] cast_array = (float[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readFloat();
return;
}
if (clazz == Integer.TYPE)
{
int[] cast_array = (int[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readInt();
return;
}
if (clazz == Long.TYPE)
{
long[] cast_array = (long[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.readLong();
return;
}
if (clazz == Short.TYPE)
{
short[] cast_array = (short[])array;
for (int i=0; i < cast_array.length; i++)
cast_array[i] = this.realInputStream.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, ObjectStreamClass stream_osc)
throws ClassNotFoundException, IOException
{
ObjectStreamField[] fields = stream_osc.fieldMapping;
for (int i = 0; i < fields.length; i += 2)
{
ObjectStreamField stream_field = fields[i];
ObjectStreamField real_field = fields[i + 1];
boolean read_value = (stream_field != null && stream_field.getOffset() >= 0 && stream_field.isToSet());
boolean set_value = (real_field != null && real_field.isToSet());
String field_name;
char type;
if (stream_field != null)
{
field_name = stream_field.getName();
type = stream_field.getTypeCode();
}
else
{
field_name = real_field.getName();
type = real_field.getTypeCode();
}
switch(type)
{
case 'Z':
{
boolean value =
read_value ? this.realInputStream.readBoolean() : false;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setBooleanField(obj, value);
break;
}
case 'B':
{
byte value =
read_value ? this.realInputStream.readByte() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setByteField(obj, value);
break;
}
case 'C':
{
char value =
read_value ? this.realInputStream.readChar(): 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setCharField(obj, value);
break;
}
case 'D':
{
double value =
read_value ? this.realInputStream.readDouble() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setDoubleField(obj, value);
break;
}
case 'F':
{
float value =
read_value ? this.realInputStream.readFloat() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setFloatField(obj, value);
break;
}
case 'I':
{
int value =
read_value ? this.realInputStream.readInt() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setIntField(obj, value);
break;
}
case 'J':
{
long value =
read_value ? this.realInputStream.readLong() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setLongField(obj, value);
break;
}
case 'S':
{
short value =
read_value ? this.realInputStream.readShort() : 0;
if (dump && read_value && set_value)
dumpElementln(" " + field_name + ": " + value);
if (set_value)
real_field.setShortField(obj, value);
break;
}
case 'L':
case '[':
{
Object value =
read_value ? readObject() : null;
if (set_value)
real_field.setObjectField(obj, value);
break;
}
default:
throw new InternalError("Invalid type code: " + type);
}
}
}
// Toggles writing primitive data to block-data buffer.
private boolean setBlockDataMode (boolean on)
{
boolean oldmode = this.readDataFromBlock;
this.readDataFromBlock = on;
if (on)
this.dataInputStream = this.blockDataInput;
else
this.dataInputStream = this.realInputStream;
return oldmode;
}
// returns a new instance of REAL_CLASS that has been constructed
// only to the level of CONSTRUCTOR_CLASS (a super class of REAL_CLASS)
private Object newObject (Class real_class, Constructor constructor)
throws ClassNotFoundException, IOException
{
if (constructor == null)
throw new InvalidClassException("Missing accessible no-arg base class constructor for " + real_class.getName());
try
{
return VMObjectInputStream.allocateObject(real_class, constructor.getDeclaringClass(), constructor);
}
catch (InstantiationException e)
{
throw (ClassNotFoundException) new ClassNotFoundException
("Instance of " + real_class + " could not be created").initCause(e);
}
}
// runs all registered ObjectInputValidations in prioritized order
// on OBJ
private void invokeValidators() throws InvalidObjectException
{
try
{
Iterator<ValidatorAndPriority> it = currentObjectValidators.iterator();
while(it.hasNext())
{
ValidatorAndPriority vap = it.next();
ObjectInputValidation validator = vap.validator;
validator.validateObject();
}
}
finally
{
currentObjectValidators = null;
}
}
private void callReadMethod (Method readObject, Class klass, Object obj)
throws ClassNotFoundException, IOException
{
try
{
readObject.invoke(obj, new Object[] { this });
}
catch (InvocationTargetException x)
{
/* Rethrow if possible. */
Throwable exception = x.getTargetException();
if (exception instanceof RuntimeException)
throw (RuntimeException) exception;
if (exception instanceof IOException)
throw (IOException) exception;
if (exception instanceof ClassNotFoundException)
throw (ClassNotFoundException) exception;
throw (IOException) new IOException(
"Exception thrown from readObject() on " + klass).initCause(x);
}
catch (Exception x)
{
throw (IOException) new IOException(
"Failure invoking readObject() on " + klass).initCause(x);
}
// Invalidate fields which has been read through readFields.
prereadFields = null;
}
private static final int BUFFER_SIZE = 1024;
private DataInputStream realInputStream;
private DataInputStream dataInputStream;
private DataInputStream blockDataInput;
private int blockDataPosition;
private int blockDataBytes;
private byte[] blockData;
private boolean useSubclassMethod;
private int nextOID;
private boolean resolveEnabled;
private Map<Integer,Pair<Boolean,Object>> handles;
private Object currentObject;
private ObjectStreamClass currentObjectStreamClass;
private TreeSet<ValidatorAndPriority> currentObjectValidators;
private boolean readDataFromBlock;
private boolean fieldsAlreadyRead;
private Hashtable<Class,ObjectStreamClass> classLookupTable;
private GetField prereadFields;
private static boolean dump;
// The nesting depth for debugging output
private int depth = 0;
private static final boolean DEBUG = false;
private void dumpElement (String msg)
{
System.out.print(msg);
}
private void dumpElementln (String msg)
{
System.out.println(msg);
for (int i = 0; i < depth; i++)
System.out.print (" ");
System.out.print (Thread.currentThread() + ": ");
}
private void dumpElementln (String msg, Object obj)
{
try
{
System.out.print(msg);
if (java.lang.reflect.Proxy.isProxyClass(obj.getClass()))
System.out.println(obj.getClass());
else
System.out.println(obj);
}
catch (Exception _)
{
}
for (int i = 0; i < depth; i++)
System.out.print (" ");
System.out.print (Thread.currentThread() + ": ");
}
// used to keep a prioritized list of object validators
private static final class ValidatorAndPriority implements Comparable
{
int priority;
ObjectInputValidation validator;
ValidatorAndPriority (ObjectInputValidation validator, int priority)
{
this.priority = priority;
this.validator = validator;
}
public int compareTo (Object o)
{
ValidatorAndPriority vap = (ValidatorAndPriority)o;
return this.priority - vap.priority;
}
}
}
|