diff options
author | Warren Levy <warrenl@cygnus.com> | 2000-11-03 07:43:06 +0000 |
---|---|---|
committer | Warren Levy <warrenl@gcc.gnu.org> | 2000-11-03 07:43:06 +0000 |
commit | 6678181b3c0196dc782aec1bbf16182cd92c0257 (patch) | |
tree | 4592ce97a133d04d307e48d5b9937d20b94bfa47 /libjava/java/net/InetAddress.java | |
parent | 11d6fb5451edf66542a7052bb8973e42ddfb7dbf (diff) | |
download | gcc-6678181b3c0196dc782aec1bbf16182cd92c0257.zip gcc-6678181b3c0196dc782aec1bbf16182cd92c0257.tar.gz gcc-6678181b3c0196dc782aec1bbf16182cd92c0257.tar.bz2 |
InetAddress.java (addr): Renamed from 'address'.
* java/net/InetAddress.java (addr): Renamed from 'address'.
(address): New field to match Serialized Form doc.
(hostName): Renamed from 'hostname' to match Serialized Form doc.
(family): New serialization field.
(serialVersionUID): New field.
(readObject): New method.
(writeObject): New method.
(getFamily): New native method.
(InetAddress): Set family.
* java/net/natInetAddress.cc (getFamily): New method.
(addr): Renamed from 'address'.
(hostName): Renamed from 'hostname' to match Serialized Form doc.
* java/net/natPlainDatagramSocketImpl.cc (addr): Renamed from 'address'.
* java/net/natPlainSocketImpl.cc (addr): Renamed from 'address'.
Serialization mod.
From-SVN: r37222
Diffstat (limited to 'libjava/java/net/InetAddress.java')
-rw-r--r-- | libjava/java/net/InetAddress.java | 83 |
1 files changed, 64 insertions, 19 deletions
diff --git a/libjava/java/net/InetAddress.java b/libjava/java/net/InetAddress.java index 303a45b..f1a1235 100644 --- a/libjava/java/net/InetAddress.java +++ b/libjava/java/net/InetAddress.java @@ -1,6 +1,6 @@ // INetAddress.java -- An Internet Protocol (IP) address. -/* Copyright (C) 1998, 1999 Free Software Foundation +/* Copyright (C) 1998, 1999, 2000 Free Software Foundation This file is part of libgcj. @@ -9,6 +9,9 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for details. */ package java.net; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.IOException; /** * @author Per Bothner @@ -24,37 +27,79 @@ package java.net; public final class InetAddress implements java.io.Serializable { - byte[] address; - String hostname; + // 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); InetAddress (byte[] address, String hostname) { - this.address = address; - this.hostname = hostname; + addr = address; + hostName = hostname; + if (address != null) + family = getFamily (address); } public boolean isMulticastAddress () { - int len = address.length; + int len = addr.length; if (len == 4) - return (address[0] & 0xF0) == 0xE0; + return (addr[0] & 0xF0) == 0xE0; if (len == 16) - return address[0] == (byte) 0xFF; + return addr[0] == (byte) 0xFF; return false; } public String getHostName () { - if (hostname == null) + if (hostName == null) lookup (null, this, false); - return hostname; + return hostName; } public byte[] getAddress () { // An experiment shows that JDK1.2 returns a different byte array each // time. This makes sense, in terms of security. - return (byte[]) address.clone(); + return (byte[]) addr.clone(); } /* Helper function due to a CNI limitation. */ @@ -83,7 +128,7 @@ public final class InetAddress implements java.io.Serializable public String getHostAddress () { StringBuffer sbuf = new StringBuffer(40); - int len = address.length; + int len = addr.length; int i = 0; if (len == 16) { // An IPv6 address. @@ -91,7 +136,7 @@ public final class InetAddress implements java.io.Serializable { if (i >= 16) return sbuf.toString(); - int x = ((address[i] & 0xFF) << 8) | (address[i+1] & 0xFF); + int x = ((addr[i] & 0xFF) << 8) | (addr[i+1] & 0xFF); boolean empty = sbuf.length() == 0; if (empty) { @@ -116,7 +161,7 @@ public final class InetAddress implements java.io.Serializable } for ( ; ; ) { - sbuf.append(address[i] & 0xFF); + sbuf.append(addr[i] & 0xFF); i++; if (i == len) break; @@ -130,10 +175,10 @@ public final class InetAddress implements java.io.Serializable // There hashing algorithm is not specified, but a simple experiment // shows that it is equal to the address, as a 32-bit big-endian integer. int hash = 0; - int len = address.length; + int len = addr.length; int i = len > 4 ? len - 4 : 0; for ( ; i < len; i++) - hash = (hash << 8) | (address[i] & 0xFF); + hash = (hash << 8) | (addr[i] & 0xFF); return hash; } @@ -147,8 +192,8 @@ public final class InetAddress implements java.io.Serializable // different host names." This violates the description in the // JDK 1.2 API documentation. A little experiementation // shows that the latter is correct. - byte[] addr1 = address; - byte[] addr2 = ((InetAddress) obj).address; + byte[] addr1 = addr; + byte[] addr2 = ((InetAddress) obj).addr; if (addr1.length != addr2.length) return false; for (int i = addr1.length; --i >= 0; ) @@ -208,7 +253,7 @@ public final class InetAddress implements java.io.Serializable // However, if there is a security manager, and the cached result // is other than "localhost", we need to check again. if (localhost == null - || (s != null && localhost.address != localhostAddress)) + || (s != null && localhost.addr != localhostAddress)) getLocalHost(s); return localhost; } |