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:
/**
* 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
}
}
}
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 ();
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;
}
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.