Java tutorial
/* * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * http://www.apache.org/licenses/LICENSE-2.0 */ package com.dc.gameserver.extComponents.Kit.ip; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Map; /** * ?IP?? * */ public class IPSeeker { private static final IPSeeker instance = new IPSeeker(); private Log log = LogFactory.getLog(this.getClass()); // IP??? private String IP_FILE = "QQWry.Dat"; // ? private static final int IP_RECORD_LENGTH = 7; private static final byte REDIRECT_MODE_1 = 0x01; private static final byte REDIRECT_MODE_2 = 0x02; // ??cacheipcache????? private Map<String, IPLocation> ipCache; // ? private RandomAccessFile ipFile; // ???? private long ipBegin, ipEnd; private IPSeeker() { ipCache = new HashMap<String, IPLocation>(); try { ipFile = new RandomAccessFile(IPSeeker.class.getClassLoader().getResource("").getPath() + IP_FILE, "r"); // ??? if (ipFile != null) { try { ipBegin = readLong4(0); ipEnd = readLong4(4); if (ipBegin == -1 || ipEnd == -1) { ipFile.close(); ipFile = null; } } catch (IOException e) { log.info("IP???IP"); ipFile = null; } } } catch (FileNotFoundException e) { log.info("IP??IP"); } } public IPLocation getIPLocation(String ip) { IPLocation location = new IPLocation(); location.setArea(this.getArea(ip)); location.setCountry(this.getCountry(ip)); return location; } /** * ?IP?? * * @param ip * ip? * @return ?? */ public String getCountry(byte[] ip) { // ip?? if (ipFile == null) return Message.bad_ip_file; // ?ip?ip? String ipStr = Util.getIpStringFromBytes(ip); // cache???ip?? if (ipCache.containsKey(ipStr)) { IPLocation ipLoc = ipCache.get(ipStr); return ipLoc.getCountry(); } else { IPLocation ipLoc = getIPLocation(ip); ipCache.put(ipStr, ipLoc.getCopy()); return ipLoc.getCountry(); } } /** * ?IP?? * * @param ip * IP? * @return ?? */ public String getCountry(String ip) { return getCountry(Util.getIpByteArrayFromString(ip)); } /** * ?IP?? * * @param ip * ip? * @return ?? */ public String getArea(byte[] ip) { // ip?? if (ipFile == null) return Message.bad_ip_file; // ?ip?ip? String ipStr = Util.getIpStringFromBytes(ip); // cache???ip?? if (ipCache.containsKey(ipStr)) { IPLocation ipLoc = ipCache.get(ipStr); return ipLoc.getArea(); } else { IPLocation ipLoc = getIPLocation(ip); ipCache.put(ipStr, ipLoc.getCopy()); return ipLoc.getArea(); } } /** * ?IP?? * * @param ip * IP? * @return ?? */ public String getArea(String ip) { return getArea(Util.getIpByteArrayFromString(ip)); } /** * ?ip?ip?IPLocation?ip??ip * * @param ip * ?IP * @return IPLocation */ 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.setCountry(Message.unknown_country); info.setArea(Message.unknown_area); } return info; } /** * offset??4longjavabig-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??3longjavabig-endian? ??? * * @param offset * ?? * @return ?long-1? */ private long readLong3(long offset) { long ret = 0; byte[] b3 = new byte[3]; 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 ?long-1? */ private long readLong3() { long ret = 0; byte[] b3 = new byte[3]; 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??ipbig-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) { //log.error(e); e.printStackTrace(); } } /** * ?ipbeginIp?beginIpbig-endian * * @param ip * ?IP * @param beginIp * IPIP * @return 0ipbeginIp1?-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 b1b210?-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; byte[] b4 = new byte[4]; // 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); } // ?ij?? // ?????? 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 IPLocation */ private IPLocation getIPLocation(long offset) { IPLocation loc = new IPLocation(); try { // 4ip ipFile.seek(offset + 4); // ?? byte b = ipFile.readByte(); if (b == REDIRECT_MODE_1) { // ??? long countryOffset = readLong3(); // ?? ipFile.seek(countryOffset); // ????? b = ipFile.readByte(); if (b == REDIRECT_MODE_2) { loc.setCountry(readString(readLong3())); ipFile.seek(countryOffset + 4); } else loc.setCountry(readString(countryOffset)); // ? loc.setArea(readArea(ipFile.getFilePointer())); } else if (b == REDIRECT_MODE_2) { loc.setCountry(readString(readLong3())); loc.setArea(readArea(offset + 8)); } else { loc.setCountry(readString(ipFile.getFilePointer() - 1)); loc.setArea(readArea(ipFile.getFilePointer())); } return loc; } catch (IOException e) { return null; } } /** * offset??????? * * @param offset * ?? * @return ?? * @throws java.io.IOException */ private String readArea(long offset) throws IOException { ipFile.seek(offset); byte b = ipFile.readByte(); if (b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) { long areaOffset = readLong3(offset + 1); if (areaOffset == 0) return Message.unknown_area; else return readString(areaOffset); } else return readString(offset); } /** * offset???0? * * @param offset * ?? * @return ? */ private String readString(long offset) { byte[] buf = new byte[100]; try { ipFile.seek(offset); int i; for (i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile.readByte()) ; if (i != 0) return Util.getString(buf, 0, i, "GBK"); } catch (IOException e) { log.error(e); } return ""; } public static IPSeeker getInstance() { return instance; } }