+ // The Serialized Form specifies that an int 'address' is saved/restored.
+ // This class uses a byte array internally so we'll just do the conversion
+ // at serialization time and leave the rest of the algorithm as is.
+ private int address;
+ transient byte[] addr;
+ String hostName;
+ // The field 'family' seems to be the AF_ value.
+ // FIXME: Much of the code in the other java.net classes does not make
+ // use of this family field. A better implementation would be to make
+ // use of getaddrinfo() and have other methods just check the family
+ // field rather than examining the length of the address each time.
+ int family;
+ private static final long serialVersionUID = 3286316764910316507L;
+
+ private void readObject(ObjectInputStream ois)
+ throws IOException, ClassNotFoundException
+ {
+ ois.defaultReadObject();
+ addr = new byte[4];
+ addr[3] = (byte) address;
+ for (int i = 2; i >= 0; --i)
+ addr[i] = (byte) (address >>= 8);
+ // Ignore family from serialized data. Since the saved address is 32 bits
+ // the deserialized object will have an IPv4 address i.e. AF_INET family.
+ // FIXME: An alternative is to call the aton method on the deserialized
+ // hostname to get a new address. The Serialized Form doc is silent
+ // on how these fields are used.
+ family = getFamily (addr);
+ }
+
+ private void writeObject(ObjectOutputStream oos) throws IOException
+ {
+ // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address
+ // or a 16 byte IPv6 address.
+ int len = addr.length;
+ int i = len - 4;
+ for (; i < len; i++)
+ address = address << 8 | (((int) addr[i]) & 0xFF);
+ oos.defaultWriteObject();
+ }
+
+ private static native int getFamily (byte[] address);