|
10 | 10 |
|
11 | 11 | public class ChinaIpMaskManager { |
12 | 12 |
|
13 | | - static SparseIntArray ChinaIpMaskDict = new SparseIntArray(3000); |
14 | | - static SparseIntArray MaskDict = new SparseIntArray(); |
15 | | - |
16 | | - public static boolean isIPInChina(int ip){ |
17 | | - boolean found=false; |
18 | | - for (int i = 0; i < MaskDict.size(); i++) { |
19 | | - int mask=MaskDict.keyAt(i); |
20 | | - int networkIP = ip & mask; |
21 | | - int mask2 = ChinaIpMaskDict.get(networkIP); |
22 | | - if(mask2==mask){ |
23 | | - found= true; |
24 | | - break; |
25 | | - } |
26 | | - } |
27 | | - return found; |
28 | | - } |
29 | | - |
30 | | - public static void loadFromFile(InputStream inputStream){ |
31 | | - int count=0; |
32 | | - try { |
33 | | - byte[] buffer=new byte[4096]; |
34 | | - while((count = inputStream.read(buffer))>0){ |
35 | | - for (int i = 0; i < count; i+=8) { |
36 | | - int ip = CommonMethods.readInt(buffer, i); |
37 | | - int mask = CommonMethods.readInt(buffer, i+4); |
38 | | - ChinaIpMaskDict.put(ip, mask); |
39 | | - MaskDict.put(mask, mask); |
40 | | - //System.out.printf("%s/%s\n", CommonMethods.IP2String(ip),CommonMethods.IP2String(mask)); |
41 | | - } |
42 | | - } |
43 | | - inputStream.close(); |
44 | | - System.out.printf("ChinaIpMask records count: %d\n", ChinaIpMaskDict.size()); |
45 | | - } catch (IOException e) { |
46 | | - // TODO Auto-generated catch block |
47 | | - e.printStackTrace(); |
48 | | - } |
49 | | - } |
| 13 | + static SparseIntArray ChinaIpMaskDict = new SparseIntArray(3000); |
| 14 | + static SparseIntArray MaskDict = new SparseIntArray(); |
| 15 | + |
| 16 | + public static boolean isIPInChina(int ip) { |
| 17 | + boolean found = false; |
| 18 | + for (int i = 0; i < MaskDict.size(); i++) { |
| 19 | + int mask = MaskDict.keyAt(i); |
| 20 | + int networkIP = ip & mask; |
| 21 | + int mask2 = ChinaIpMaskDict.get(networkIP); |
| 22 | + if (mask2 == mask) { |
| 23 | + found = true; |
| 24 | + break; |
| 25 | + } |
| 26 | + } |
| 27 | + return found; |
| 28 | + } |
| 29 | + |
| 30 | + public static void loadFromFile(InputStream inputStream) { |
| 31 | + int count = 0; |
| 32 | + try { |
| 33 | + byte[] buffer = new byte[4096]; |
| 34 | + while ((count = inputStream.read(buffer)) > 0) { |
| 35 | + for (int i = 0; i < count; i += 8) { |
| 36 | + int ip = CommonMethods.readInt(buffer, i); |
| 37 | + int mask = CommonMethods.readInt(buffer, i + 4); |
| 38 | + ChinaIpMaskDict.put(ip, mask); |
| 39 | + MaskDict.put(mask, mask); |
| 40 | + //System.out.printf("%s/%s\n", CommonMethods.IP2String(ip),CommonMethods.IP2String(mask)); |
| 41 | + } |
| 42 | + } |
| 43 | + inputStream.close(); |
| 44 | + System.out.printf("ChinaIpMask records count: %d\n", ChinaIpMaskDict.size()); |
| 45 | + } catch (IOException e) { |
| 46 | + // TODO Auto-generated catch block |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + } |
50 | 50 | } |
0 commit comments