1 |
/* ObjectInputStream.java -- Class used to read serialized objects |
/* ObjectInputStream.java -- Class used to read serialized objects |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
73 |
public ObjectInputStream (InputStream in) |
public ObjectInputStream (InputStream in) |
74 |
throws IOException, StreamCorruptedException |
throws IOException, StreamCorruptedException |
75 |
{ |
{ |
76 |
|
if (Configuration.DEBUG) |
77 |
|
{ |
78 |
|
String val = System.getProperty("gcj.dumpobjects"); |
79 |
|
if (dump == false && val != null && !val.equals("")) |
80 |
|
{ |
81 |
|
dump = true; |
82 |
|
System.out.println ("Serialization debugging enabled"); |
83 |
|
} |
84 |
|
else if (dump == true && (val == null || val.equals(""))) |
85 |
|
{ |
86 |
|
dump = false; |
87 |
|
System.out.println ("Serialization debugging disabled"); |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
this.resolveEnabled = false; |
this.resolveEnabled = false; |
92 |
this.isDeserializing = false; |
this.isDeserializing = false; |
93 |
this.blockDataPosition = 0; |
this.blockDataPosition = 0; |
583 |
} |
} |
584 |
} |
} |
585 |
|
|
586 |
|
|
587 |
|
/** |
588 |
|
Allows subclasses to resolve objects that are read from the |
589 |
|
stream with other objects to be returned in their place. This |
590 |
|
method is called the first time each object is encountered. |
591 |
|
|
592 |
|
This method must be enabled before it will be called in the |
593 |
|
serialization process. |
594 |
|
|
595 |
|
@exception IOException Exception from underlying |
596 |
|
<code>OutputStream</code>. |
597 |
|
|
598 |
|
@see enableResolveObject (boolean) |
599 |
|
*/ |
600 |
|
protected Object resolveObject (Object obj) throws IOException |
601 |
|
{ |
602 |
|
return obj; |
603 |
|
} |
604 |
|
|
605 |
|
|
606 |
protected Class resolveProxyClass (String[] intfs) |
protected Class resolveProxyClass (String[] intfs) |
607 |
throws IOException, ClassNotFoundException |
throws IOException, ClassNotFoundException |
608 |
{ |
{ |
630 |
} |
} |
631 |
|
|
632 |
/** |
/** |
|
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; |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
633 |
If <code>enable</code> is <code>true</code> and this object is |
If <code>enable</code> is <code>true</code> and this object is |
634 |
trusted, then <code>resolveObject (Object)</code> will be called |
trusted, then <code>resolveObject (Object)</code> will be called |
635 |
in subsequent calls to <code>readObject (Object)</code>. |
in subsequent calls to <code>readObject (Object)</code>. |
681 |
{ |
{ |
682 |
if (this.blockDataPosition >= this.blockDataBytes) |
if (this.blockDataPosition >= this.blockDataBytes) |
683 |
readNextBlock (); |
readNextBlock (); |
684 |
return this.blockData[this.blockDataPosition++]; |
return (this.blockData[this.blockDataPosition++] & 0xff); |
685 |
} |
} |
686 |
else |
else |
687 |
return this.realInputStream.read (); |
return this.realInputStream.read (); |
688 |
} |
} |
689 |
|
|
690 |
public int read (byte data[], int offset, int length) throws IOException |
public int read (byte[] data, int offset, int length) throws IOException |
691 |
{ |
{ |
692 |
if (this.readDataFromBlock) |
if (this.readDataFromBlock) |
693 |
{ |
{ |
1518 |
|
|
1519 |
|
|
1520 |
// returns a new instance of REAL_CLASS that has been constructed |
// returns a new instance of REAL_CLASS that has been constructed |
1521 |
// only to th level of CONSTRUCTOR_CLASS (a super class of REAL_CLASS) |
// only to the level of CONSTRUCTOR_CLASS (a super class of REAL_CLASS) |
1522 |
private Object newObject (Class real_class, Class constructor_class) |
private Object newObject (Class real_class, Class constructor_class) |
1523 |
{ |
{ |
1524 |
try |
try |
1558 |
// of the same name in SecurityManger |
// of the same name in SecurityManger |
1559 |
private static native ClassLoader currentClassLoader (SecurityManager sm); |
private static native ClassLoader currentClassLoader (SecurityManager sm); |
1560 |
|
|
1561 |
private native void callReadMethod (Object obj, Class clazz); |
private static Field getField (Class klass, String name) |
1562 |
|
throws java.lang.NoSuchFieldException |
1563 |
|
{ |
1564 |
|
return klass.getDeclaredField(name); |
1565 |
|
} |
1566 |
|
|
1567 |
|
private static Method getMethod (Class klass, String name, Class args[]) |
1568 |
|
throws java.lang.NoSuchMethodException |
1569 |
|
{ |
1570 |
|
return klass.getDeclaredMethod(name, args); |
1571 |
|
} |
1572 |
|
|
1573 |
|
private void callReadMethod (Object obj, Class klass) throws IOException |
1574 |
|
{ |
1575 |
|
try |
1576 |
|
{ |
1577 |
|
Class classArgs[] = {ObjectInputStream.class}; |
1578 |
|
Method m = getMethod (klass, "readObject", classArgs); |
1579 |
|
if (m == null) |
1580 |
|
return; |
1581 |
|
Object args[] = {this}; |
1582 |
|
m.invoke (obj, args); |
1583 |
|
} |
1584 |
|
catch (InvocationTargetException x) |
1585 |
|
{ |
1586 |
|
/* Rethrow if possible. */ |
1587 |
|
Throwable exception = x.getTargetException(); |
1588 |
|
if (exception instanceof RuntimeException) |
1589 |
|
throw (RuntimeException) exception; |
1590 |
|
if (exception instanceof IOException) |
1591 |
|
throw (IOException) exception; |
1592 |
|
|
1593 |
|
throw new IOException ("Exception thrown from readObject() on " + |
1594 |
|
klass + ": " + exception.getClass().getName()); |
1595 |
|
} |
1596 |
|
catch (Exception x) |
1597 |
|
{ |
1598 |
|
throw new IOException ("Failure invoking readObject() on " + |
1599 |
|
klass + ": " + x.getClass().getName()); |
1600 |
|
} |
1601 |
|
} |
1602 |
|
|
1603 |
private native Object allocateObject (Class clazz) |
private native Object allocateObject (Class clazz) |
1604 |
throws InstantiationException; |
throws InstantiationException; |
1605 |
|
|
1606 |
private native void callConstructor (Class clazz, Object obj); |
private native void callConstructor (Class clazz, Object obj); |
1607 |
|
|
1608 |
private native void setBooleanField (Object obj, String field_name, |
private void setBooleanField (Object obj, String field_name, |
1609 |
boolean val); |
boolean val) |
1610 |
private native void setByteField (Object obj, String field_name, |
{ |
1611 |
byte val); |
try |
1612 |
private native void setCharField (Object obj, String field_name, |
{ |
1613 |
char val); |
Class klass = obj.getClass (); |
1614 |
private native void setDoubleField (Object obj, String field_name, |
Field f = getField (klass, field_name); |
1615 |
double val); |
f.setAccessible(true); |
1616 |
private native void setFloatField (Object obj, String field_name, |
f.setBoolean (obj, val); |
1617 |
float val); |
} |
1618 |
private native void setIntField (Object obj, String field_name, |
catch (Exception _) |
1619 |
int val); |
{ |
1620 |
private native void setLongField (Object obj, String field_name, |
} |
1621 |
long val); |
} |
1622 |
private native void setShortField (Object obj, String field_name, |
|
1623 |
short val); |
private void setByteField (Object obj, String field_name, |
1624 |
private native void setObjectField (Object obj, String field_name, |
byte val) |
1625 |
String type_code, Object val); |
{ |
1626 |
|
try |
1627 |
|
{ |
1628 |
|
Class klass = obj.getClass (); |
1629 |
|
Field f = getField (klass, field_name); |
1630 |
|
f.setAccessible(true); |
1631 |
|
f.setByte (obj, val); |
1632 |
|
} |
1633 |
|
catch (Exception _) |
1634 |
|
{ |
1635 |
|
} |
1636 |
|
} |
1637 |
|
|
1638 |
|
private void setCharField (Object obj, String field_name, |
1639 |
|
char val) |
1640 |
|
{ |
1641 |
|
try |
1642 |
|
{ |
1643 |
|
Class klass = obj.getClass (); |
1644 |
|
Field f = getField (klass, field_name); |
1645 |
|
f.setAccessible(true); |
1646 |
|
f.setChar (obj, val); |
1647 |
|
} |
1648 |
|
catch (Exception _) |
1649 |
|
{ |
1650 |
|
} |
1651 |
|
} |
1652 |
|
|
1653 |
|
private void setDoubleField (Object obj, String field_name, |
1654 |
|
double val) |
1655 |
|
{ |
1656 |
|
try |
1657 |
|
{ |
1658 |
|
Class klass = obj.getClass (); |
1659 |
|
Field f = getField (klass, field_name); |
1660 |
|
f.setAccessible(true); |
1661 |
|
f.setDouble (obj, val); |
1662 |
|
} |
1663 |
|
catch (Exception _) |
1664 |
|
{ |
1665 |
|
} |
1666 |
|
} |
1667 |
|
|
1668 |
|
private void setFloatField (Object obj, String field_name, |
1669 |
|
float val) |
1670 |
|
{ |
1671 |
|
try |
1672 |
|
{ |
1673 |
|
Class klass = obj.getClass (); |
1674 |
|
Field f = getField (klass, field_name); |
1675 |
|
f.setAccessible(true); |
1676 |
|
f.setFloat (obj, val); |
1677 |
|
} |
1678 |
|
catch (Exception _) |
1679 |
|
{ |
1680 |
|
} |
1681 |
|
} |
1682 |
|
|
1683 |
|
private void setIntField (Object obj, String field_name, |
1684 |
|
int val) |
1685 |
|
{ |
1686 |
|
try |
1687 |
|
{ |
1688 |
|
Class klass = obj.getClass (); |
1689 |
|
Field f = getField (klass, field_name); |
1690 |
|
f.setAccessible(true); |
1691 |
|
f.setInt (obj, val); |
1692 |
|
} |
1693 |
|
catch (Exception _) |
1694 |
|
{ |
1695 |
|
} |
1696 |
|
} |
1697 |
|
|
1698 |
|
|
1699 |
|
private void setLongField (Object obj, String field_name, |
1700 |
|
long val) |
1701 |
|
{ |
1702 |
|
try |
1703 |
|
{ |
1704 |
|
Class klass = obj.getClass (); |
1705 |
|
Field f = getField (klass, field_name); |
1706 |
|
f.setAccessible(true); |
1707 |
|
f.setLong (obj, val); |
1708 |
|
} |
1709 |
|
catch (Exception _) |
1710 |
|
{ |
1711 |
|
} |
1712 |
|
} |
1713 |
|
|
1714 |
|
|
1715 |
|
private void setShortField (Object obj, String field_name, |
1716 |
|
short val) |
1717 |
|
{ |
1718 |
|
try |
1719 |
|
{ |
1720 |
|
Class klass = obj.getClass (); |
1721 |
|
Field f = getField (klass, field_name); |
1722 |
|
f.setAccessible(true); |
1723 |
|
f.setShort (obj, val); |
1724 |
|
} |
1725 |
|
catch (Exception _) |
1726 |
|
{ |
1727 |
|
} |
1728 |
|
} |
1729 |
|
|
1730 |
|
|
1731 |
|
private void setObjectField (Object obj, String field_name, String type_code, |
1732 |
|
Object val) |
1733 |
|
{ |
1734 |
|
try |
1735 |
|
{ |
1736 |
|
Class klass = obj.getClass (); |
1737 |
|
Field f = getField (klass, field_name); |
1738 |
|
f.setAccessible(true); |
1739 |
|
// FIXME: We should check the type_code here |
1740 |
|
f.set (obj, val); |
1741 |
|
} |
1742 |
|
catch (Exception _) |
1743 |
|
{ |
1744 |
|
} |
1745 |
|
} |
1746 |
|
|
1747 |
private static final int BUFFER_SIZE = 1024; |
private static final int BUFFER_SIZE = 1024; |
1748 |
private static final Class[] readObjectParams = { ObjectInputStream.class }; |
private static final Class[] readObjectParams = { ObjectInputStream.class }; |
1749 |
|
|
1766 |
|
|
1767 |
private static boolean dump = false; |
private static boolean dump = false; |
1768 |
|
|
|
public static void setDump (boolean dodump) |
|
|
{ |
|
|
dump = dodump; |
|
|
} |
|
|
|
|
1769 |
private void dumpElement (String msg) |
private void dumpElement (String msg) |
1770 |
{ |
{ |
1771 |
if (dump) |
if (Configuration.DEBUG && dump) |
1772 |
System.out.print (msg); |
System.out.print(msg); |
1773 |
} |
} |
1774 |
|
|
1775 |
private void dumpElementln (String msg) |
private void dumpElementln (String msg) |
1776 |
{ |
{ |
1777 |
if (dump) |
if (Configuration.DEBUG && dump) |
1778 |
System.out.println (msg); |
System.out.println(msg); |
1779 |
} |
} |
1780 |
|
|
1781 |
static |
static |