The filepointer
gets stuck at a point resulting in a StackOverflowError
. Can you point me to what exactly is wrong here? The error exactly is: java.lang.StackOverflowError
I am doing a seek to find the location as record width is not fixed.
Here is the piece of code:
private static void binarySearch(RandomAccessFile raf, String searchvalue, Long low, Long high) throws IOException
{
Long middle = (low + high) / 2;
Long mreal = null;
if(low > raf.length() -1 || high > raf.length()-1 || low >= high) {
System.out.println("Element not found:"); return ;
}
StringBuilder sb = new StringBuilder();
for(long filePointer = middle; filePointer != -1; filePointer--) {
raf.seek(filePointer);
int readByte = raf.readByte();
if(readByte == 0xA) {
break;
}
sb.append((char)readByte);
}
String lastLine = sb.reverse().toString();
System.out.println(lastLine);
mreal = raf.getFilePointer();
String str = raf.readLine();
System.out.println(str);
String values[] = str.split("\t",-1);
int compared = searchvalue.compareTo(values[fieldindex]);
System.out.println(fieldindex);
if(compared == 0) {
System.out.println("Value found. The other details:");
for(int i=0; i < values.length;i++)
System.out.print("\t" + values[i]);
return;
} else if(compared < 0)
binarySearch(raf,searchvalue,low,mreal-1);
else if(compared > 0)
binarySearch(raf,searchvalue,mreal,high);
}
Stack trace:
Exception in thread "main" java.lang.StackOverflowError
>at java.util.regex.Pattern$Node.<init>(Pattern.java:2993)
>at java.util.regex.Pattern$CharProperty.<init>(Pattern.java:3332)
>at java.util.regex.Pattern$CharProperty.<init>(Pattern.java:3332)
>at java.util.regex.Pattern$BmpCharProperty.<init>(Pattern.java:3363)
>at java.util.regex.Pattern$BmpCharProperty.<init>(Pattern.java:3363)
>at java.util.regex.Pattern$Single.<init>(Pattern.java:3391)
>at java.util.regex.Pattern.newSingle(Pattern.java:2951)
>at java.util.regex.Pattern.atom(Pattern.java:1985)
>at java.util.regex.Pattern.sequence(Pattern.java:1885)
>at java.util.regex.Pattern.expr(Pattern.java:1752)
>at java.util.regex.Pattern.compile(Pattern.java:1460)
>at java.util.regex.Pattern.<init>(Pattern.java:1133)
>at java.util.regex.Pattern.compile(Pattern.java:823)
>at java.lang.String.split(String.java:2292)