使用java中的数组过滤ipv4地址

时间:2013-11-12 16:20:34

标签: java arrays

我必须过滤Ipv4(32位)的IP地址,但是当我使用最多32位{array [32 bit]}的数组索引时,它会给出错误。所以我将ip分为三部分; 1-one部分包含25位{array [25 bit]}的索引值 2-秒部分包含5比特的值,并且该值使用5到32解码器解码。 3-两位的第三部分表示4个不同的阵列,其中5位解码值存储在25位地址位置。 它给出了第四个短阵列的错误。 当我将地址减少到24位并解码5位存储在8个不同的短阵列中时,这里3位选择8个阵列中的数据,那么它也会在第8个短阵列上产生错误,    “线程中的异常”主“java.lang.OutOfMemoryError:Java堆空间”

如何解决此问题? 是否有其他数据结构,如linkedlist,tree等可以解决这个问题?

import java.util.Scanner; class IPAddress3 {

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.println("Enter IP address to search from list:");
    String ipAddress=input.nextLine();
    //String ipAddress="171.255.100.23";
    String[] octets = ipAddress.split("\\.");
    StringBuilder sb = new StringBuilder();
    for (String octet : octets) {

            int number = Integer.parseInt(octet);
            if(number>255)
            {
                System.out.println("Error:Invalid Ip address,\n" +
                        "Each octet should be less or equal to 255\n");
                System.exit(0);
            }
            else{
            String binaryPart = Integer.toBinaryString(number);
            if (binaryPart.length() < 8) {
                for (int i = binaryPart.length(); i < 8; i++) {
                    binaryPart = "0" + binaryPart;
                }
            }
            sb.append(binaryPart);
            }
    }
    String binaryForm = sb.toString();
    System.out.println(binaryForm);
    System.out.println("2 MSB: " + binaryForm.substring(0, 2));
    System.out.println("5 bits: " + binaryForm.substring(2, 7));
    System.out.println("25 LSB: " + binaryForm.substring(7));

    String MSB2Bit=binaryForm.substring(0, 2);
    String fivebits= binaryForm.substring(2, 7);
    String twenty5bits=binaryForm.substring(8);
    byte msb2Bit=Byte.parseByte(MSB2Bit);
    byte data=Byte.parseByte(fivebits,2);
    int address=Integer.parseInt(twenty5bits,2);
    System.out.println("Data:\t"+data);
    System.out.println("Address: "+address);



    short[] Array0=new short[33554432];
short[] Array1=new short[33554432];
    short[] Array2=new short[33554432];
    short[] Array3=new short[33554432];//error in this line

   /* 
    short[] Array0=new short[16777216];
    short[] Array1=new short[16777216];
    short[] Array2=new short[16777216];
    short[] Array3=new short[16777216];
    short[] Array4=new short[16777216];
    short[] Array5=new short[16777216];
    short[] Array6=new short[16777216];
    short[] Array7=new short[16777210];// error here
    /*


            Array0[1677721]=127; 
            Array1[1677721]=127;
            Array2[1677721]=127;



            int thirty2BitWord = 0 ;// Array0[ad]=Array0[ad] | Arra0[ad];
            int filterWord = 8;


                    switch(data)
                    {   
                            case 0:
                            thirty2BitWord=1;
                            break;

                            case 1:
                            thirty2BitWord=2;
                            break;

                            case 2:
                            thirty2BitWord=4;
                            break;

                            case 3:
                            thirty2BitWord=8;
                            break;
                            case 4:
                            thirty2BitWord=16;
                            break;

                            case 5:
                            thirty2BitWord=32;
                            break;

                            case 6:
                            thirty2BitWord=64;
                            break;

                            case 7:
                            thirty2BitWord=128;
                            break;

                            case 8:
                            thirty2BitWord=256;
                            break;

                            case 9:
                            thirty2BitWord=512;
                            break;

                            case 10:
                            thirty2BitWord=1024;
                            break;

                            case 11:
                            thirty2BitWord=2048;
                            break;

                            case 12:
                            thirty2BitWord=4096;
                            break;

                            case 13:
                            thirty2BitWord=8192;
                            break;

                            case 14:
                            thirty2BitWord=16384;
                            break;

                            case 15:
                            thirty2BitWord=32768;
                            break;

                            case 16:
                            thirty2BitWord=65536;
                            break;

                            case 17:
                            thirty2BitWord=131072;
                            break;

                            case 18:
                            thirty2BitWord=262144;
                            break;

                            case 19:
                            thirty2BitWord=524288;
                            break;

                            case 20:
                            thirty2BitWord=1048576;
                            break;

                            case 21:
                            thirty2BitWord=2097152;
                            break;

                            case 22:
                            thirty2BitWord=4194304;
                            break;

                            case 23:
                            thirty2BitWord=8388608;
                            break;

                            case 24:
                            thirty2BitWord=16777216;
                            break;

                            case 25:
                            thirty2BitWord=33554432;
                            break;

                            case 26:
                            thirty2BitWord=67108864;
                            break;

                            case 27:
                            thirty2BitWord=134217728;
                            break;

                            case 28:
                            thirty2BitWord=268435456;
                            break;

                            case 29:
                            thirty2BitWord=536870912;
                            break;

                            case 30:
                            thirty2BitWord=1073741824;
                            break;

                            case 31:
                            thirty2BitWord=2147483647;
                            break;


                    }

                    if(msb2Bit==0)
                    {
                        filterWord=thirty2BitWord & Array0[address];    
                    }
                    else if(msb2Bit==1)
                    {
                        filterWord=thirty2BitWord & Array1[address];    
                    }
                    else if(msb2Bit==2)
                    {
                        filterWord=thirty2BitWord & Array2[address];    
                    }
                   /* else 
                    {
                        filterWord=thirty2BitWord & Array3[address];
                    }*/

                    byte filterbit;
                    if(filterWord==0)
                    {
                        filterbit=0;

                        System.out.println("No such address is found in blacklist, So Allow\n");
                    }
                    else
                    {
                        filterbit=1;

                        System.out.println("IP match, So block this address\n"); 
                    }


}

}

1 个答案:

答案 0 :(得分:-1)

对于短期运行,您可以通过增加内存来修复堆大小错误:

How to increase application heap size in Eclipse?

我没有检查过你的代码太多,因为这个错误就是虚拟机内存不足。可能有一种更好的方法可以做到这一点,但如果程序只需要更多的RAM,这应该可以解决问题!