TreeMap floorEntry函数不使用整数值(Treemap定义为long)

时间:2015-05-22 02:49:50

标签: java ip-address treemap

我正在尝试执行基于间隔的搜索,我从文件加载并尝试查找我的ipaddress所在的间隔。以下是我的代码。此代码仅适用于较长但不适用于整数版本不是长号的IP地址。

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 Map。载入中 过度。 1432262975089正在启动查找。 540772找不到 1432262975089 \ n建立成功(总时间:4秒)

1 个答案:

答案 0 :(得分:0)

IP地址是32位无符号整数。在Java中,int是32位签名整数。

如果使用signed int来表示IP地址,则必须在代码中容纳所有IP地址的上半部分实际上都是负数的事实。

Java 7不提供对无符号int的内置支持,因此您必须实现所需的行为,或者为Integer找到满足您需求的另一个类包装器(来自某处)。

Java 8在Integer类中引入了用于将int作为无符号进行比较的方法。有关适当的方法,请参阅https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html