2015-05-22 49 views
0

我試圖執行基於時間間隔的搜索,我從文件加載並試圖找到我的ip地址所在的間隔。以下是我的代碼。此代碼只適用於很長時間,但不適用於整數版本不是很長的IP地址。TreeMap floorEntry函數不能使用整數值(Treemap定義爲long)

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Map; 
import java.util.NavigableMap; 
import java.util.Scanner; 
import java.util.StringTokenizer; 
import java.util.TreeMap; 


public class RangeBasedSearchAsn { 

    public static class AsnInfo { 
     private long asn; 
     private String ipSubnet; 
     private String isp; 

     @Override 
     public String toString() { 
      return "Here are the details:\n" 
        + this.asn + " " + this.ipSubnet + " " + this.isp ; 
     } 

     public AsnInfo(long asn, String ipSubnet, String isp) { 
      this.asn = asn; 
      this.ipSubnet = ipSubnet; 
      this.isp = isp; 
     } 
     public long getAsn() { 
      return asn; 
     } 

     public void setAsn(long asn) { 
      this.asn = asn; 
     } 

     public String getIpSubnet() { 
      return ipSubnet; 
     } 

     public void setIpSubnet(String ipSubnet) { 
      this.ipSubnet = ipSubnet; 
     } 

     public String getIsp() { 
      return isp; 
     } 

     public void setIsp(String isp) { 
      this.isp = isp; 
     } 


    } 

    public static class Range { 

      private long upper; 
     private AsnInfo asnInfo; 

     public Range(long upper, AsnInfo value) { 
      this.upper = upper; 
      this.asnInfo = value; 
     } 

     public long getUpper() { 
      return upper; 
     } 

     public void setUpper(long upper) { 
      this.upper = upper; 
     } 

     public AsnInfo getValue() { 
      return asnInfo; 
     } 

     public void setValue(AsnInfo value) { 
      this.asnInfo = value; 
     } 

    } 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
     long key = 848163455L; 
     NavigableMap<Long, Range> asnTreeMap = new TreeMap<>(); 
     System.out.println(System.currentTimeMillis()); 
     System.out.println("Loading isp Map."); 
     FileInputStream inputStream = null; 
     Scanner sc = null; 
     try { 
      inputStream = new FileInputStream("C:\\Talend\\TalendTestArea\\rbl_ipv4_zone.txt"); 
      sc = new Scanner(inputStream, "UTF-8"); 
      while (sc.hasNextLine()) { 
       String line = sc.nextLine(); 
       StringTokenizer st = new StringTokenizer(line, ";"); 
       while (st.hasMoreTokens() && st.countTokens() == 7) { 
         st.nextToken(); 
        st.nextToken(); 
        long token1 = Long.parseLong(st.nextToken()); 
        System.out.println("here is token1:" + token1); 
        long token2 = Long.parseLong(st.nextToken()); 
        System.out.println("here is token1:" + token2); 
        long token3 = Long.parseLong(st.nextToken()); 
        System.out.println("here is token1:" + token3); 
        asnTreeMap.put(token1, new Range(token2, new AsnInfo(token3,st.nextToken(),st.nextToken()))); 
       } 
      } 
      if (sc.ioException() != null) { 
       throw sc.ioException(); 
      } 
     } finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
      if (sc != null) { 
       sc.close(); 
      } 
     } 
     System.out.println("Loading Over."); 
     System.out.println(System.currentTimeMillis()); 
     System.out.println("Starting Lookup."); 
     long[] ips = {30503936L}; 
     for(int i = 0 ; i < ips.length;i++){ 
      System.out.println(asnTreeMap.size()); 
      Map.Entry<Long, Range> entry = asnTreeMap.floorEntry(ips[i]); 
     if (entry == null) { 
     System.out.println("Value not valid"); 
     } else if (key <= entry.getValue().upper) { 
     System.out.println("Carrier = " + entry.getValue().asnInfo.toString() + "\n"); 
     } else { 
     System.out.println("Not found"); 
     } 
     System.out.println(System.currentTimeMillis()); 
     } 
    } 
} 
  1. 下面是輸出運行:1432262970924加載ISP地圖。正在加載 。 1432262975089開始查找。 540772未找到 1432262975089 \ n BUILD SUCCESSFUL(總時間:4秒)
+2

請清理你的代碼 - 發佈*最小*,完整和可驗證的例子。用一半*註釋出來的例子*不是*最小*,我不打算閱讀或試圖理解它。 –

回答

0

IP地址是一個32位無符號整數。在Java中,int s是32位簽名的整數。

如果您使用帶符號的int來表示IP地址,則必須在代碼中容納所有IP地址的上半部分實際上都是負數的事實。

Java 7不提供對未簽名的int的內置支持,因此您必須實現所需的行爲或爲Integer找到其他類包裝器(從某處)以滿足您的需求。

Java 8在Integer類中引入了用於將int s作爲無符號數進行比較的方法。有關適當的方法,請參見https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

+0

感謝您的信息。 – hopeIsTheonlyWeapon

+0

幫助?我仍然不完全確定你的問題在問什麼。 – austinian

+0

是的,這是我的不好,但我明白了 – hopeIsTheonlyWeapon