2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / net / Inet6Address.java
blobc3418c654a951f6ffa3d46ac7f74b37d8c58c9b2
1 /* Inet6Address.java
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.net;
41 import java.util.Arrays;
43 /**
44 * @author Michael Koch
45 * @date August 3, 2002.
49 * Written using on-line Java Platform 1.4 API Specification and
50 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt)
51 * Status: Believed complete and correct.
54 public final class Inet6Address extends InetAddress
56 static final long serialVersionUID = 6880410070516793377L;
58 /**
59 * Needed for serialization
61 byte[] ipaddress;
63 /**
64 * Create an Inet6Address object
66 * @param addr The IP address
67 * @param host The hostname
69 Inet6Address (byte[] addr, String host)
71 super (addr, host);
72 this.ipaddress = addr;
75 /**
76 * Utility routine to check if the InetAddress is an IP multicast address
78 * @since 1.1
80 public boolean isMulticastAddress ()
82 return ipaddress [0] == 0xFF;
85 /**
86 * Utility routine to check if the InetAddress in a wildcard address
88 * @since 1.4
90 public boolean isAnyLocalAddress ()
92 byte[] anylocal = { 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0 };
95 return Arrays.equals(ipaddress, anylocal);
98 /**
99 * Utility routine to check if the InetAddress is a loopback address
101 * @since 1.4
103 public boolean isLoopbackAddress ()
105 byte[] loopback = { 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 1 };
108 return Arrays.equals(ipaddress, loopback);
112 * Utility routine to check if the InetAddress is an link local address
114 * @since 1.4
116 public boolean isLinkLocalAddress ()
118 return ipaddress [0] == 0xFA;
122 * Utility routine to check if the InetAddress is a site local address
124 * @since 1.4
126 public boolean isSiteLocalAddress ()
128 return ipaddress [0] == 0xFB;
132 * Utility routine to check if the multicast address has global scope
134 * @since 1.4
136 public boolean isMCGlobal ()
138 if (!isMulticastAddress ())
139 return false;
141 return (ipaddress [1] & 0x0F) == 0xE;
145 * Utility routine to check if the multicast address has node scope
147 * @since 1.4
149 public boolean isMCNodeLocal ()
151 if (!isMulticastAddress ())
152 return false;
154 return (ipaddress [1] & 0x0F) == 0x1;
158 * Utility routine to check if the multicast address has link scope
160 * @since 1.4
162 public boolean isMCLinkLocal ()
164 if (!isMulticastAddress ())
165 return false;
167 return (ipaddress [1] & 0x0F) == 0x2;
171 * Utility routine to check if the multicast address has site scope
173 * @since 1.4
175 public boolean isMCSiteLocal ()
177 if (!isMulticastAddress ())
178 return false;
180 return (ipaddress [1] & 0x0F) == 0x5;
184 * Utility routine to check if the multicast address has organization scope
186 * @since 1.4
188 public boolean isMCOrgLocal ()
190 if (!isMulticastAddress ())
191 return false;
193 return (ipaddress [1] & 0x0F) == 0x8;
197 * Returns the raw IP address of this InetAddress object. The result is in
198 * network byte order: the highest order byte of the address is i
199 * n getAddress()[0]
201 public byte[] getAddress ()
203 return ipaddress;
207 * Returns the IP address string in textual presentation
209 public String getHostAddress ()
211 StringBuffer sbuf = new StringBuffer (40);
213 for (int i = 0; i < 16; i += 2)
215 int x = ((ipaddress [i] & 0xFF) << 8) | (ipaddress [i + 1] & 0xFF);
216 boolean empty = sbuf.length () == 0;
218 if (empty)
220 if (i > 0)
221 sbuf.append ("::");
223 else
224 sbuf.append (':');
226 if (x != 0 || i >= 14)
227 sbuf.append (Integer.toHexString (x));
230 return sbuf.toString ();
234 * Returns a hashcode for this IP address
236 public int hashCode ()
238 return super.hashCode ();
242 * Compares this object against the specified object
244 public boolean equals (Object obj)
246 if (! (obj instanceof Inet6Address))
247 return false;
249 Inet6Address tmp = (Inet6Address) obj;
251 return super.equals (tmp)
252 && this.ipaddress == tmp.ipaddress;
256 * Utility routine to check if the InetAddress is an
257 * IPv4 compatible IPv6 address
259 * @since 1.4
261 public boolean isIPv4CompatibleAddress ()
263 if (ipaddress [0] != 0x00 || ipaddress [1] != 0x00 ||
264 ipaddress [2] != 0x00 || ipaddress [3] != 0x00 ||
265 ipaddress [4] != 0x00 || ipaddress [5] != 0x00 ||
266 ipaddress [6] != 0x00 || ipaddress [7] != 0x00 ||
267 ipaddress [8] != 0x00 || ipaddress [9] != 0x00 ||
268 ipaddress [10] != 0x00 || ipaddress [11] != 0x00)
269 return false;
271 return true;