I have to filter IP address of Ipv4(32 bit) , but when i use array index up to 32 bit {array[32 bit]} it give error. So i divided the ip to three parts as; 1-one part contain index value of 25 bit { array[25 bit]} 2- second part contain value of 5 bit and this value is decoded using 5 to 32 decoder. 3-The third part of two bit indicate 4 different array in which 5 bit decoded value is stored on 25 bit address location. It gives an error on fourth short array. when i reduce address to 24 bit and decode 5 bit to store in 8 different short array, here 3 bits select array out of 8, then it also give an error on eighth short array, "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"
How can this problem is resolved? Is there any other data structure like linkedlist, tree etc that can solve this problem?
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");
}
}
}