?? ipseeker.java
字號:
return info;
}
/**
* 從offset位置讀取4個字節為一個long,因為java為big-endian格式,所以沒辦法 用了這么一個函數來做轉換
*
* @param offset
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong4(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ret |= (ipFile.readByte() & 0xFF);
ret |= ((ipFile.readByte() << 8) & 0xFF00);
ret |= ((ipFile.readByte() << 16) & 0xFF0000);
ret |= ((ipFile.readByte() << 24) & 0xFF000000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從offset位置讀取3個字節為一個long,因為java為big-endian格式,所以沒辦法 用了這么一個函數來做轉換
*
* @param offset
* @return 讀取的long值,返回-1表示讀取文件失敗
*/
private long readLong3(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從當前位置讀取3個字節轉換成long
*
* @return
*/
private long readLong3() {
long ret = 0;
try {
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 從offset位置讀取四個字節的ip地址放入ip數組中,讀取后的ip為big-endian格式,但是
* 文件中是little-endian形式,將會進行轉換
*
* @param offset
* @param ip
*/
private void readIP(long offset, byte[] ip) {
try {
ipFile.seek(offset);
ipFile.readFully(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 從offset位置讀取四個字節的ip地址放入ip數組中,讀取后的ip為big-endian格式,但是
* 文件中是little-endian形式,將會進行轉換
*
* @param offset
* @param ip
*/
private void readIP(int offset, byte[] ip) {
mbb.position(offset);
mbb.get(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
}
/**
* 把類成員ip和beginIp比較,注意這個beginIp是big-endian的
*
* @param ip
* 要查詢的IP
* @param beginIp
* 和被查詢IP相比較的IP
* @return 相等返回0,ip大于beginIp則返回1,小于返回-1。
*/
private int compareIP(byte[] ip, byte[] beginIp) {
for (int i = 0; i < 4; i++) {
int r = compareByte(ip[i], beginIp[i]);
if (r != 0)
return r;
}
return 0;
}
/**
* 把兩個byte當作無符號數進行比較
*
* @param b1
* @param b2
* @return 若b1大于b2則返回1,相等返回0,小于返回-1
*/
private int compareByte(byte b1, byte b2) {
if ((b1 & 0xFF) > (b2 & 0xFF)) // 比較是否大于
return 1;
else if ((b1 ^ b2) == 0)// 判斷是否相等
return 0;
else
return -1;
}
/**
* 這個方法將根據ip的內容,定位到包含這個ip國家地區的記錄處,返回一個絕對偏移 方法使用二分法查找。
*
* @param ip
* 要查詢的IP
* @return 如果找到了,返回結束IP的偏移,如果沒有找到,返回-1
*/
private long locateIP(byte[] ip) {
long m = 0;
int r;
// 比較第一個ip項
readIP(ipBegin, b4);
r = compareIP(ip, b4);
if (r == 0)
return ipBegin;
else if (r < 0)
return -1;
// 開始二分搜索
for (long i = ipBegin, j = ipEnd; i < j;) {
m = getMiddleOffset(i, j);
readIP(m, b4);
r = compareIP(ip, b4);
// log.debug(Utils.getIpStringFromBytes(b));
if (r > 0)
i = m;
else if (r < 0) {
if (m == j) {
j -= IP_RECORD_LENGTH;
m = j;
} else
j = m;
} else
return readLong3(m + 4);
}
// 如果循環結束了,那么i和j必定是相等的,這個記錄為最可能的記錄,但是并非
// 肯定就是,還要檢查一下,如果是,就返回結束地址區的絕對偏移
m = readLong3(m + 4);
readIP(m, b4);
r = compareIP(ip, b4);
if (r <= 0)
return m;
else
return -1;
}
/**
* 得到begin偏移和end偏移中間位置記錄的偏移
*
* @param begin
* @param end
* @return
*/
private long getMiddleOffset(long begin, long end) {
long records = (end - begin) / IP_RECORD_LENGTH;
records >>= 1;
if (records == 0)
records = 1;
return begin + records * IP_RECORD_LENGTH;
}
/**
* 給定一個ip國家地區記錄的偏移,返回一個IPLocation結構
*
* @param offset
* @return
*/
private IPLocation getIPLocation(long offset) {
try {
// 跳過4字節ip
ipFile.seek(offset + 4);
// 讀取第一個字節判斷是否標志字節
byte b = ipFile.readByte();
if (b == AREA_FOLLOWED) {
// 讀取國家偏移
long countryOffset = readLong3();
// 跳轉至偏移處
ipFile.seek(countryOffset);
// 再檢查一次標志字節,因為這個時候這個地方仍然可能是個重定向
b = ipFile.readByte();
if (b == NO_AREA) {
loc.country = readString(readLong3());
ipFile.seek(countryOffset + 4);
} else
loc.country = readString(countryOffset);
// 讀取地區標志
loc.area = readArea(ipFile.getFilePointer());
} else if (b == NO_AREA) {
loc.country = readString(readLong3());
loc.area = readArea(offset + 8);
} else {
loc.country = readString(ipFile.getFilePointer() - 1);
loc.area = readArea(ipFile.getFilePointer());
}
return loc;
} catch (IOException e) {
return null;
}
}
/**
* @param offset
* @return
*/
private IPLocation getIPLocation(int offset) {
// 跳過4字節ip
mbb.position(offset + 4);
// 讀取第一個字節判斷是否標志字節
byte b = mbb.get();
if (b == AREA_FOLLOWED) {
// 讀取國家偏移
int countryOffset = readInt3();
// 跳轉至偏移處
mbb.position(countryOffset);
// 再檢查一次標志字節,因為這個時候這個地方仍然可能是個重定向
b = mbb.get();
if (b == NO_AREA) {
loc.country = readString(readInt3());
mbb.position(countryOffset + 4);
} else
loc.country = readString(countryOffset);
// 讀取地區標志
loc.area = readArea(mbb.position());
} else if (b == NO_AREA) {
loc.country = readString(readInt3());
loc.area = readArea(offset + 8);
} else {
loc.country = readString(mbb.position() - 1);
loc.area = readArea(mbb.position());
}
return loc;
}
/**
* 從offset偏移開始解析后面的字節,讀出一個地區名
*
* @param offset
* @return 地區名字符串
* @throws IOException
*/
private String readArea(long offset) throws IOException {
ipFile.seek(offset);
byte b = ipFile.readByte();
if (b == 0x01 || b == 0x02) {
long areaOffset = readLong3(offset + 1);
if (areaOffset == 0)
return "未知地區";
else
return readString(areaOffset);
} else
return readString(offset);
}
/**
* @param offset
* @return
*/
private String readArea(int offset) {
mbb.position(offset);
byte b = mbb.get();
if (b == 0x01 || b == 0x02) {
int areaOffset = readInt3();
if (areaOffset == 0)
return "未知地區";
else
return readString(areaOffset);
} else
return readString(offset);
}
/**
* 從offset偏移處讀取一個以0結束的字符串
*
* @param offset
* @return 讀取的字符串,出錯返回空字符串
*/
private String readString(long offset) {
try {
ipFile.seek(offset);
int i;
for (i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile
.readByte())
;
if (i != 0)
return Utils.getString(buf, 0, i, "GBK");
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "";
}
/**
* 從內存映射文件的offset位置得到一個0結尾字符串
*
* @param offset
* @return
*/
private String readString(int offset) {
try {
mbb.position(offset);
int i;
for (i = 0, buf[i] = mbb.get(); buf[i] != 0; buf[++i] = mbb.get())
;
if (i != 0)
return Utils.getString(buf, 0, i, "GBK");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return "";
}
public String getAddress(String ip) {
String sCnty = getCountry(ip);
String sArea = getArea(ip);
String country = sCnty.equals(" CZ88.NET") ? "" : sCnty;
String area = sArea.equals(" CZ88.NET") ? "" : sArea;
String address = country + " " + area;
return address.trim();
}
public String getAddress_faster(byte[] ip) {
// 檢查ip地址文件是否正常
if (ipFile == null)
return "錯誤的IP數據庫文件";
// 保存ip,轉換ip字節數組為字符串形式
String ipStr = Utils.getIpStringFromBytes(ip);
// 先檢查cache中是否已經包含有這個ip的結果,沒有再搜索文件
if (ipCache.containsKey(ipStr)) {
IPLocation loc = (IPLocation) ipCache.get(ipStr);
return loc.country + " " + loc.area;
} else {
IPLocation loc = getIPLocation(ip);
ipCache.put(ipStr, loc.getCopy());
return loc.country + " " + loc.area;
}
}
public String getAddress_faster(String ip) {
String sAddr = getAddress_faster(Utils.getIpByteArrayFromString(ip));
return sAddr.replaceAll(" CZ88.NET", "").trim();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -