Java implementation based on IP for address

Source: Internet
Author: User
Tags add end final flush net string sort trim
Based on the IP address of the Java implementation, can be \ t-delimited IP, address format (beginning ip\t end of the ip\t country \ \ \ n) into an ascending binary format, through a binary lookup can be found in the 50-150MS IP address information.
Raw data can be generated iplook. Code:

Iptool.java
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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
60W
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
/*
* Date Created 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 record number |ip Offset | index Offset | data offset
* (4|4|2|2) * #起始IP | end ip| Country Index | Zone index
* (2|4) * #2位索引, 4-bit offset (pointing to the corresponding country|local start position) (countrynull|localnull) *
*
* @author Starfire
*
*/
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 & 0x07f;
if (b < 0) {
L |= 0x80;
}
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 (150000);
HashMap map = new HashMap (65535);
ArrayList dataList = new ArrayList (65535);
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 ();
End = Token.nexttoken ();
Country = Token.nexttoken (). Trim ();
Local = Token.nexttoken ();
if (Local.startswith ("/")) {
Local = local.substring (1);
}
Local = Local.trim ();
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;
Long start = Int2long (Info1.startip);
Long end = Int2long (Info2.startip);
return (int) (start-end);
}

});
Write IP information
iterator = Iplist.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 index information
int position = Ipfile.dataoffset;
int num = 0;
iterator = Datalist.iterator ();
while (Iterator.hasnext ()) {
String value = (string) iterator.next ();
Out.writeshort (num++);
Out.writeint (position);
if (Position > 2729877) {
System.err.println ("error:" + position);
}
Position + + value.getbytes (). length + 1;
}
Out.flush ();

Write country|local data
iterator = Sort.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,magic number is 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.ipoffset + pos * ipfile.ip_info_size + 8);
int ci = short2int (Raf.readshort ());
int li = Short2int (Raf.readshort ());
Raf.seek (Ipfile.indexoffset + ci * ipfile.index_size + 2);
CI = Raf.readint ();
Raf.seek (Ipfile.indexoffset + li * ipfile.index_size + 2);
Li = Raf.readint ();
Long cl = Int2long (CI);
Long ll = Int2long (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 {

pos = Find (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 = (end + start)/2;
Raf.seek (Ipfile.ipoffset + middle * ipfile.ip_info_size);
Long i = Int2long (Raf.readint ());
Long J = Int2long (Raf.readint ());
if (target >= i && target <= j) {
return middle;
}
if (target < i) {
end = Middle;
} else {
start = middle;
}
Return find (Start, end, 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 & 0x7fffffffl;
if (I < 0) {
L |= 0x080000000l;
}
return l;
}

public static int Short2int (short s) {
int i = S & 0x7fff;
if (s < 0) {
s |= 0x08000;
}
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 = 0x05080321;
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
21st
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
/*
* Date Created 2004-10-23
*
*/
Package Starfire.proxy.ip;

/**
* @author Starfire
*
*/
public class Ipinfo {

Private String IP;
Private String country = "";
Private String local = "";



/**
* @return return to country.
*/
Public String Getcountry () {
return country;
}
/**
* @param country to set the country.
*/
public void Setcountry (String country) {
This.country = country;
}
/**
* @return return IP.
*/
Public String GetIP () {
return IP;
}
/**
* IP to be set @param IP.
*/
public void setIp (String IP) {
This.ip = IP;
}
/**
* @return return to Local.
*/
Public String getlocal () {
return to local;
}
/**
* @param local to set local.
*/
public void setLocal (String local) {
this.local = local;
}
}


Use the Iptool format (string datapath, String TargetPath) to convert the original data file into an internal use format.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.