当前位置:Gxlcms > 数据库问题 > java读取真IP数据库QQwry.dat的源代码(转载)

java读取真IP数据库QQwry.dat的源代码(转载)

时间:2021-07-01 10:21:17 帮助过:3人阅读

二、Utils.java

技术分享/*
技术分享 * Created on 2004-8-4
技术分享 *
技术分享 */
技术分享
技术分享
技术分享import java.io.UnsupportedEncodingException;
技术分享
技术分享/**
技术分享 * @author LJ-silver
技术分享 */
技术分享public class Utils {
技术分享    /**
技术分享     * 从ip的字符串形式得到字节数组形式
技术分享     * @param ip 字符串形式的ip
技术分享     * @return 字节数组形式的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)); 
技术分享    }
技术分享    /**
技术分享     * 对原始字符串进行编码转换,如果失败,返回原始的字符串
技术分享     * @param s 原始字符串
技术分享     * @param srcEncoding 源编码方式
技术分享     * @param destEncoding 目标编码方式
技术分享     * @return 转换编码后的字符串,失败返回原始字符串
技术分享     */
技术分享    public static String getString(String s, String srcEncoding, String destEncoding) {
技术分享        try {
技术分享            return new String(s.getBytes(srcEncoding), destEncoding);
技术分享        } catch (UnsupportedEncodingException e) {
技术分享            return s;
技术分享        }
技术分享    }
技术分享    
技术分享    /**
技术分享     * 根据某种编码方式将字节数组转换成字符串
技术分享     * @param b 字节数组
技术分享     * @param encoding 编码方式
技术分享     * @return 如果encoding不支持,返回一个缺省编码的字符串
技术分享     */
技术分享    public static String getString(byte[] b, String encoding) {
技术分享        try {
技术分享            return new String(b, encoding);
技术分享        } catch (UnsupportedEncodingException e) {
技术分享            return new String(b);
技术分享        }
技术分享    }
技术分享    
技术分享    /**
技术分享     * 根据某种编码方式将字节数组转换成字符串
技术分享     * @param b 字节数组
技术分享     * @param offset 要转换的起始位置
技术分享     * @param len 要转换的长度
技术分享     * @param encoding 编码方式
技术分享     * @return 如果encoding不支持,返回一个缺省编码的字符串
技术分享     */
技术分享    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的字节数组形式
技术分享     * @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文件,以根据ip获得好友位置,QQwry.dat的格式是
技术分享 * 一. 文件头,共8字节
技术分享 *        1. 第一个起始IP的绝对偏移, 4字节
技术分享 *     2. 最后一个起始IP的绝对偏移, 4字节
技术分享 * 二. "结束地址/国家/区域"记录区
技术分享 *     四字节ip地址后跟的每一条记录分成两个部分
技术分享 *     1. 国家记录
技术分享 *     2. 地区记录
技术分享 *     但是地区记录是不一定有的。而且国家记录和地区记录都有两种形式
技术分享 *     1. 以0结束的字符串
技术分享 *     2. 4个字节,一个字节可能为0x1或0x2
技术分享 *           a. 为0x1时,表示在绝对偏移后还跟着一个区域的记录,注意是绝对偏移之后,而不是这四个字节之后
技术分享 *        b. 为0x2时,表示在绝对偏移后没有区域记录
技术分享 *        不管为0x1还是0x2,后三个字节都是实际国家名的文件内绝对偏移
技术分享 *           如果是地区记录,0x1和0x2的含义不明,但是如果出现这两个字节,也肯定是跟着3个字节偏移,如果不是
技术分享 *        则为0结尾字符串
技术分享 * 三. "起始地址/结束地址偏移"记录区
技术分享 *     1. 每条记录7字节,按照起始地址从小到大排列
技术分享 *        a. 起始IP地址,4字节
技术分享 *        b. 结束ip地址的绝对偏移,3字节
技术分享 *
技术分享 * 注意,这个文件里的ip地址和所有的偏移量均采用little-endian格式,而java是采用
技术分享 * big-endian格式的,要注意转换
技术分享 * 
技术分享 *
技术分享 * @author 马若劼
技术分享 */
技术分享public class IPSeeker {
技术分享    /**
技术分享     *      * 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区
技术分享     * 
技术分享     *
技术分享     * @author swallow     */
技术分享    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,查询一个ip时首先查看cache,以减少不必要的重复查找
技术分享    private Hashtable ipCache;
技术分享    // 随机文件访问类
技术分享    private RandomAccessFile ipFile;
技术分享    // 内存映射文件
技术分享    private MappedByteBuffer mbb;
技术分享    // 单一模式实例
技术分享    private static IPSeeker instance = new IPSeeker();
技术分享    // 起始地区的开始和结束的绝对偏移
技术分享    private long ipBegin, ipEnd;
技术分享    // 为提高效率而采用的临时变量
技术分享    private IPLocation loc;
技术分享    private byte[] buf;
技术分享    private byte[] b4;
技术分享    private byte[] b3;
技术分享
技术分享    /**
技术分享     * 私有构造函数
技术分享     */
技术分享    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地址信息文件格式有错误,IP显示功能将无法使用");
技术分享                ipFile = null;
技术分享            }
技术分享        }
技术分享    }
技术分享
技术分享    /**
技术分享     * @return 单一实例
技术分享     */
技术分享    public static IPSeeker getInstance() {
技术分享        return instance;
技术分享    }
技术分享
技术分享    /**
技术分享     * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
技术分享     * @param s 地点子串
技术分享     * @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) {
技术分享            // 读取结束IP偏移
技术分享            long temp = readLong3(offset);
技术分享            // 如果temp不等于-1,读取IP的地点信息
技术分享            if(temp != -1) {
技术分享                IPLocation loc = getIPLocation(temp);
技术分享                // 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
技术分享                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);
技术分享                    // 得到结束IP
技术分享                    readIP(temp, b4);
技术分享                    entry.endIp = Utils.getIpStringFromBytes(b4);
技术分享                    // 添加该记录
技术分享                    ret.add(entry);
技术分享                }
技术分享            }
技术分享        }
技术分享        return ret;
技术分享    }
技术分享
技术分享    /**
技术分享     * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
技术分享     * @param s 地点子串
技术分享     * @return 包含IPEntry类型的List
技术分享     */
技术分享    public List getIPEntries(String s) {
技术分享        List ret = new ArrayList();
技术分享        try {
技术分享            // 映射IP信息文件到内存中
技术分享            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);
技术分享                    // 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
技术分享                    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);
技术分享                        // 得到结束IP
技术分享                        readIP(temp, b4);
技术分享                        entry.endIp = Utils.getIpStringFromBytes(b4);
技术分享                        // 添加该记录
技术分享                        ret.add(entry);
技术分享                    }
技术分享                }
技术分享            }
技术分享        } catch (IOException e) {
技术分享            System.out.println(e.getMessage());
技术分享        }
技术分享        return ret;
技术分享    }
技术分享
技术分享    /**
技术分享     * 从内存映射文件的offset位置开始的3个字节读取一个int
技术分享     * @param offset
技术分享     * @return
技术分享     */
技术分享    private int readInt3(int offset) {
技术分享        mbb.position(offset);
技术分享        return mbb.getInt() & 0x00FFFFFF;
技术分享    }
技术分享
技术分享    /**
技术分享     * 从内存映射文件的当前位置开始的3个字节读取一个int
技术分享     * @return
技术分享     */
技术分享    private int readInt3() {
技术分享        return mbb.getInt() & 0x00FFFFFF;
技术分享    }
技术分享
技术分享    /**
技术分享     * 根据IP得到国家名
技术分享     * @param ip ip的字节数组形式
技术分享     * @return 国家名字符串
技术分享

人气教程排行