• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with EOFException

 
Bill Rowell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on UrlyBird 1.3.1 and continue to run into problems with EOFException when reading the last record in the file. The original file length is 5034 bytes. I add a record at the end of the file (160 bytes long) making it 5194 bytes long and try to read it using the code below:

byte[] input = new byte[MetaData.getRecordLength()];
long filePointer = database.getFilePointer();
long dbLength = database.length();
database.readFully(input);

database is a RandomAccessFile. When the readFully attempts to read the last record, the length of the input array is 160 (the delete flag is considered part of the record), the filePointer is 5035 bytes, and dbLength is 5194 bytes. Thus it seems as if there should be no EOFException because 5194-5035+1 = 160. The first record is record 0. I check whether
MetaData.getFileHeaderLength() + (recNo + 1) * MetaData.getRecordLength() > database.length() before performing the readFully().

Is it possible that the file pointer is going one byte beyond the end of the file after the readFully and is causing an EOFException?

Does anyone have any idea what might be going on?
It seems as if I added an extra byte at the end of the file, everything would be OK.

Thanks for your help!
Bill
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you're setting the file pointer to the correct position.
If you're setting it one byte too far you would get the situation you're encountering.

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeroen is right (gee, I seem to be saying this a lot lately ). It looks like you are starting your read from the wrong location.

Originally posted by Bill Rowell:
... The original file length is 5034 bytes. ... When the readFully attempts to read the last record ... the filePointer is 5035 bytes
If your original file length is 5034, then when you are about to read the next record, your file pointer should be 5034.

Sometimes simple examples will show what should be. So, here is an example:You will find that if you run this, then prior to reading the first byte, the file pointer is set to 0. With that file offset in the correct location, it is safe to read the record length (in my case, the entire file).

Regards, Andrew
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps a fuller example, showing the results of adding a record then going back to the point where I can read it:Regards, Andrew
 
Bill Rowell
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone
Thanks for your advice!
I just discovered that before I had read the 160-byte record with readFully that I had inadvertently advanced the file pointer by one byte by using the readByte method to read the delete flag. Once I simply read the record into the array and then checked the delete flag within the array (the first element in the array) the EOFExceptions disappeared.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic