Source Code Cross Referenced for Inet4Address.java in  » 6.0-JDK-Modules » j2me » java » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Inet4Address.java	1.27 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         */
026:
027:        package java.net;
028:
029:        import java.security.AccessController;
030:        import java.io.ObjectStreamException;
031:        import sun.security.action.*;
032:
033:        /**
034:         * This class represents an Internet Protocol version 4 (IPv4) address.
035:         * Defined by <a href="http://www.ietf.org/rfc/rfc790.txt">
036:         * <i>RFC&nbsp;790: Assigned Numbers</i></a>,
037:         * <a href="http://www.ietf.org/rfc/rfc1918.txt">
038:         * <i>RFC&nbsp;1918: Address Allocation for Private Internets</i></a>,
039:         * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
040:         * Administratively Scoped IP Multicast</i></a>
041:         *
042:         * <h4> <A NAME="format">Textual representation of IP addresses<a> </h4>
043:         *
044:         * Textual representation of IPv4 address used as input to methods
045:         * takes one of the following forms:
046:         *
047:         * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
048:         * <tr><td><tt>d.d.d.d</tt><td></tr>
049:         * <tr><td><tt>d.d.d</tt><td></tr>
050:         * <tr><td><tt>d.d</td></tr>
051:         * <tr><td><tt>d</td></tr>
052:         * </table></blockquote>
053:         *
054:         * <p> When four parts are specified, each is interpreted as a byte of
055:         * data and assigned, from left to right, to the four bytes of an IPv4
056:         * address.
057:
058:         * <p> When a three part address is specified, the last part is
059:         * interpreted as a 16-bit quantity and placed in the right most two
060:         * bytes of the network address. This makes the three part address
061:         * format convenient for specifying Class B net- work addresses as
062:         * 128.net.host.
063:         *
064:         * <p> When a two part address is supplied, the last part is
065:         * interpreted as a 24-bit quantity and placed in the right most three
066:         * bytes of the network address. This makes the two part address
067:         * format convenient for specifying Class A network addresses as
068:         * net.host.
069:         *
070:         * <p> When only one part is given, the value is stored directly in
071:         * the network address without any byte rearrangement.
072:         *
073:         * <p> For methods that return a textual representation as output
074:         * value, the first form, i.e. a dotted-quad string, is used.
075:         *
076:         * <h4> The Scope of a Multicast Address </h4>
077:         * 
078:         * Historically the IPv4 TTL field in the IP header has doubled as a
079:         * multicast scope field: a TTL of 0 means node-local, 1 means
080:         * link-local, up through 32 means site-local, up through 64 means
081:         * region-local, up through 128 means continent-local, and up through
082:         * 255 are global. However, the administrative scoping is preferred.
083:         * Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt">
084:         * <i>RFC&nbsp;2365: Administratively Scoped IP Multicast</i></a>
085:         */
086:
087:        public final class Inet4Address extends InetAddress {
088:            final static int INADDRSZ = 4;
089:
090:            /** use serialVersionUID from InetAddress, but Inet4Address instance
091:             *  is always replaced by an InetAddress instance before being
092:             *  serialized */
093:            private static final long serialVersionUID = 3286316764910316507L;
094:
095:            /*
096:             * Perform initializations.
097:             */
098:            static {
099:                init();
100:            }
101:
102:            Inet4Address() {
103:                super ();
104:                hostName = null;
105:                address = 0;
106:                family = IPv4;
107:            }
108:
109:            Inet4Address(String hostName, byte addr[]) {
110:                this .hostName = hostName;
111:                this .family = IPv4;
112:                if (addr != null) {
113:                    if (addr.length == INADDRSZ) {
114:                        address = addr[3] & 0xFF;
115:                        address |= ((addr[2] << 8) & 0xFF00);
116:                        address |= ((addr[1] << 16) & 0xFF0000);
117:                        address |= ((addr[0] << 24) & 0xFF000000);
118:                    }
119:                }
120:            }
121:
122:            Inet4Address(String hostName, int address) {
123:                this .hostName = hostName;
124:                this .family = IPv4;
125:                this .address = address;
126:            }
127:
128:            /**
129:             * Replaces the object to be serialized with an InetAddress object.
130:             *
131:             * @return the alternate object to be serialized.
132:             *
133:             * @throws ObjectStreamException if a new object replacing this
134:             * object could not be created
135:             */
136:            private Object writeReplace() throws ObjectStreamException {
137:                // will replace the to be serialized 'this' object
138:                InetAddress inet = new InetAddress();
139:                inet.hostName = this .hostName;
140:                inet.address = this .address;
141:
142:                /**
143:                 * Prior to 1.4 an InetAddress was created with a family
144:                 * based on the platform AF_INET value (usually 2).
145:                 * For compatibility reasons we must therefore write the
146:                 * the InetAddress with this family.
147:                 */
148:                inet.family = 2;
149:
150:                return inet;
151:            }
152:
153:            /**
154:             * Utility routine to check if the InetAddress is an
155:             * IP multicast address. IP multicast address is a Class D
156:             * address i.e first four bits of the address are 1110.
157:             * @return a <code>boolean</code> indicating if the InetAddress is 
158:             * an IP multicast address
159:             * @since   JDK1.1
160:             */
161:            public boolean isMulticastAddress() {
162:                return ((address & 0xf0000000) == 0xe0000000);
163:            }
164:
165:            /**
166:             * Utility routine to check if the InetAddress in a wildcard address.
167:             * @return a <code>boolean</code> indicating if the Inetaddress is
168:             *         a wildcard address.
169:             * @since 1.4
170:             */
171:            public boolean isAnyLocalAddress() {
172:                return address == 0;
173:            }
174:
175:            /**
176:             * Utility routine to check if the InetAddress is a loopback address. 
177:             *
178:             * @return a <code>boolean</code> indicating if the InetAddress is 
179:             * a loopback address; or false otherwise.
180:             * @since 1.4
181:             */
182:            private static final int loopback = 2130706433; /* 127.0.0.1 */
183:
184:            public boolean isLoopbackAddress() {
185:                /* 127.x.x.x */
186:                byte[] byteAddr = getAddress();
187:                return byteAddr[0] == 127;
188:            }
189:
190:            /**
191:             * Utility routine to check if the InetAddress is an link local address. 
192:             *
193:             * @return a <code>boolean</code> indicating if the InetAddress is 
194:             * a link local address; or false if address is not a link local unicast address.
195:             * @since 1.4
196:             */
197:            public boolean isLinkLocalAddress() {
198:                // link-local unicast in IPv4 (169.254.0.0/16)
199:                // defined in "Documenting Special Use IPv4 Address Blocks
200:                // that have been Registered with IANA" by Bill Manning
201:                // draft-manning-dsua-06.txt
202:                return (((address >>> 24) & 0xFF) == 169)
203:                        && (((address >>> 16) & 0xFF) == 254);
204:            }
205:
206:            /**
207:             * Utility routine to check if the InetAddress is a site local address. 
208:             *
209:             * @return a <code>boolean</code> indicating if the InetAddress is 
210:             * a site local address; or false if address is not a site local unicast address.
211:             * @since 1.4
212:             */
213:            public boolean isSiteLocalAddress() {
214:                // refer to RFC 1918
215:                // 10/8 prefix
216:                // 172.16/12 prefix
217:                // 192.168/16 prefix
218:                return (((address >>> 24) & 0xFF) == 10)
219:                        || ((((address >>> 24) & 0xFF) == 172) && (((address >>> 16) & 0xF0) == 16))
220:                        || ((((address >>> 24) & 0xFF) == 192) && (((address >>> 16) & 0xFF) == 168));
221:            }
222:
223:            /**
224:             * Utility routine to check if the multicast address has global scope.
225:             *
226:             * @return a <code>boolean</code> indicating if the address has 
227:             *         is a multicast address of global scope, false if it is not 
228:             *         of global scope or it is not a multicast address
229:             * @since 1.4
230:             */
231:            public boolean isMCGlobal() {
232:                // 224.0.1.0 to 238.255.255.255
233:                byte[] byteAddr = getAddress();
234:                return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238)
235:                        && !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 && byteAddr[2] == 0);
236:            }
237:
238:            /**
239:             * Utility routine to check if the multicast address has node scope.
240:             *
241:             * @return a <code>boolean</code> indicating if the address has 
242:             *         is a multicast address of node-local scope, false if it is not 
243:             *         of node-local scope or it is not a multicast address
244:             * @since 1.4
245:             */
246:            public boolean isMCNodeLocal() {
247:                // unless ttl == 0
248:                return false;
249:            }
250:
251:            /**
252:             * Utility routine to check if the multicast address has link scope.
253:             *
254:             * @return a <code>boolean</code> indicating if the address has 
255:             *         is a multicast address of link-local scope, false if it is not 
256:             *         of link-local scope or it is not a multicast address
257:             * @since 1.4
258:             */
259:            public boolean isMCLinkLocal() {
260:                // 224.0.0/24 prefix and ttl == 1
261:                return (((address >>> 24) & 0xFF) == 224)
262:                        && (((address >>> 16) & 0xFF) == 0)
263:                        && (((address >>> 8) & 0xFF) == 0);
264:            }
265:
266:            /**
267:             * Utility routine to check if the multicast address has site scope.
268:             *
269:             * @return a <code>boolean</code> indicating if the address has 
270:             *         is a multicast address of site-local scope, false if it is not 
271:             *         of site-local scope or it is not a multicast address
272:             * @since 1.4
273:             */
274:            public boolean isMCSiteLocal() {
275:                // 239.255/16 prefix or ttl < 32
276:                return (((address >>> 24) & 0xFF) == 239)
277:                        && (((address >>> 16) & 0xFF) == 255);
278:            }
279:
280:            /**
281:             * Utility routine to check if the multicast address has organization scope.
282:             *
283:             * @return a <code>boolean</code> indicating if the address has 
284:             *         is a multicast address of organization-local scope, 
285:             *         false if it is not of organization-local scope 
286:             *         or it is not a multicast address
287:             * @since 1.4
288:             */
289:            public boolean isMCOrgLocal() {
290:                // 239.192 - 239.195
291:                return (((address >>> 24) & 0xFF) == 239)
292:                        && (((address >>> 16) & 0xFF) >= 192)
293:                        && (((address >>> 16) & 0xFF) <= 195);
294:            }
295:
296:            /**
297:             * Returns the raw IP address of this <code>InetAddress</code>
298:             * object. The result is in network byte order: the highest order
299:             * byte of the address is in <code>getAddress()[0]</code>.
300:             *
301:             * @return  the raw IP address of this object.
302:             */
303:            public byte[] getAddress() {
304:                byte[] addr = new byte[INADDRSZ];
305:
306:                addr[0] = (byte) ((address >>> 24) & 0xFF);
307:                addr[1] = (byte) ((address >>> 16) & 0xFF);
308:                addr[2] = (byte) ((address >>> 8) & 0xFF);
309:                addr[3] = (byte) (address & 0xFF);
310:                return addr;
311:            }
312:
313:            /**
314:             * Returns the IP address string in textual presentation form.
315:             *
316:             * @return  the raw IP address in a string format.
317:             * @since   JDK1.0.2
318:             */
319:            public String getHostAddress() {
320:                return numericToTextFormat(getAddress());
321:            }
322:
323:            /**
324:             * Returns a hashcode for this IP address.
325:             *
326:             * @return  a hash code value for this IP address.
327:             */
328:            public int hashCode() {
329:                return address;
330:            }
331:
332:            /**
333:             * Compares this object against the specified object.
334:             * The result is <code>true</code> if and only if the argument is
335:             * not <code>null</code> and it represents the same IP address as
336:             * this object.
337:             * <p>
338:             * Two instances of <code>InetAddress</code> represent the same IP
339:             * address if the length of the byte arrays returned by
340:             * <code>getAddress</code> is the same for both, and each of the
341:             * array components is the same for the byte arrays.
342:             *
343:             * @param   obj   the object to compare against.
344:             * @return  <code>true</code> if the objects are the same;
345:             *          <code>false</code> otherwise.
346:             * @see     java.net.InetAddress#getAddress()
347:             */
348:            public boolean equals(Object obj) {
349:                return (obj != null) && (obj instanceof  Inet4Address)
350:                        && (((InetAddress) obj).address == address);
351:            }
352:
353:            // Utilities
354:            /* 
355:             * Converts IPv4 binary address into a string suitable for presentation.
356:             *
357:             * @param src a byte array representing an IPv4 numeric address
358:             * @return a String representing the IPv4 address in 
359:             *         textual representation format
360:             * @since 1.4
361:             */
362:
363:            static String numericToTextFormat(byte[] src) {
364:                return (src[0] & 0xff) + "." + (src[1] & 0xff) + "."
365:                        + (src[2] & 0xff) + "." + (src[3] & 0xff);
366:            }
367:
368:            /*
369:             * Converts IPv4 address in its textual presentation form 
370:             * into its numeric binary form.
371:             * 
372:             * @param src a String representing an IPv4 address in standard format
373:             * @return a byte array representing the IPv4 numeric address
374:             * @since 1.4
375:             */
376:            static byte[] textToNumericFormat(String src) {
377:                if (src.length() == 0) {
378:                    return null;
379:                }
380:
381:                int octets;
382:                char ch;
383:                byte[] dst = new byte[INADDRSZ];
384:                char[] srcb = src.toCharArray();
385:                boolean saw_digit = false;
386:
387:                octets = 0;
388:                int i = 0;
389:                int cur = 0;
390:                while (i < srcb.length) {
391:                    ch = srcb[i++];
392:                    if (Character.isDigit(ch)) {
393:                        // note that Java byte is signed, so need to convert to int
394:                        int sum = (dst[cur] & 0xff) * 10
395:                                + (Character.digit(ch, 10) & 0xff);
396:
397:                        if (sum > 255)
398:                            return null;
399:
400:                        dst[cur] = (byte) (sum & 0xff);
401:                        if (!saw_digit) {
402:                            if (++octets > INADDRSZ)
403:                                return null;
404:                            saw_digit = true;
405:                        }
406:                    } else if (ch == '.' && saw_digit) {
407:                        if (octets == INADDRSZ)
408:                            return null;
409:                        cur++;
410:                        dst[cur] = 0;
411:                        saw_digit = false;
412:                    } else
413:                        return null;
414:                }
415:                if (octets < INADDRSZ)
416:                    return null;
417:                return dst;
418:            }
419:
420:            /**
421:             * Perform class load-time initializations.
422:             */
423:            private static native void init();
424:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.