The method java.io.RandomAccessFile.setLength is incomplete about how it affects the file offset as returned by getFilePointer(). For example, there is no mention of the case where the desired length of the file is greater than the present length and the file offset is greater than both of those lengths, i.e.,
length() < newLength < getFilePointer()
What actually happens in this case is that the file is extended to size newLength and the file position is clamped to newLength. After the method returns, getFilePointer() will return newLength.
Neither is there mention of what happens when the present and desired lengths are equal. In this case, the file itself and its length are unaffected, but if the file offset is greater than newLength, then it will be clamped to newLength, so that after the call getFilePointer() will return newLength.
length() < newLength < getFilePointer()
What actually happens in this case is that the file is extended to size newLength and the file position is clamped to newLength. After the method returns, getFilePointer() will return newLength.
Neither is there mention of what happens when the present and desired lengths are equal. In this case, the file itself and its length are unaffected, but if the file offset is greater than newLength, then it will be clamped to newLength, so that after the call getFilePointer() will return newLength.
- csr for
-
JDK-8325260 Clarify specification of java.io.RandomAccessFile.setLength
- Closed