KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > InetNetwork


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 import java.net.InetAddress JavaDoc;
32
33 /**
34  * Represents an internet network mask.
35  */

36 public class InetNetwork {
37   private long _address;
38   private long _mask;
39   private int _maskIndex;
40
41   /**
42    * Create a internet mask.
43    *
44    * @param inetAddress the main address
45    * @param maskIndex the number of bits to match.
46    */

47   public InetNetwork(InetAddress JavaDoc inetAddress, int maskIndex)
48   {
49     byte []bytes = inetAddress.getAddress();
50
51     long address = 0;
52     for (int i = 0; i < bytes.length; i++)
53       address = 256 * address + (bytes[i] & 0xff);
54
55     _address = address;
56     _maskIndex = maskIndex;
57     _mask = -1L << (32 - maskIndex);
58   }
59   
60   /**
61    * Creates an inet network with a mask.
62    */

63   public InetNetwork(long address, int maskIndex)
64   {
65     _address = address;
66     _maskIndex = maskIndex;
67     _mask = -1L << (32 - maskIndex);
68   }
69
70   public static InetNetwork create(String JavaDoc network)
71   {
72     if (network == null)
73       return null;
74     
75     int i = 0;
76     int len = network.length();
77     
78     long address = 0;
79     int digits = 0;
80
81     int ch = 0;
82     while (i < len) {
83       if (network.charAt(i) == '/')
84         break;
85
86       int digit = 0;
87       for (; i < len && (ch = network.charAt(i)) >= '0' && ch <= '9'; i++)
88         digit = 10 * digit + ch - '0';
89
90       address = 256 * address + digit;
91
92       digits++;
93
94       if (i < len && ch == '.')
95         i++;
96     }
97
98     while (digits++ < 4) {
99       address *= 256;
100     }
101       
102
103     int mask;
104     if (i < len && network.charAt(i) == '/') {
105       mask = 0;
106       for (i++; i < len && (ch = network.charAt(i)) >= '0' && ch <= '9'; i++)
107         mask = 10 * mask + ch - '0';
108     }
109     else
110       mask = 32;
111
112     return new InetNetwork(address, mask);
113   }
114
115   /**
116    * Returns true if the address is in this network.
117    */

118   public boolean isMatch(InetAddress JavaDoc inetAddress)
119   {
120     byte []bytes = inetAddress.getAddress();
121
122     long address = 0;
123     for (int i = 0; i < bytes.length; i++)
124       address = 256 * address + (bytes[i] & 0xff);
125
126     return (_address & _mask) == (address & _mask);
127   }
128
129   /**
130    * Returns true if the address is in this network.
131    */

132   public boolean isMatch(byte []bytes)
133   {
134     long address = 0;
135     for (int i = 0; i < bytes.length; i++)
136       address = 256 * address + (bytes[i] & 0xff);
137
138     return (_address & _mask) == (address & _mask);
139   }
140
141   /**
142    * Returns true if the address is in this network.
143    */

144   public boolean isMatch(long address)
145   {
146     return (_address & _mask) == (address & _mask);
147   }
148
149   /**
150    * Return a readable string.
151    */

152   public String JavaDoc toString()
153   {
154     CharBuffer cb = CharBuffer.allocate();
155
156     for (int i = 0; i < 4; i++) {
157       if (i != 0)
158         cb.append('.');
159
160       cb.append((_address >> (3 - i) * 8) & 0xff);
161     }
162
163     cb.append('/');
164     cb.append(_maskIndex);
165
166     return cb.close();
167   }
168 }
169
Popular Tags