I am using DataInputStream to read from this file, and available() to determine whether or not the next read will be successful.
Then you're doing it wrong. See the Javadoc: "Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.". Many implementations returns zero, and the ones that don't return zero don't guarantee to return a positive number: they can't, when the number concerned exceeds Integer.MAX_VALUE.
Also, the size of the file could change between available()
and read().
You should detect end of stream by catching EOFException
on the methods that throw it, or -1 or null
returned by the methods that don't (i.e. read()
, all overloads, and readLine()
respectively).