Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / net / Inet6Address.java
blob69d266a4c1764c18d451adba3be5a3cfea18fd92
1 /* Inet6Address.java --
2 Copyright (C) 2002, 2003, 2004 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;
44 * Written using on-line Java Platform 1.4 API Specification and
45 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt)
47 * @author Michael Koch
48 * @status Believed complete and correct.
50 public final class Inet6Address extends InetAddress
52 static final long serialVersionUID = 6880410070516793377L;
54 /**
55 * Needed for serialization
57 byte[] ipaddress;
59 /**
60 * Create an Inet6Address object
62 * @param addr The IP address
63 * @param host The hostname
65 Inet6Address(byte[] addr, String host)
67 super(addr, host);
68 // Super constructor clones the addr. Get a reference to the clone.
69 this.ipaddress = this.addr;
72 /**
73 * Utility routine to check if the InetAddress is an IP multicast address
75 * @since 1.1
77 public boolean isMulticastAddress()
79 return ipaddress[0] == 0xFF;
82 /**
83 * Utility routine to check if the InetAddress in a wildcard address
85 * @since 1.4
87 public boolean isAnyLocalAddress()
89 byte[] anylocal = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
91 return Arrays.equals(ipaddress, anylocal);
94 /**
95 * Utility routine to check if the InetAddress is a loopback address
97 * @since 1.4
99 public boolean isLoopbackAddress()
101 byte[] loopback = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
103 return Arrays.equals(ipaddress, loopback);
107 * Utility routine to check if the InetAddress is an link local address
109 * @since 1.4
111 public boolean isLinkLocalAddress()
113 return ipaddress[0] == 0xFA;
117 * Utility routine to check if the InetAddress is a site local address
119 * @since 1.4
121 public boolean isSiteLocalAddress()
123 return ipaddress[0] == 0xFB;
127 * Utility routine to check if the multicast address has global scope
129 * @since 1.4
131 public boolean isMCGlobal()
133 if (! isMulticastAddress())
134 return false;
136 return (ipaddress[1] & 0x0F) == 0xE;
140 * Utility routine to check if the multicast address has node scope
142 * @since 1.4
144 public boolean isMCNodeLocal()
146 if (! isMulticastAddress())
147 return false;
149 return (ipaddress[1] & 0x0F) == 0x1;
153 * Utility routine to check if the multicast address has link scope
155 * @since 1.4
157 public boolean isMCLinkLocal()
159 if (! isMulticastAddress())
160 return false;
162 return (ipaddress[1] & 0x0F) == 0x2;
166 * Utility routine to check if the multicast address has site scope
168 * @since 1.4
170 public boolean isMCSiteLocal()
172 if (! isMulticastAddress())
173 return false;
175 return (ipaddress[1] & 0x0F) == 0x5;
179 * Utility routine to check if the multicast address has organization scope
181 * @since 1.4
183 public boolean isMCOrgLocal()
185 if (! isMulticastAddress())
186 return false;
188 return (ipaddress[1] & 0x0F) == 0x8;
192 * Returns the raw IP address of this InetAddress object. The result is in
193 * network byte order: the highest order byte of the address is i
194 * n getAddress()[0]
196 public byte[] getAddress()
198 return (byte[]) ipaddress.clone();
202 * Returns the IP address string in textual presentation
204 public String getHostAddress()
206 StringBuffer sbuf = new StringBuffer(40);
208 for (int i = 0; i < 16; i += 2)
210 int x = ((ipaddress[i] & 0xFF) << 8) | (ipaddress[i + 1] & 0xFF);
212 if (i > 0)
213 sbuf.append(':');
215 sbuf.append(Integer.toHexString(x));
218 return sbuf.toString();
222 * Returns a hashcode for this IP address
224 public int hashCode()
226 return super.hashCode();
230 * Compares this object against the specified object
232 public boolean equals(Object obj)
234 if (! (obj instanceof Inet6Address))
235 return false;
237 // this.ipaddress is never set in this class except to
238 // the value of the super class' addr. The super classes
239 // equals(Object) will do the compare.
240 return super.equals(obj);
244 * Utility routine to check if the InetAddress is an
245 * IPv4 compatible IPv6 address
247 * @since 1.4
249 public boolean isIPv4CompatibleAddress()
251 if (ipaddress[0] != 0x00 || ipaddress[1] != 0x00 || ipaddress[2] != 0x00
252 || ipaddress[3] != 0x00 || ipaddress[4] != 0x00
253 || ipaddress[5] != 0x00 || ipaddress[6] != 0x00
254 || ipaddress[7] != 0x00 || ipaddress[8] != 0x00
255 || ipaddress[9] != 0x00 || ipaddress[10] != 0x00
256 || ipaddress[11] != 0x00)
257 return false;
259 return true;