KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > util > IPAddress


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.util;
18
19 import java.net.InetAddress JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * TCP/IP Address Utility Class
24  */

25 public class IPAddress
26 {
27
28     /**
29      * Check if the specified address is a valid numeric TCP/IP address
30      *
31      * @param ipaddr String
32      * @return boolean
33      */

34     public final static boolean isNumericAddress(String JavaDoc ipaddr)
35     {
36
37         // Check if the string is valid
38

39         if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
40             return false;
41
42         // Check the address string, should be n.n.n.n format
43

44         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(ipaddr, ".");
45         if (token.countTokens() != 4)
46             return false;
47
48         while (token.hasMoreTokens())
49         {
50
51             // Get the current token and convert to an integer value
52

53             String JavaDoc ipNum = token.nextToken();
54
55             try
56             {
57                 int ipVal = Integer.valueOf(ipNum).intValue();
58                 if (ipVal < 0 || ipVal > 255)
59                     return false;
60             }
61             catch (NumberFormatException JavaDoc ex)
62             {
63                 return false;
64             }
65         }
66
67         // Looks like a valid IP address
68

69         return true;
70     }
71
72     /**
73      * Check if the specified address is a valid numeric TCP/IP address and return as an integer
74      * value
75      *
76      * @param ipaddr String
77      * @return int
78      */

79     public final static int parseNumericAddress(String JavaDoc ipaddr)
80     {
81
82         // Check if the string is valid
83

84         if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
85             return 0;
86
87         // Check the address string, should be n.n.n.n format
88

89         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(ipaddr, ".");
90         if (token.countTokens() != 4)
91             return 0;
92
93         int ipInt = 0;
94
95         while (token.hasMoreTokens())
96         {
97
98             // Get the current token and convert to an integer value
99

100             String JavaDoc ipNum = token.nextToken();
101
102             try
103             {
104
105                 // Validate the current address part
106

107                 int ipVal = Integer.valueOf(ipNum).intValue();
108                 if (ipVal < 0 || ipVal > 255)
109                     return 0;
110
111                 // Add to the integer address
112

113                 ipInt = (ipInt << 8) + ipVal;
114             }
115             catch (NumberFormatException JavaDoc ex)
116             {
117                 return 0;
118             }
119         }
120
121         // Return the integer address
122

123         return ipInt;
124     }
125
126     /**
127      * Convert an IP address into an integer value
128      *
129      * @param ipaddr InetAddress
130      * @return int
131      */

132     public final static int asInteger(InetAddress JavaDoc ipaddr)
133     {
134
135         // Get the address as an array of bytes
136

137         byte[] addrBytes = ipaddr.getAddress();
138
139         // Build an integer value from the bytes
140

141         return DataPacker.getInt(addrBytes, 0);
142     }
143
144     /**
145      * Check if the specified address is within the required subnet
146      *
147      * @param ipaddr String
148      * @param subnet String
149      * @param mask String
150      * @return boolean
151      */

152     public final static boolean isInSubnet(String JavaDoc ipaddr, String JavaDoc subnet, String JavaDoc mask)
153     {
154
155         // Convert the addresses to integer values
156

157         int ipaddrInt = parseNumericAddress(ipaddr);
158         if (ipaddrInt == 0)
159             return false;
160
161         int subnetInt = parseNumericAddress(subnet);
162         if (subnetInt == 0)
163             return false;
164
165         int maskInt = parseNumericAddress(mask);
166         if (maskInt == 0)
167             return false;
168
169         // Check if the address is part of the subnet
170

171         if ((ipaddrInt & maskInt) == subnetInt)
172             return true;
173         return false;
174     }
175
176     /**
177      * Convert a raw IP address array as a String
178      *
179      * @param ipaddr byte[]
180      * @return String
181      */

182     public final static String JavaDoc asString(byte[] ipaddr)
183     {
184
185         // Check if the address is valid
186

187         if (ipaddr == null || ipaddr.length != 4)
188             return null;
189
190         // Convert the raw IP address to a string
191

192         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
193
194         str.append((int) (ipaddr[0] & 0xFF));
195         str.append(".");
196         str.append((int) (ipaddr[1] & 0xFF));
197         str.append(".");
198         str.append((int) (ipaddr[2] & 0xFF));
199         str.append(".");
200         str.append((int) (ipaddr[3] & 0xFF));
201
202         // Return the address string
203

204         return str.toString();
205     }
206 }
207
Popular Tags