⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookupservice.java

📁 GeoIP提供的Java API
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		try{		    record.longitude = Float.parseFloat(value);		} catch(NumberFormatException e) {		    record.latitude = 0;		}	    }	    if (key.equals("dm")) {		try{		    record.dma_code = Integer.parseInt(value);		} catch(NumberFormatException e) {		    record.dma_code = 0;		}	    }	    if (key.equals("ac")) {		try{		    record.area_code = Integer.parseInt(value);		} catch(NumberFormatException e) {		    record.area_code = 0;		}	    }	}        return record;    }    public synchronized Region getRegion(String str) {	InetAddress addr;	try {	    addr = InetAddress.getByName(str);	}	catch (UnknownHostException e) {	    return null;	}	return getRegion(bytesToLong(addr.getAddress()));    }    public synchronized Region getRegion(long ipnum) {        Region record = new Region();        int seek_region = 0;        if (databaseType == DatabaseInfo.REGION_EDITION_REV0) {            seek_region = seekCountry(ipnum) - STATE_BEGIN_REV0;            char ch[] = new char[2];            if (seek_region >= 1000) {                record.countryCode = "US";                record.countryName = "United States";                ch[0] = (char)(((seek_region - 1000)/26) + 65);                ch[1] = (char)(((seek_region - 1000)%26) + 65);	        record.region = new String(ch);            } else {                record.countryCode = countryCode[seek_region];                record.countryName = countryName[seek_region];                record.region = "";            }        } else if (databaseType == DatabaseInfo.REGION_EDITION_REV1) {            seek_region = seekCountry(ipnum) - STATE_BEGIN_REV1;            char ch[] = new char[2];            if (seek_region < US_OFFSET) {                record.countryCode = "";                record.countryName = "";	        record.region = "";            } else if (seek_region < CANADA_OFFSET) {                record.countryCode = "US";                record.countryName = "United States";                ch[0] = (char)(((seek_region - US_OFFSET)/26) + 65);                ch[1] = (char)(((seek_region - US_OFFSET)%26) + 65);	        record.region = new String(ch);            } else if (seek_region < WORLD_OFFSET) {                record.countryCode = "CA";                record.countryName = "Canada";                ch[0] = (char)(((seek_region - CANADA_OFFSET)/26) + 65);                ch[1] = (char)(((seek_region - CANADA_OFFSET)%26) + 65);	        record.region = new String(ch);            } else {                record.countryCode = countryCode[(seek_region - WORLD_OFFSET) / FIPS_RANGE];                record.countryName = countryName[(seek_region - WORLD_OFFSET) / FIPS_RANGE];                record.region = "";            }	}	return record;    }    public synchronized Location getLocation(long ipnum) {        int record_pointer;        byte record_buf[] = new byte[FULL_RECORD_LENGTH];        int record_buf_offset = 0;        Location record = new Location();        int str_length = 0;        int j, seek_country;        double latitude = 0, longitude = 0;        try {            seek_country = seekCountry(ipnum);            if (seek_country == databaseSegments[0]) {                return null;            }            record_pointer = seek_country + (2 * recordLength - 1) * databaseSegments[0];            if ((dboptions & GEOIP_MEMORY_CACHE) == 1) {                //read from memory                for (int i = 0; i < FULL_RECORD_LENGTH; i++) {                    record_buf[i] = dbbuffer[i+record_pointer];	        }            } else {                //read from disk                file.seek(record_pointer);                file.read(record_buf);            }            // get country            record.countryCode = countryCode[unsignedByteToInt(record_buf[0])];            record.countryName = countryName[unsignedByteToInt(record_buf[0])];            record_buf_offset++;            // get region            while (record_buf[record_buf_offset + str_length] != '\0')                str_length++;            if (str_length > 0) {                record.region = new String(record_buf, record_buf_offset, str_length);            }            record_buf_offset += str_length + 1;            str_length = 0;            // get city            while (record_buf[record_buf_offset + str_length] != '\0')                str_length++;            if (str_length > 0) {                record.city = new String(record_buf, record_buf_offset, str_length, "ISO-8859-1");            }            record_buf_offset += str_length + 1;            str_length = 0;            // get postal code            while (record_buf[record_buf_offset + str_length] != '\0')                str_length++;            if (str_length > 0) {                record.postalCode = new String(record_buf, record_buf_offset, str_length);            }            record_buf_offset += str_length + 1;            // get latitude            for (j = 0; j < 3; j++)                latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));            record.latitude = (float) latitude/10000 - 180;            record_buf_offset += 3;            // get longitude            for (j = 0; j < 3; j++)                longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));	    record.longitude = (float) longitude/10000 - 180;	    record.dma_code = 0;	    record.area_code = 0;	    if (databaseType == DatabaseInfo.CITY_EDITION_REV1) {		// get DMA code		int dmaarea_combo = 0;		if (record.countryCode == "US") {		    record_buf_offset += 3;		    for (j = 0; j < 3; j++)			dmaarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));		    record.dma_code = dmaarea_combo/1000;		    record.area_code = dmaarea_combo % 1000;		}            }	}	catch (IOException e) {            System.err.println("IO Exception while seting up segments");        }        return record;    }    public String getOrg(InetAddress addr) {        return getOrg(bytesToLong(addr.getAddress()));    }    public String getOrg(String str) {        InetAddress addr;	try {	    addr = InetAddress.getByName(str);	}	catch (UnknownHostException e) {            return null;	}	return getOrg(addr);    }    // GeoIP Organization and ISP Edition methods    public synchronized String getOrg(long ipnum) {        int seek_org;        int record_pointer;        int str_length = 0;        byte [] buf = new byte[MAX_ORG_RECORD_LENGTH];        String org_buf;        try {            seek_org = seekCountry(ipnum);            if (seek_org == databaseSegments[0]) {		return null;            }            record_pointer = seek_org + (2 * recordLength - 1) * databaseSegments[0];            if ((dboptions & GEOIP_MEMORY_CACHE) == 1) {                //read from memory                for (int i = 0;i < FULL_RECORD_LENGTH;i++) {                    buf[i] = dbbuffer[i+record_pointer];	        }            } else {		//read from disk                file.seek(record_pointer);                file.read(buf);            }            while (buf[str_length] != '\0') {		str_length++;            }            org_buf = new String(buf, 0, str_length, "ISO-8859-1");            return org_buf;        }        catch (IOException e) {            System.out.println("IO Exception");            return null;        }    }    /**     * Finds the country index value given an IP address.     *     * @param ipAddress the ip address to find in long format.     * @return the country index.     */    private synchronized int seekCountry(long ipAddress) {	byte [] buf = new byte[2 * MAX_RECORD_LENGTH];	int [] x = new int[2];        int offset = 0;        _check_mtime();        for (int depth = 31; depth >= 0; depth--) {            if ((dboptions & GEOIP_MEMORY_CACHE) == 1) {		//read from memory                for (int i = 0;i < 2 * MAX_RECORD_LENGTH;i++) {		    buf[i] = dbbuffer[(2 * recordLength * offset)+i];		}            } else if ((dboptions & GEOIP_INDEX_CACHE) != 0) {                //read from index cache                for (int i = 0;i < 2 * MAX_RECORD_LENGTH;i++) {		    buf[i] = index_cache[(2 * recordLength * offset)+i];		}                        } else {		//read from disk 		try {                    file.seek(2 * recordLength * offset);                    file.read(buf);                }                catch (IOException e) {                    System.out.println("IO Exception");                }            }            for (int i = 0; i<2; i++) {                x[i] = 0;                for (int j = 0; j<recordLength; j++) {                    int y = buf[i*recordLength+j];                    if (y < 0) {                        y+= 256;                    }                    x[i] += (y << (j * 8));                }            }            if ((ipAddress & (1 << depth)) > 0) {                if (x[1] >= databaseSegments[0]) {                    return x[1];                }                offset = x[1];            }            else {                if (x[0] >= databaseSegments[0]) {                    return x[0];                }                offset = x[0];	    }	}        // shouldn't reach here        System.err.println("Error seeking country while seeking " + ipAddress);        return 0;    }    /**     * Returns the long version of an IP address given an InetAddress object.     *     * @param address the InetAddress.     * @return the long form of the IP address.     */    private static long bytesToLong(byte [] address) {        long ipnum = 0;        for (int i = 0; i < 4; ++i) {            long y = address[i];            if (y < 0) {                y+= 256;            }            ipnum += y << ((3-i)*8);        }        return ipnum;    }    private static int unsignedByteToInt(byte b) {        return (int) b & 0xFF;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -