]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/net/Inet4Address.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)
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
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
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. */
40 import java
.io
.ObjectStreamException
;
41 import java
.util
.Arrays
;
45 * @author Michael Koch
46 * @date August 3, 2002.
50 * Written using on-line Java Platform 1.4 API Specification and
51 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt),
52 * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt),
53 * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt)
54 * Status: Believed complete and correct.
56 public final class Inet4Address
extends InetAddress
59 * For compatability with Sun's JDK 1.4.2 rev. 5
61 static final long serialVersionUID
= 3286316764910316507L;
64 * needed for serialization
66 private Object
writeReplace() throws ObjectStreamException
68 return new InetAddress(addr
, hostName
);
72 * Creates a Inet4Address
74 * @param addr The IP address
75 * @param host The Hostname
77 Inet4Address(byte[] addr
, String host
)
83 * Checks if the address is a multicast address
87 public boolean isMulticastAddress()
89 return (addr
[0] & 0xF0) == 0xE0;
93 * Checks if this address is a loopback address
95 public boolean isLoopbackAddress()
97 return addr
[0] == 0x7F;
101 * Checks if this address is a wildcard address
105 public boolean isAnyLocalAddress()
107 byte[] anylocal
= { 0, 0, 0, 0 };
109 return Arrays
.equals(addr
, anylocal
);
113 * Checks if this address is a link local address
117 public boolean isLinkLocalAddress()
119 // XXX: This seems to not exist with IPv4 addresses
124 * Checks if this address is a site local address
128 public boolean isSiteLocalAddress()
134 // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here:
135 // it says 172.16.0.0 - 172.255.255.255 are site local addresses
138 if (addr
[0] == 0xAC && (addr
[1] & 0xF0) == 0x01)
142 if (addr
[0] == 0xC0 && addr
[1] == 0xA8)
145 // XXX: Do we need to check more addresses here ?
150 * Checks if this multicast address has global scope
154 public boolean isMCGlobal()
156 // XXX: This seems to net exist with IPv4 addresses
161 * Checks if this multicast address has node scope
165 public boolean isMCNodeLocal()
167 // XXX: This seems to net exist with IPv4 addresses
172 * Checks if this multicast address has link scope
176 public boolean isMCLinkLocal()
178 if (! isMulticastAddress())
181 return (addr
[0] == 0xE0) && (addr
[1] == 0x00) && (addr
[2] == 0x00);
185 * Checks if this multicast address has site scope
189 public boolean isMCSiteLocal()
191 // XXX: This seems to net exist with IPv4 addresses
196 * Checks if this multicast address has organization scope
200 public boolean isMCOrgLocal()
202 // XXX: This seems to net exist with IPv4 addresses
207 * Returns the address of the current instance
209 public byte[] getAddress()
215 * Returns the address as string
219 public String
getHostAddress()
221 StringBuffer sbuf
= new StringBuffer(40);
222 int len
= addr
.length
;
227 sbuf
.append(addr
[i
] & 0xFF);
236 return sbuf
.toString();
240 * Computes the hashcode of the instance
242 public int hashCode()
245 int len
= addr
.length
;
246 int i
= len
> 4 ? len
- 4 : 0;
249 hash
= (hash
<< 8) | (addr
[i
] & 0xFF);
255 * Compare the current Inet4Address instance with obj
257 * @param obj Object to compare with
259 public boolean equals(Object obj
)
261 if (! (obj
instanceof InetAddress
))
265 byte[] addr2
= ((InetAddress
) obj
).addr
;
267 if (addr1
.length
!= addr2
.length
)
270 for (int i
= addr1
.length
; --i
>= 0;)
271 if (addr1
[i
] != addr2
[i
])
276 } // class Inet4Address
This page took 0.048301 seconds and 5 git commands to generate.