I am trying to check whether a certain position in RandomAcceessFile is empty in order to avoid overwriting some data before writing in the file. I initialized a very large RandomAccessFile:
randAccFile = new RandomAccessFile( DIR + "/" + FILENAME, "rw" );
randAccFile.setLength(LARGE_SIZE);
Then I have this method to check whether a given position is Empty, let's say pointer is set to 20:
randAccFile.seek(pointer);
byte[] data = new byte[BLOCK_SIZE];
randAccFile.readFully(data);
Afterwards, I am trying to check emptiness by trimming the data and seeing whether the length of resulting string is 0. Before I get to that point readFully() throws
java.io.EOFException
at java.base/java.io.RandomAccessFile.readFully(RandomAccessFile.java:474)
at java.base/java.io.RandomAccessFile.readFully(RandomAccessFile.java:448)
I wonder if the RandomAccessFile was not initialized in the right way, or if I am not allowed to readFully from part of it that wasn't filled up yet. I also wonder if there is a better way of checking whether the RandomAccessFile is empty certain position. It is surprising that I get EOF exception since the length of the file is much larger than the offset at which I am trying to read + the block size. In my program it might happen that there will be parts that remain empty forever and that there will be chunks of data written after it since I am not writing and reading from it sequentially.
Any suggestions welcome.