source: trunk/gcc/libjava/java/net/Inet4Address.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.1 KB
Line 
1/* Inet4Address.java
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.net;
39
40import java.io.IOException;
41import java.io.ObjectStreamException;
42
43/**
44 * @author Michael Koch
45 * @date August 3, 2002.
46 */
47
48/*
49 * Written using on-line Java Platform 1.4 API Specification and
50 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt),
51 * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt),
52 * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt)
53 * Status: Believed complete and correct.
54 */
55
56public final class Inet4Address extends InetAddress
57{
58 static final long serialVersionUID = 7615067291688066509L;
59
60 /**
61 * needed for serialization
62 */
63 private Object writeReplace () throws ObjectStreamException
64 {
65 return new InetAddress (addr, hostName);
66 }
67
68 /**
69 * Creates a Inet4Address
70 *
71 * @param addr The IP address
72 * @param host The Hostname
73 */
74 protected Inet4Address(byte[] addr, String host)
75 {
76 super (addr, host);
77 }
78
79 /**
80 * Checks if the address is a multicast address
81 *
82 * @since 1.1
83 */
84 public boolean isMulticastAddress ()
85 {
86 return (addr [0] & 0xF0) == 0xE0;
87 }
88
89 /**
90 * Checks if this address is a loopback address
91 */
92 public boolean isLoopbackAddress ()
93 {
94 return addr [0] == 0x7F;
95 }
96
97 /**
98 * Checks if this address is a wildcard address
99 *
100 * @since 1.4
101 */
102 public boolean isAnyLocalAddress ()
103 {
104 byte[] anylocal = { 0, 0, 0, 0 };
105
106 return addr == anylocal;
107 }
108
109 /**
110 * Checks if this address is a link local address
111 *
112 * @since 1.4
113 */
114 public boolean isLinkLocalAddress ()
115 {
116 // XXX: This seems to not exist with IPv4 addresses
117 return false;
118 }
119
120 /**
121 * Checks if this address is a site local address
122 *
123 * @since 1.4
124 */
125 public boolean isSiteLocalAddress ()
126 {
127 // 10.0.0.0/8
128 if (addr [0] == 0x0A)
129 return true;
130
131 // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here:
132 // it says 172.16.0.0 - 172.255.255.255 are site local addresses
133 //
134 // 172.16.0.0/12
135 if (addr [0] == 0xAC && (addr [1] & 0xF0) == 0x01)
136 return true;
137
138 // 192.168.0.0/16
139 if (addr [0] == 0xC0 && addr [1] == 0xA8)
140 return true;
141
142 // XXX: Do we need to check more addresses here ?
143 return false;
144 }
145
146 /**
147 * Checks if this multicast address has global scope
148 *
149 * @since 1.4
150 */
151 public boolean isMCGlobal ()
152 {
153 // XXX: This seems to net exist with IPv4 addresses
154 return false;
155 }
156
157 /**
158 * Checks if this multicast address has node scope
159 *
160 * @since 1.4
161 */
162 public boolean isMCNodeLocal ()
163 {
164 // XXX: This seems to net exist with IPv4 addresses
165 return false;
166 }
167
168 /**
169 * Checks if this multicast address has link scope
170 *
171 * @since 1.4
172 */
173 public boolean isMCLinkLocal ()
174 {
175 if (!isMulticastAddress ())
176 return false;
177
178 return (addr [0] == 0xE0)
179 && (addr [1] == 0x00)
180 && (addr [2] == 0x00);
181 }
182
183 /**
184 * Checks if this multicast address has site scope
185 *
186 * @since 1.4
187 */
188 public boolean isMCSiteLocal ()
189 {
190 // XXX: This seems to net exist with IPv4 addresses
191 return false;
192 }
193
194 /**
195 * Checks if this multicast address has organization scope
196 *
197 * @since 1.4
198 */
199 public boolean isMCOrgLocal ()
200 {
201 // XXX: This seems to net exist with IPv4 addresses
202 return false;
203 }
204
205 /**
206 * Returns the address of the current instance
207 */
208 public byte[] getAddress ()
209 {
210 return addr;
211 }
212
213 /**
214 * Returns the address as string
215 *
216 * @since 1.0.2
217 */
218 public String getHostAddress ()
219 {
220 StringBuffer sbuf = new StringBuffer (40);
221 int len = addr.length;
222 int i = 0;
223
224 for ( ; ; )
225 {
226 sbuf.append (addr [i] & 0xFF);
227 i++;
228
229 if (i == len)
230 break;
231
232 sbuf.append ('.');
233 }
234
235 return sbuf.toString ();
236 }
237
238 /**
239 * Computes the hashcode of the instance
240 */
241 public int hashCode ()
242 {
243 int hash = 0;
244 int len = addr.length;
245 int i = len > 4 ? len - 4 : 0;
246
247 for ( ; i < len; i++)
248 hash = (hash << 8) | (addr [i] & 0xFF);
249
250 return hash;
251 }
252
253 /**
254 * Compare the current Inet4Address instance with obj
255 *
256 * @param obj Object to compare with
257 */
258 public boolean equals (Object obj)
259 {
260 if (obj == null || ! (obj instanceof InetAddress))
261 return false;
262
263 byte[] addr1 = addr;
264 byte[] addr2 = ((InetAddress) obj).addr;
265
266 if (addr1.length != addr2.length)
267 return false;
268
269 for (int i = addr1.length; --i >= 0; )
270 if (addr1 [i] != addr2 [i])
271 return false;
272
273 return true;
274 }
275} // class Inet4Address
Note: See TracBrowser for help on using the repository browser.