<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Terry.Li-彬

    虛其心,可解天下之問;專其心,可治天下之學(xué);靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      143 隨筆 :: 344 文章 :: 130 評(píng)論 :: 0 Trackbacks
    java讀取純真IP數(shù)據(jù)庫QQwry.dat的源代碼,要運(yùn)行此程序必須有
    到網(wǎng)上下載QQwry.da,由于太大,我這里就不提供了。
    一、IPEntry.java
    /** *
    * 一條IP范圍記錄,不僅包括國家和區(qū)域,也包括起始IP和結(jié)束IP *?

    *?
    * @author 馬若劼
    */
    public class IPEntry {
    ????public String beginIp;
    ????public String endIp;
    ????public String country;
    ????public String area;
    ????
    ????/**
    ???? * 構(gòu)造函數(shù)
    ???? */
    ???


    public IPEntry() {
    ????????beginIp = endIp = country = area = "";
    ????}
    ????
    ????public String toString(){
    ?? ????retur????????this.area+"??"+this.country+"IP范圍:"+this.beginIp+"-"+this.endIp;
    ????}
    ?? }?


    二、Utils.java


    /*
    * Created on 2004-8-4
    *
    */


    import java.io.UnsupportedEncodingException;

    /**
    * @author LJ-silver
    */
    public class Utils {
    ????/**
    ???? * 從ip的字符串形式得到字節(jié)數(shù)組形式
    ???? * @param ip 字符串形式的ip
    ???? * @return 字節(jié)數(shù)組形式的ip
    ???? */
    ????public static byte[] getIpByteArrayFromString(String ip) {
    ????????byte[] ret = new byte[4];
    ????????java.util.StringTokenizer st = new java.util.StringTokenizer(ip, ".");
    ????????try {
    ????????????ret[0] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
    ????????????ret[1] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
    ????????????ret[2] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
    ????????????ret[3] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
    ????????} catch (Exception e) {
    ????????????System.out.println(e.getMessage());
    ????????}
    ????????return ret;
    ????}
    ????
    ????public static void main(String args[]){
    ???????? byte[] a=getIpByteArrayFromString(args[0]);
    ??????????for(int i=0;i< a.length;i++)
    ????????????????System.out.println(a[i]);
    ??????????System.out.println(getIpStringFromBytes(a));?
    ????}
    ????/**
    ???? * 對(duì)原始字符串進(jìn)行編碼轉(zhuǎn)換,如果失敗,返回原始的字符串
    ???? * @param s 原始字符串
    ???? * @param srcEncoding 源編碼方式
    ???? * @param destEncoding 目標(biāo)編碼方式
    ???? * @return 轉(zhuǎn)換編碼后的字符串,失敗返回原始字符串
    ???? */
    ????public static String getString(String s, String srcEncoding, String destEncoding) {
    ????????try {
    ????????????return new String(s.getBytes(srcEncoding), destEncoding);
    ????????} catch (UnsupportedEncodingException e) {
    ????????????return s;
    ????????}
    ????}
    ????
    ????/**
    ???? * 根據(jù)某種編碼方式將字節(jié)數(shù)組轉(zhuǎn)換成字符串
    ???? * @param b 字節(jié)數(shù)組
    ???? * @param encoding 編碼方式
    ???? * @return 如果encoding不支持,返回一個(gè)缺省編碼的字符串
    ???? */
    ????public static String getString(byte[] b, String encoding) {
    ????????try {
    ????????????return new String(b, encoding);
    ????????} catch (UnsupportedEncodingException e) {
    ????????????return new String(b);
    ????????}
    ????}
    ????
    ????/**
    ???? * 根據(jù)某種編碼方式將字節(jié)數(shù)組轉(zhuǎn)換成字符串
    ???? * @param b 字節(jié)數(shù)組
    ???? * @param offset 要轉(zhuǎn)換的起始位置
    ???? * @param len 要轉(zhuǎn)換的長度
    ???? * @param encoding 編碼方式
    ???? * @return 如果encoding不支持,返回一個(gè)缺省編碼的字符串
    ???? */
    ????public static String getString(byte[] b, int offset, int len, String encoding) {
    ????????try {
    ????????????return new String(b, offset, len, encoding);
    ????????} catch (UnsupportedEncodingException e) {
    ????????????return new String(b, offset, len);
    ????????}
    ????}
    ????
    ????/**
    ???? * @param ip ip的字節(jié)數(shù)組形式
    ???? * @return 字符串形式的ip
    ???? */
    ????public static String getIpStringFromBytes(byte[] ip) {
    ????????StringBuffer sb = new StringBuffer();
    ????????sb.append(ip[0] & 0xFF);
    ????????sb.append('.');?? ????
    ????????sb.append(ip[1] & 0xFF);
    ????????sb.append('.');?? ????
    ????????sb.append(ip[2] & 0xFF);
    ????????sb.append('.');?? ????
    ????????sb.append(ip[3] & 0xFF);
    ????????return sb.toString();
    ????}

    }?


    三、IPSeeker.java

    /*
    * LumaQQ - Java QQ Client
    *
    * Copyright (C) 2004 luma < stubma@163.com>
    *
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */



    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.ByteOrder;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;




    /**
    *??* 用來讀取QQwry.dat文件,以根據(jù)ip獲得好友位置,QQwry.dat的格式是
    * 一. 文件頭,共8字節(jié)
    * ?????? 1. 第一個(gè)起始IP的絕對(duì)偏移, 4字節(jié)
    *???? 2. 最后一個(gè)起始IP的絕對(duì)偏移, 4字節(jié)
    * 二. "結(jié)束地址/國家/區(qū)域"記錄區(qū)
    *???? 四字節(jié)ip地址后跟的每一條記錄分成兩個(gè)部分
    *???? 1. 國家記錄
    *???? 2. 地區(qū)記錄
    *???? 但是地區(qū)記錄是不一定有的。而且國家記錄和地區(qū)記錄都有兩種形式
    *???? 1. 以0結(jié)束的字符串
    *???? 2. 4個(gè)字節(jié),一個(gè)字節(jié)可能為0x1或0x2
    * ??????????a. 為0x1時(shí),表示在絕對(duì)偏移后還跟著一個(gè)區(qū)域的記錄,注意是絕對(duì)偏移之后,而不是這四個(gè)字節(jié)之后
    *????????b. 為0x2時(shí),表示在絕對(duì)偏移后沒有區(qū)域記錄
    *????????不管為0x1還是0x2,后三個(gè)字節(jié)都是實(shí)際國家名的文件內(nèi)絕對(duì)偏移
    * ??????????如果是地區(qū)記錄,0x1和0x2的含義不明,但是如果出現(xiàn)這兩個(gè)字節(jié),也肯定是跟著3個(gè)字節(jié)偏移,如果不是
    *????????則為0結(jié)尾字符串
    * 三. "起始地址/結(jié)束地址偏移"記錄區(qū)
    *???? 1. 每條記錄7字節(jié),按照起始地址從小到大排列
    *????????a. 起始IP地址,4字節(jié)
    *????????b. 結(jié)束ip地址的絕對(duì)偏移,3字節(jié)
    *
    * 注意,這個(gè)文件里的ip地址和所有的偏移量均采用little-endian格式,而java是采用
    * big-endian格式的,要注意轉(zhuǎn)換
    *?
    *
    * @author 馬若劼
    */
    public class IPSeeker {
    ????/**
    ???? * ???? * 用來封裝ip相關(guān)信息,目前只有兩個(gè)字段,ip所在的國家和地區(qū)
    ???? *?
    ???? *
    ???? * @author 馬若劼
    ???? */
    ????private class IPLocation {
    ????????public String country;
    ????????public String area;

    ????????public IPLocation() {
    ????????????country = area = "";
    ????????}

    ????????public IPLocation getCopy() {
    ????????????IPLocation ret = new IPLocation();
    ????????????ret.country = country;
    ????????????ret.area = area;
    ????????????return ret;
    ????????}
    ????}

    ????private static final String IP_FILE = IPSeeker.class.getResource("/QQWry.dat").toString().substring(5);

    ????// 一些固定常量,比如記錄長度等等
    ????private static final int IP_RECORD_LENGTH = 7;
    ????private static final byte AREA_FOLLOWED = 0x01;
    ????private static final byte NO_AREA = 0x2;

    ????// 用來做為cache,查詢一個(gè)ip時(shí)首先查看cache,以減少不必要的重復(fù)查找
    ????private Hashtable ipCache;
    ????// 隨機(jī)文件訪問類
    ????private RandomAccessFile ipFile;
    ????// 內(nèi)存映射文件
    ????private MappedByteBuffer mbb;
    ????// 單一模式實(shí)例
    ????private static IPSeeker instance = new IPSeeker();
    ????// 起始地區(qū)的開始和結(jié)束的絕對(duì)偏移
    ????private long ipBegin, ipEnd;
    ????// 為提高效率而采用的臨時(shí)變量
    ????private IPLocation loc;
    ????private byte[] buf;
    ????private byte[] b4;
    ????private byte[] b3;

    ????/**
    ???? * 私有構(gòu)造函數(shù)
    ???? */
    ????private IPSeeker()??{
    ????????ipCache = new Hashtable();
    ????????loc = new IPLocation();
    ????????buf = new byte[100];
    ????????b4 = new byte[4];
    ????????b3 = new byte[3];
    ????????try {
    ????????????ipFile = new RandomAccessFile(IP_FILE, "r");
    ????????} catch (FileNotFoundException e) {
    ????????????????????????System.out.println(IPSeeker.class.getResource("/QQWry.dat").toString());
    ????????????????????????System.out.println(IP_FILE);
    ????????????System.out.println("IP地址信息文件沒有找到,IP顯示功能將無法使用");
    ????????????ipFile = null;

    ????????}
    ????????// 如果打開文件成功,讀取文件頭信息
    ????????if(ipFile != null) {
    ????????????try {
    ????????????????ipBegin = readLong4(0);
    ????????????????ipEnd = readLong4(4);
    ????????????????if(ipBegin == -1 || ipEnd == -1) {
    ????????????????????ipFile.close();
    ????????????????????ipFile = null;
    ????????????????}
    ????????????} catch (IOException e) {
    ????????????????System.out.println("IP地址信息文件格式有錯(cuò)誤,IP顯示功能將無法使用");
    ????????????????ipFile = null;
    ????????????}
    ????????}
    ????}

    ????/**
    ???? * @return 單一實(shí)例
    ???? */
    ????public static IPSeeker getInstance() {
    ????????return instance;
    ????}

    ????/**
    ???? * 給定一個(gè)地點(diǎn)的不完全名字,得到一系列包含s子串的IP范圍記錄
    ???? * @param s 地點(diǎn)子串
    ???? * @return 包含IPEntry類型的List
    ???? */
    ????public List getIPEntriesDebug(String s) {
    ????????List ret = new ArrayList();
    ????????long endOffset = ipEnd + 4;
    ????????for(long offset = ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
    ????????????// 讀取結(jié)束IP偏移
    ????????????long temp = readLong3(offset);
    ????????????// 如果temp不等于-1,讀取IP的地點(diǎn)信息
    ????????????if(temp != -1) {
    ????????????????IPLocation loc = getIPLocation(temp);
    ????????????????// 判斷是否這個(gè)地點(diǎn)里面包含了s子串,如果包含了,添加這個(gè)記錄到List中,如果沒有,繼續(xù)
    ????????????????if(loc.country.indexOf(s) != -1 || loc.area.indexOf(s) != -1) {
    ????????????????????IPEntry entry = new IPEntry();
    ????????????????????entry.country = loc.country;
    ????????????????????entry.area = loc.area;
    ????????????????????// 得到起始IP
    ????????????????????readIP(offset - 4, b4);
    ????????????????????entry.beginIp = Utils.getIpStringFromBytes(b4);
    ????????????????????// 得到結(jié)束IP
    ????????????????????readIP(temp, b4);
    ????????????????????entry.endIp = Utils.getIpStringFromBytes(b4);
    ????????????????????// 添加該記錄
    ????????????????????ret.add(entry);
    ????????????????}
    ????????????}
    ????????}
    ????????return ret;
    ????}

    ????/**
    ???? * 給定一個(gè)地點(diǎn)的不完全名字,得到一系列包含s子串的IP范圍記錄
    ???? * @param s 地點(diǎn)子串
    ???? * @return 包含IPEntry類型的List
    ???? */
    ????public List getIPEntries(String s) {
    ????????List ret = new ArrayList();
    ????????try {
    ????????????// 映射IP信息文件到內(nèi)存中
    ????????????if(mbb == null) {
    ????????????????FileChannel fc = ipFile.getChannel();
    ????????????????mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFile.length());
    ????????????????mbb.order(ByteOrder.LITTLE_ENDIAN);
    ????????????}

    ????????????int endOffset = (int)ipEnd;
    ????????????for(int offset = (int)ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
    ????????????????int temp = readInt3(offset);
    ????????????????if(temp != -1) {
    ????????????????????IPLocation loc = getIPLocation(temp);
    ????????????????????// 判斷是否這個(gè)地點(diǎn)里面包含了s子串,如果包含了,添加這個(gè)記錄到List中,如果沒有,繼續(xù)
    ????????????????????if(loc.country.indexOf(s) != -1 || loc.area.indexOf(s) != -1) {
    ????????????????????????IPEntry entry = new IPEntry();
    ????????????????????????entry.country = loc.country;
    ????????????????????????entry.area = loc.area;
    ????????????????????????// 得到起始IP
    ????????????????????????readIP(offset - 4, b4);
    ????????????????????????entry.beginIp = Utils.getIpStringFromBytes(b4);
    ????????????????????????// 得到結(jié)束IP
    ????????????????????????readIP(temp, b4);
    ????????????????????????entry.endIp = Utils.getIpStringFromBytes(b4);
    ????????????????????????// 添加該記錄
    ????????????????????????ret.add(entry);
    ????????????????????}
    ????????????????}
    ????????????}
    ????????} catch (IOException e) {
    ????????????System.out.println(e.getMessage());
    ????????}
    ????????return ret;
    ????}

    ????/**
    ???? * 從內(nèi)存映射文件的offset位置開始的3個(gè)字節(jié)讀取一個(gè)int
    ???? * @param offset
    ???? * @return
    ???? */
    ????private int readInt3(int offset) {
    ????????mbb.position(offset);
    ????????return mbb.getInt() & 0x00FFFFFF;
    ????}

    ????/**
    ???? * 從內(nèi)存映射文件的當(dāng)前位置開始的3個(gè)字節(jié)讀取一個(gè)int
    ???? * @return
    ???? */
    ????private int readInt3() {
    ????????return mbb.getInt() & 0x00FFFFFF;
    ????}

    ????/**
    ???? * 根據(jù)IP得到國家名
    ???? * @param ip ip的字節(jié)數(shù)組形式
    ???? * @return 國家名字符串
    ???? */
    ????public String getCountry(byte[] ip) {
    ????????// 檢查ip地址文件是否正常
    ????????if(ipFile == null) return "錯(cuò)誤的IP數(shù)據(jù)庫文件";
    ????????// 保存ip,轉(zhuǎn)換ip字節(jié)數(shù)組為字符串形式
    ????????String ipStr = Utils.getIpStringFromBytes(ip);
    ????????// 先檢查cache中是否已經(jīng)包含有這個(gè)ip的結(jié)果,沒有再搜索文件
    ????????if(ipCache.containsKey(ipStr)) {
    ????????????IPLocation loc = (IPLocation)ipCache.get(ipStr);
    ????????????return loc.country;
    ????????} else {
    ????????????IPLocation loc = getIPLocation(ip);
    ????????????ipCache.put(ipStr, loc.getCopy());
    ????????????return loc.country;
    ????????}
    ????}

    ????/**
    ???? * 根據(jù)IP得到國家名
    ???? * @param ip IP的字符串形式
    ???? * @return 國家名字符串
    ???? */
    ????public String getCountry(String ip) {
    ????????return getCountry(Utils.getIpByteArrayFromString(ip));
    ????}

    ????/**
    ???? * 根據(jù)IP得到地區(qū)名
    ???? * @param ip ip的字節(jié)數(shù)組形式
    ???? * @return 地區(qū)名字符串
    ???? */
    ????public String getArea(byte[] ip) {
    ????????// 檢查ip地址文件是否正常
    ????????if(ipFile == null) return "錯(cuò)誤的IP數(shù)據(jù)庫文件";
    ????????// 保存ip,轉(zhuǎn)換ip字節(jié)數(shù)組為字符串形式
    ????????String ipStr = Utils.getIpStringFromBytes(ip);
    ????????// 先檢查cache中是否已經(jīng)包含有這個(gè)ip的結(jié)果,沒有再搜索文件
    ????????if(ipCache.containsKey(ipStr)) {
    ????????????IPLocation loc = (IPLocation)ipCache.get(ipStr);
    ????????????return loc.area;
    ????????} else {
    ????????????IPLocation loc = getIPLocation(ip);
    ????????????ipCache.put(ipStr, loc.getCopy());
    ????????????return loc.area;
    ????????}
    ????}

    ????/**
    ???? * 根據(jù)IP得到地區(qū)名
    ???? * @param ip IP的字符串形式
    ???? * @return 地區(qū)名字符串
    ???? */
    ????public String getArea(String ip) {
    ????????return getArea(Utils.getIpByteArrayFromString(ip));
    ????}

    ????/**
    ???? * 根據(jù)ip搜索ip信息文件,得到IPLocation結(jié)構(gòu),所搜索的ip參數(shù)從類成員ip中得到
    ???? * @param ip 要查詢的IP
    ???? * @return IPLocation結(jié)構(gòu)
    ???? */
    ????private IPLocation getIPLocation(byte[] ip) {
    ????????IPLocation info = null;
    ????????long offset = locateIP(ip);
    ????????if(offset != -1)
    ????????????info = getIPLocation(offset);
    ????????if(info == null) {
    ????????????info = new IPLocation();
    ????????????info.country = "未知國家";
    ????????????info.area = "未知地區(qū)";
    ????????}
    ????????return info;
    ????}

    ????/**
    ???? * 從offset位置讀取4個(gè)字節(jié)為一個(gè)long,因?yàn)閖ava為big-endian格式,所以沒辦法
    ???? * 用了這么一個(gè)函數(shù)來做轉(zhuǎn)換
    ???? * @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個(gè)字節(jié)為一個(gè)long,因?yàn)閖ava為big-endian格式,所以沒辦法
    ???? * 用了這么一個(gè)函數(shù)來做轉(zhuǎn)換
    ???? * @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;
    ????????}
    ????}

    ????/**
    ???? * 從當(dāng)前位置讀取3個(gè)字節(jié)轉(zhuǎn)換成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位置讀取四個(gè)字節(jié)的ip地址放入ip數(shù)組中,讀取后的ip為big-endian格式,但是
    ???? * 文件中是little-endian形式,將會(huì)進(jìn)行轉(zhuǎn)換
    ???? * @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位置讀取四個(gè)字節(jié)的ip地址放入ip數(shù)組中,讀取后的ip為big-endian格式,但是
    ???? * 文件中是little-endian形式,將會(huì)進(jìn)行轉(zhuǎn)換
    ???? * @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比較,注意這個(gè)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;
    ????}

    ????/**
    ???? * 把兩個(gè)byte當(dāng)作無符號(hào)數(shù)進(jìn)行比較
    ???? * @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;
    ????}

    ????/**
    ???? * 這個(gè)方法將根據(jù)ip的內(nèi)容,定位到包含這個(gè)ip國家地區(qū)的記錄處,返回一個(gè)絕對(duì)偏移
    ???? * 方法使用二分法查找。
    ???? * @param ip 要查詢的IP
    ???? * @return 如果找到了,返回結(jié)束IP的偏移,如果沒有找到,返回-1
    ???? */
    ????private long locateIP(byte[] ip) {
    ????????long m = 0;
    ????????int r;
    ????????// 比較第一個(gè)ip項(xiàng)
    ????????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);
    ????????}
    ????????// 如果循環(huán)結(jié)束了,那么i和j必定是相等的,這個(gè)記錄為最可能的記錄,但是并非
    ????????//???? 肯定就是,還要檢查一下,如果是,就返回結(jié)束地址區(qū)的絕對(duì)偏移
    ????????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;
    ????}

    ????/**
    ???? * 給定一個(gè)ip國家地區(qū)記錄的偏移,返回一個(gè)IPLocation結(jié)構(gòu)
    ???? * @param offset
    ???? * @return
    ???? */
    ????private IPLocation getIPLocation(long offset) {
    ????????try {
    ????????????// 跳過4字節(jié)ip
    ????????????ipFile.seek(offset + 4);
    ????????????// 讀取第一個(gè)字節(jié)判斷是否標(biāo)志字節(jié)
    ????????????byte b = ipFile.readByte();
    ????????????if(b == AREA_FOLLOWED) {
    ????????????????// 讀取國家偏移
    ????????????????long countryOffset = readLong3();
    ????????????????// 跳轉(zhuǎn)至偏移處
    ????????????????ipFile.seek(countryOffset);
    ????????????????// 再檢查一次標(biāo)志字節(jié),因?yàn)檫@個(gè)時(shí)候這個(gè)地方仍然可能是個(gè)重定向
    ????????????????b = ipFile.readByte();
    ????????????????if(b == NO_AREA) {
    ????????????????????loc.country = readString(readLong3());
    ????????????????????ipFile.seek(countryOffset + 4);
    ????????????????} else
    ????????????????????loc.country = readString(countryOffset);
    ????????????????// 讀取地區(qū)標(biāo)志
    ????????????????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字節(jié)ip
    ????????mbb.position(offset + 4);
    ????????// 讀取第一個(gè)字節(jié)判斷是否標(biāo)志字節(jié)
    ????????byte b = mbb.get();
    ????????if(b == AREA_FOLLOWED) {
    ????????????// 讀取國家偏移
    ????????????int countryOffset = readInt3();
    ????????????// 跳轉(zhuǎn)至偏移處
    ????????????mbb.position(countryOffset);
    ????????????// 再檢查一次標(biāo)志字節(jié),因?yàn)檫@個(gè)時(shí)候這個(gè)地方仍然可能是個(gè)重定向
    ????????????b = mbb.get();
    ????????????if(b == NO_AREA) {
    ????????????????loc.country = readString(readInt3());
    ????????????????mbb.position(countryOffset + 4);
    ????????????} else
    ????????????????loc.country = readString(countryOffset);
    ????????????// 讀取地區(qū)標(biāo)志
    ????????????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偏移開始解析后面的字節(jié),讀出一個(gè)地區(qū)名
    ???? * @param offset
    ???? * @return 地區(qū)名字符串
    ???? * @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 "未知地區(qū)";
    ????????????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 "未知地區(qū)";
    ????????????else
    ????????????????return readString(areaOffset);
    ????????} else
    ????????????return readString(offset);
    ????}

    ????/**
    ???? * 從offset偏移處讀取一個(gè)以0結(jié)束的字符串
    ???? * @param offset
    ???? * @return 讀取的字符串,出錯(cuò)返回空字符串
    ???? */
    ????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 "";
    ????}

    ????/**
    ???? * 從內(nèi)存映射文件的offset位置得到一個(gè)0結(jié)尾字符串
    ???? * @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 country = getCountry(ip).equals(" CZ88.NET")?"":getCountry(ip);
    ????????String area = getArea(ip).equals(" CZ88.NET")?"":getArea(ip);
    ????????String address = country+" "+area;
    ????????return address.trim();
    ????}
    }
    posted on 2010-11-20 14:23 禮物 閱讀(3529) 評(píng)論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 浮力影院第一页小视频国产在线观看免费| 91在线精品亚洲一区二区| 国产精品久久免费| 男女拍拍拍免费视频网站| 亚洲综合色一区二区三区| 久久av无码专区亚洲av桃花岛| 国产亚洲精品自在线观看| 国产日产成人免费视频在线观看 | 4480yy私人影院亚洲| 亚洲午夜精品在线| 亚洲最新视频在线观看| 亚洲H在线播放在线观看H| 亚洲狠狠狠一区二区三区| 色婷婷六月亚洲婷婷丁香| 亚洲欧洲另类春色校园网站| 亚洲另类精品xxxx人妖| 成人亚洲国产精品久久| 亚洲精品日韩一区二区小说| 亚洲老熟女五十路老熟女bbw| 亚洲性色AV日韩在线观看| 少妇亚洲免费精品| 一级女人18片毛片免费视频| 免费夜色污私人影院网站| 全部在线播放免费毛片| 免费国产黄网站在线观看可以下载| 免费视频成人手机在线观看网址| 精品久久久久久国产免费了| 久视频精品免费观看99| 精品福利一区二区三区免费视频| **一级一级毛片免费观看| 国产三级免费电影| 亚洲一级Av无码毛片久久精品| 亚洲av无码乱码在线观看野外 | 亚洲国产精品xo在线观看| 国产成人亚洲毛片| 曰批全过程免费视频观看免费软件| 成全在线观看免费观看大全 | www视频在线观看免费| 日韩亚洲国产综合久久久| 国产午夜亚洲不卡| 亚洲精品福利你懂|