Thursday, January 8, 2009

Buffered java.io.RandomAccessFile

Now for some total randomness. I'm posting this because I know someday, someone, somewhere out there, will come across this exact same problem and will google for it. The solutions I found via google were meh, and I know I would've liked to have this. So I post this for the children of the future (unless Sun gets its act together).

2)Most read type methods in RAF end up calling read(), so read() is the only method that really needs to be overridden. The exceptions are the read(byte b[]), read(byte b[], int off, int len), readFully(byte b[]) and readFully(byte b[], int off, int len) methods. They end up calling a private readBytes method. It's probably not a big deal, since in those methods you're asking for a byte array anyway, but it will throw off the buffpos file pointer!

3) If you're calling lots of readLine(), the readLine() in the original RAF sucks. It constantly creates a new StringBuffer object which is unnecessary and can really slow things down. In my class, I reuse a StringBuilder and just delete the contents. Unfortunately, for some dumbass reason, readLine() is final, so my method is readLine2(). From a simple test of reading through a file using readline, I find the performance is nearly the same as using BufferedReader.

4) people on the Sun Forums are total fucking douchebags. Don't ever go there looking for help.

I created sample application that logs text data at rate of 1 line per 10 ms (i line has 300 characters) and need to monitor it in real time. I used your class and found it is taking 50% of CPU. Then I used RandomAccessFile.readFully using buffer size of 4096 and found it is taking 3-4% CPU. Its huge difference! Is there anyway you can optimize your class? Thanks