According to the IP address for achieving java

  According to find IP addresses of java realized, can \ t separated ip address format (initial IP \ t end IP \ t national \ t region \ n) into a binary format ascending through the binary search in 50 — 150ms to find the corresponding IP address information. 
  IPLook generate raw data available. Code: IPTool.java 0 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  / * 
  * Create Date 2004-10-23 
*
  * / 
  Package starfire.proxy.ip; 

  Import java.io.BufferedReader; 
  Import java.io.DataOutputStream; 
  Import java.io.File; 
  Import java.io.FileInputStream; 
  Import java.io.FileOutputStream; 
  Import java.io.IOException; 
  Import java.io.InputStreamReader; 
  Import java.io.RandomAccessFile; 
  Import java.net.InetAddress; 
  Import java.net.UnknownHostException; 
  Import java.util.ArrayList; 
  Import java.util.Collections; 
  Import java.util.Comparator; 
  Import java.util.HashMap; 
  Import java.util.Iterator; 
  Import java.util.StringTokenizer; 

  / ** 
  * 4 | 4 | 4 | 4 | 4 # total number of records | ip offset | index offset | offset data 
  * (4 | 4 | 2 | 2) * # initial IP | end IP | National Index | Regional Index 
  * (2 | 4) * # 2 indexes, four offset (at the corresponding country | local start position) (countryNULL | localNULL) * 
*
  * @ Author  *
  * / 
  (Public class IPTool 
  Private String dataPath; 

  Private RandomAccessFile raf; 

  Private IPFile ipfile; 

  Private byte buffer [] = new byte [40]; 

  Private IPInfo minInfo = new IPInfo (); 

  Private IPInfo maxInfo = new IPInfo (); 

  Public static int str2Ip (String ip) (throws UnknownHostException 
  InetAddress address = InetAddress.getByName (ip); 
  Byte [] bytes = address.getAddress (); 
  Int a, b, c, d; 
  A = byte2int (bytes [0]); 
  B = byte2int (bytes [1]); 
  C = byte2int (bytes [2]); 
  D = byte2int (bytes [3]); 
  Int result = (a <<24) | (b <<16) | (c <<8) | d; 
  Return result; 
  ) 

  Public static int byte2int (byte b) ( 
  Int l = b & 0×07f; 
  If (b <0) ( 
  L | = 0×80; 
  ) 
  Return l; 
  ) 

  Public static void format (String dataPath, String targetPath) 
  Throws IOException ( 
  Long time = System.currentTimeMillis (); 
  BufferedReader in = new BufferedReader (new InputStreamReader ( 
  New FileInputStream (dataPath))); 
  FileOutputStream fos = new FileOutputStream (targetPath); 
  DataOutputStream out = new DataOutputStream (fos); 
  String line; 
  ArrayList ipList = new ArrayList (150,000); 
  HashMap map = new HashMap (65,535); 
  ArrayList dataList = new ArrayList (65,535); 
  Iterator iterator = null; 
  While ((line = in.readLine ())! = Null) ( 
  StringTokenizer token = new StringTokenizer (line, "\ t"); 
  String start; 
  String end; 
  String country; 
  String local; 
  If (token.countTokens () == 4) ( 
  Start = token.nextToken (); 
  Token.nextToken end = (); 
  Country = token.nextToken (). Trim (); 
  Token.nextToken local = (); 
  If (local.startsWith ("/")) ( 
  Local.substring local = (1); 
  ) 
  Local.trim local = (); 
  Try ( 
  IPOriginInfo info = new IPOriginInfo (); 
  Info.startIP = str2Ip (start); 
  Info.endIP = str2Ip (end); 
  Info.country = country; 
  Info.local = local; 
  IpList.add (info); 
  Object obj = map.put (country, new Integer (map.size ())); 
  If (obj! = Null) ( 
  Map.put (country, obj); 
  ) 
  Else ( 
  DataList.add (country); 
  ) 
  Obj = map.put (local, new Integer (map.size ())); 
  If (obj! = Null) ( 
  Map.put (local, obj); 
  ) 
  Else ( 
  DataList.add (local); 
  ) 
  ) Catch (UnknownHostException e) ( 
  Continue; 
  ) 
  ) 
  ) 

  IPFile ipfile = new IPFile (); 
  Ipfile.totalRecords = ipList.size (); 
  Ipfile.ipOffset = IPFile.HEADER_SIZE; 
  Ipfile.indexOffset + = ipfile.ipOffset ipList.size () 
  * IPFile.IP_INFO_SIZE; 
  Ipfile.dataOffset + = ipfile.indexOffset map.size () * IPFile.INDEX_SIZE; 

  System.out.println ( "totalrecords:" + ipfile.totalRecords); 
  System.out.println ( "ipoffset:" + ipfile.ipOffset); 
  System.out.println ( "indexoffset:" + ipfile.indexOffset); 
  System.out.println ( "dataoffset:" + ipfile.dataOffset); 
  System.out.println ( "data size:" + map.size ()); 
  / / System.out.println ( "total size:" + total); 

  Out.writeInt (ipfile.magic); 
  Out.writeInt (ipfile.totalRecords); 
  Out.writeInt (ipfile.ipOffset); 
  Out.writeInt (ipfile.indexOffset); 
  Out.writeInt (ipfile.dataOffset); 
  Out.flush (); 

  / / Sort 
  Collections.sort (ipList, new Comparator () ( 

  Public int compare (Object o1, Object o2) ( 
  IPOriginInfo info1 = (IPOriginInfo) o1; 
  IPOriginInfo info2 = (IPOriginInfo) o2; 
  Int2long long start = (info1.startIP); 
  = Int2long long end (info2.startIP); 
  Return (int) (start - end); 
  ) 

  )); 
  / / Write ip information 
  = IpList.iterator iterator (); 
  While (iterator.hasNext ()) ( 
  IPOriginInfo info = (IPOriginInfo) iterator.next (); 
  Out.writeInt (info.startIP); 
  Out.writeInt (info.endIP); 
  Short s = ((Integer) map.get (info.country)). ShortValue (); 
  Out.writeShort (s); 
  S = ((Integer) map.get (info.local)). ShortValue (); 
  Out.writeShort (s); 
  ) 
  Out.flush (); 

  / / Write indexing information 
  Ipfile.dataOffset = int position; 
  Int num = 0; 
  = DataList.iterator iterator (); 
  While (iterator.hasNext ()) ( 
  String value = (String) iterator.next (); 
  Out.writeShort (num + +); 
  Out.writeInt (position); 
  If (position> 2729877) ( 
  System.err.println ( "error:" + position); 
  ) 
  Value.getBytes position + = (). Length + 1; 
  ) 
  Out.flush (); 

  / / Write country | local data 
  / / = Sort.iterator iterator (); 
  Int size = dataList.size (); 
  For (int i = 0; i <size; i + +) ( 
  String value = (String) dataList.get (i); 
  Out.write (value.getBytes ()); 
  Out.write ((byte) 0); 
  ) 
  Out.flush (); 

  In.close (); 
  Out.close (); 
  System.out.println ( "used" + (System.currentTimeMillis () - time) / 1000 
  + "Seconds"); 
  ) 

  Public IPTool (String dataPath) throws IOException ( 
  This.dataPath = dataPath; 
  This.ipfile = new IPFile (); 
  Load (dataPath); 
  ) 

  Void load (String dataPath) throws IOException ( 
  File f = new File (dataPath); 
  Raf = new RandomAccessFile (f, "r"); 
  Int magic = raf.readInt (); 
  If (magic! = IPFile.MAGIC) ( 
  Throw new IOException ( "bad format, the magic number not match"); 
  ) 
  Ipfile.totalRecords = raf.readInt (); 
  Ipfile.ipOffset = raf.readInt (); 
  Ipfile.indexOffset = raf.readInt (); 
  Ipfile.dataOffset = raf.readInt (); 
  Ipfile.dataCount = (ipfile.dataOffset - ipfile.indexOffset) 
  / IPFile.INDEX_SIZE; 
  If (raf.length () <ipfile.dataOffset) ( 
  Throw new IOException ( "bad format, length not match"); 
  ) 
  Ipfile.ipCount = (ipfile.indexOffset - ipfile.ipOffset) 
  / IPFile.IP_INFO_SIZE; 
  Ipfile.minIP = int2long (raf.readInt ()); 
  Raf.seek (ipfile.indexOffset - IPFile.IP_INFO_SIZE); 
  Ipfile.maxIP = int2long (raf.readInt ()); 
  / / System.out.println (ipfile.ipCount); 
  / / FillInfo (0, minInfo); 
  / / FillInfo (ipfile.ipCount-1, maxInfo); 
  ) 

  Private void fillInfo (int pos, IPInfo info) throws IOException ( 
  Raf.seek (* IPFile.IP_INFO_SIZE ipfile.ipOffset pos + + 8); 
  Int ci = short2int (raf.readShort ()); 
  Int li = short2int (raf.readShort ()); 
  Raf.seek (ci * IPFile.INDEX_SIZE ipfile.indexOffset + + 2); 
  Ci = raf.readInt (); 
  Raf.seek (* IPFile.INDEX_SIZE ipfile.indexOffset li + + 2); 
  Li = raf.readInt (); 
  Int2long long cl = (ci); 
  Int2long long ll = (li); 
  / / System.out.println ( "ci =" + + cl "li =" + ll); 
  Raf.seek (cl); 
  Int ret = raf.read (buffer); 
  Int i = 0; 
  For (; i <ret; i + +) ( 
  If (buffer [i] == 0) ( 
  Break; 
  ) 
  ) 
  Info.setCountry (new String (buffer, 0, i)); 
  Raf.seek (ll); 
  Ret = raf.read (buffer); 
  For (i = 0; i <ret; i + +) ( 
  If (buffer [i] == 0) ( 
  Break; 
  ) 
  ) 
  Info.setLocal (new String (buffer, 0, i)); 
  / / System.out.println (ll + "length =" + raf.length ()); 
  ) 

  Public synchronized IPInfo find (String ip) ( 
  IPInfo info = new IPInfo (); 
  Info.setIp (ip); 
  Try ( 
  / / Long time = System.currentTimeMillis (); 
  Long ipNum = ip2long (ip); 
  Int pos = 1; 
  If (ipNum == ipfile.minIP) ( 
  Pos = 0; 
  ) Else if (ipNum == ipfile.maxIP) ( 
  Pos = ipfile.ipCount - 1; 
  Else () 

  Find pos = (0, ipfile.ipCount - 1, ipNum); 
  ) 
  If (pos! = -1) ( 
  FillInfo (pos, info); 
  ) 
  / / System.out.println ( "time =" + (System.currentTimeMillis ()-time)); 
  ) Catch (UnknownHostException e) ( 

  ) Catch (IOException e) ( 
  E.printStackTrace (); 
  ) 
  Return info; 
  ) 

  Public int find (int start, int end, long target) throws IOException ( 
  If (start> = end) ( 
  Return -1; 
  ) 
  Int middle = (+ start end) / 2; 
  Raf.seek (* IPFile.IP_INFO_SIZE middle ipfile.ipOffset +); 
  Long i = int2long (raf.readInt ()); 
  Int2long long j = (raf.readInt ()); 
  If (target> = i & & target <= j) ( 
  Return middle; 
  ) 
  If (target <i) ( 
  End = middle; 
  Else () 
  Start = middle; 
  ) 
  Return find (start, end, the target); 
  ) 

  Public static long ip2long (String ip) (throws UnknownHostException 
  Int ipNum = str2Ip (ip); 
  Return int2long (ipNum); 
  ) 

  Public static long int2long (int i) ( 
  Long l = i & 0×7fffffffL; 
  If (i <0) ( 
  L | = 0×080000000L; 
  ) 
  Return l; 
  ) 

  Public static int short2int (short s) ( 
  Int i = s & 0×7fff; 
  If (s <0) ( 
  S | = 0×08000; 
  ) 
  Return s; 
  ) 

  Protected void finalize () ( 
  If (raf! = Null) ( 
  Try ( 
  Raf.close (); 
  ) Catch (IOException e) ( 
  ) 
  ) 
  ) 

  Public static void main (String [] args) throws Exception ( 
  / / Int ip = str2Ip ( "192.168.0.1"); 
  / / System.out.println (ip2long (ip)); 
  / / Format ( "ipdata.txt", "result.dat"); 
  IPTool tool = new IPTool ( "result.dat"); 
  IPInfo info = tool.find ( "211.155.224.222"); 
  System.out.println (info.getCountry () + "" + info.getLocal ()); 
  ) 
  ) 

  (Class IPOriginInfo 
  Public int startIP; 
  Public int endIP; 
  Public String country; 
  Public String local; 
  ) 

  (Class IPFile 
  Public static final int MAGIC = 0×05080321; 
  Public static final int HEADER_SIZE = 20; 
  Public static final int IP_INFO_SIZE = 12; 
  Public static final int INDEX_SIZE = 6; 

  Public final int magic = MAGIC; 
  Public int totalRecords; 
  Public int ipOffset = HEADER_SIZE; 
  Public int indexOffset; 
  Public int dataOffset; 

  Int dataCount; 
  Int ipCount; 
  Long minIP; 
  Long maxIP; 
  ) 


  IPInfo.java 0 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  / * 
  * Create Date 2004-10-23 
*
  * / 
  Package starfire.proxy.ip; 

  / ** 
  * @ Author  *
  * / 
  (Public class IPInfo 

  Private String ip; 
  Private String country = ""; 
  Private String local = ""; 



  / ** 
  * @ Return to return to country. 
  * / 
  Public String getCountry () ( 
  Return country; 
  ) 
  / ** 
  * @ Param country to set up the country. 
  * / 
  Public void setCountry (String country) ( 
  This.country = country; 
  ) 
  / ** 
  * @ Return ip return. 
  * / 
  Public String getIp () ( 
  Return ip; 
  ) 
  / ** 
  * @ Param ip to set ip. 
  * / 
  Public void setIp (String ip) ( 
  This.ip = ip; 
  ) 
  / ** 
  * @ Return to return to local. 
  * / 
  Public String getLocal () ( 
  Return local; 
  ) 
  / ** 
  * @ Param local settings to the local. 
  * / 
  Public void setLocal (String local) ( 
  This.local = local; 
  ) 
  ) 

  IPTool use the format (String dataPath, String targetPath) can be transformed into the original data file format for internal use. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “According to the IP address for achieving java”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.