JAvaはipの具体的なアドレスを取得します

19484 ワード

本文は本の内容から抜粋して,私はよく記録したと思います.
public class IpUtil {

	public static String getIpAddress(String ip) {
		try{
			return IPSeeker.getInstance().getAddress(ip);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "    ";
	}

}
以下は具体的なコードです
import java.io.File;
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;




/**
 * <pre>
 *     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   ,     
 * </pre>
 *
 * @author    
 */
public class IPSeeker {
	/**
	 * <pre>
	 *     ip    ,        ,ip        
	 * </pre>
	 *
	 * @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 File IP_FILE = new File(IPSeeker.class.getResource("").getFile(), "QQWry.dat");

	//       ,        
	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(IP_FILE.getAbsolutePath());
                        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       
	 */
	public String getCountry(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;
		} else {
			IPLocation loc = getIPLocation(ip);
			ipCache.put(ipStr, loc.getCopy());
			return loc.country;
		}
	}

	/**
	 *   IP     
	 * @param ip IP      
	 * @return       
	 */
	public String getCountry(String ip) {
	    return getCountry(Utils.getIpByteArrayFromString(ip));
	}

	/**
	 *   IP     
	 * @param ip ip       
	 * @return       
	 */
	public String getArea(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.area;
		} else {
			IPLocation loc = getIPLocation(ip);
			ipCache.put(ipStr, loc.getCopy());
			return loc.area;
		}
	}

	/**
	 *   IP     
	 * @param ip IP      
	 * @return       
	 */
	public String getArea(String ip) {
	    return getArea(Utils.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.country = "    ";
			info.area = "    ";
		}
		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 country = getCountry(ip).equals(" CZ88.NET")?"":getCountry(ip);
		String area = getArea(ip).equals(" CZ88.NET")?"":getArea(ip);
        String address = country+" "+area;
		return address.trim();
	}
}
/**
 * <pre>
 *   IP    ,         ,     IP   IP
 * </pre>
 * 
 * @author    
 */
public class IPEntry {
    public String beginIp;
    public String endIp;
    public String country;
    public String area;
    
    /**
     *     
     */
    public IPEntry() {
        beginIp = endIp = country = area = "";
    }
    
    public String toString(){
    	return this.area+"  "+this.country+"  IP  :"+this.beginIp+"-"+this.endIp;
    }
}
/**
 * @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();
    }

}
テスト後
import java.util.*;
public class Test {

	public static void main(String[] args) {
		
		args = new String[]{"ip", "9.128.2.1"};
		
		IPSeeker seeker = IPSeeker.getInstance();

		if(args.length==2){
			if("ip".equals(args[0])){
				System.out.println(args[0]+"      :"+seeker.getAddress(args[1]));
                            System.out.println(args[0]+"        :"+seeker.getCountry(args[1]));
			}else if("address".equals(args[0])){
				List a = seeker.getIPEntries(args[1]);
                            System.out.println(args[0]+":");
				for(int i=0;i<a.size();i++){
					System.out.println(a.get(i).toString());
				}
			}else{
				System.out.println("usage:java Test ip/address yourIpString/yourAddressString");
			}
		}else{
			System.out.println("usage:java Test ip/address yourIpString/yourAddressString");
	 
              }
 }
}