2006-07-18 Sven de Marothy <[EMAIL PROTECTED]> * java/net/Inet6Address.java: Add 1.5 serialized fields. (getByAddress): New methods. (readObject, writeObject): New methods. (equals): Reimplement.
Index: java/net/Inet6Address.java =================================================================== RCS file: /sources/classpath/classpath/java/net/Inet6Address.java,v retrieving revision 1.11 diff -U3 -r1.11 Inet6Address.java --- java/net/Inet6Address.java 2 Jul 2005 20:32:39 -0000 1.11 +++ java/net/Inet6Address.java 18 Jul 2006 02:55:16 -0000 @@ -39,13 +39,16 @@ package java.net; import java.util.Arrays; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.IOException; /* * Written using on-line Java Platform 1.4 API Specification and * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt) * * @author Michael Koch - * @status Believed complete and correct. + * @status Updated to 1.5. Serialization compatibility is tested. */ public final class Inet6Address extends InetAddress { @@ -57,6 +60,39 @@ byte[] ipaddress; /** + * The scope ID, if any. + * @since 1.5 + * @serial + */ + private int scope_id; + + /** + * The scope ID, if any. + * @since 1.5 + * @serial + */ + private boolean scope_id_set; + + /** + * Whether ifname is set or not. + * @since 1.5 + * @serial + */ + private boolean scope_ifname_set; + + /** + * Name of the network interface, used only by the serialization methods + * @since 1.5 + * @serial + */ + private String ifname; + + /** + * Scope network interface, or <code>null</code>. + */ + private transient NetworkInterface nif; + + /** * Create an Inet6Address object * * @param addr The IP address @@ -67,6 +103,10 @@ super(addr, host); // Super constructor clones the addr. Get a reference to the clone. this.ipaddress = this.addr; + ifname = null; + scope_ifname_set = scope_id_set = false; + scope_id = 0; + nif = null; } /** @@ -199,6 +239,43 @@ } /** + * Creates a scoped Inet6Address where the scope has an integer id. + * + * @throws UnkownHostException if the address is an invalid number of bytes. + */ + public static Inet6Address getByAddress(String host, byte[] addr, + int scopeId) + throws UnknownHostException + { + if( addr.length != 16 ) + throw new UnknownHostException("Illegal address length: " + addr.length + + " bytes."); + Inet6Address ip = new Inet6Address( addr, host ); + ip.scope_id = scopeId; + ip.scope_id_set = true; + return ip; + } + + /** + * Creates a scoped Inet6Address where the scope is a given + * NetworkInterface. + * + * @throws UnkownHostException if the address is an invalid number of bytes. + */ + public static Inet6Address getByAddress(String host, byte[] addr, + NetworkInterface nif) + throws UnknownHostException + { + if( addr.length != 16 ) + throw new UnknownHostException("Illegal address length: " + addr.length + + " bytes."); + Inet6Address ip = new Inet6Address( addr, host ); + ip.nif = nif; + + return ip; + } + + /** * Returns the IP address string in textual presentation */ public String getHostAddress() @@ -214,12 +291,17 @@ sbuf.append(Integer.toHexString(x)); } + if( nif != null ) + sbuf.append( "%" + nif.getName() ); + else if( scope_id_set ) + sbuf.append( "%" + scope_id ); return sbuf.toString(); } /** * Returns a hashcode for this IP address + * (The hashcode is independent of scope) */ public int hashCode() { @@ -234,10 +316,23 @@ if (! (obj instanceof Inet6Address)) return false; - // this.ipaddress is never set in this class except to - // the value of the super class' addr. The super classes - // equals(Object) will do the compare. - return super.equals(obj); + Inet6Address ip = (Inet6Address)obj; + if (ipaddress.length != ip.ipaddress.length) + return false; + + for (int i = 0; i < ip.ipaddress.length; i++) + if (ipaddress[i] != ip.ipaddress[i]) + return false; + + if( ip.nif != null && nif != null ) + return nif.equals( ip.nif ); + if( ip.nif != nif ) + return false; + if( ip.scope_id_set != scope_id_set ) + return false; + if( scope_id_set ) + return (scope_id == ip.scope_id); + return true; } /** @@ -258,4 +353,38 @@ return true; } + + /** + * Required for 1.5-compatible serialization. + * @since 1.5 + */ + private void readObject(ObjectInputStream s) + throws IOException, ClassNotFoundException + { + s.defaultReadObject(); + try + { + if( scope_ifname_set ) + nif = NetworkInterface.getByName( ifname ); + } + catch( SocketException se ) + { + // FIXME: Ignore this? or throw an IOException? + } + } + + /** + * Required for 1.5-compatible serialization. + * @since 1.5 + */ + private void writeObject(ObjectOutputStream s) + throws IOException + { + if( nif != null ) + { + ifname = nif.getName(); + scope_ifname_set = true; + } + s.defaultWriteObject(); + } }