summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven de Marothy <sven@physto.se>2006-07-18 02:58:13 +0000
committerSven de Marothy <sven@physto.se>2006-07-18 02:58:13 +0000
commit82ef255db7e61349497eb5de988d07e3be9a7d60 (patch)
tree616f4c2b9679edf108a24448320e9b3e9f9a22cb
parentb5b2f77197665f8ef7f09a7eb586ab41ec93ba8a (diff)
downloadclasspath-82ef255db7e61349497eb5de988d07e3be9a7d60.tar.gz
2006-07-18 Sven de Marothy <sven@physto.se>
* java/net/Inet6Address.java: Add 1.5 serialized fields. (getByAddress): New methods. (readObject, writeObject): New methods. (equals): Reimplement.
-rw-r--r--ChangeLog8
-rw-r--r--java/net/Inet6Address.java139
2 files changed, 142 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index af8153827..9f4e62530 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2006-07-18 Sven de Marothy <sven@physto.se>
+
+ * java/net/Inet6Address.java:
+ Add 1.5 serialized fields.
+ (getByAddress): New methods.
+ (readObject, writeObject): New methods.
+ (equals): Reimplement.
+
2006-07-18 David Gilbert <david.gilbert@object-refinery.com>
* java/awt/image/Raster.java: Added API docs and reformatted source
diff --git a/java/net/Inet6Address.java b/java/net/Inet6Address.java
index 0d62fe919..6f41b792b 100644
--- a/java/net/Inet6Address.java
+++ b/java/net/Inet6Address.java
@@ -39,13 +39,16 @@ exception statement from your version. */
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 @@ public final class Inet6Address extends InetAddress
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 @@ public final class Inet6Address extends InetAddress
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 @@ public final class Inet6Address extends InetAddress
}
/**
+ * 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 @@ public final class Inet6Address extends InetAddress
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 @@ public final class Inet6Address extends InetAddress
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 @@ public final class Inet6Address extends InetAddress
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();
+ }
}