JDK-8022741 : Improve RandomAccessFile.length() to handle special files or files opened with special options
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6-pool,7-pool,8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2013-08-09
  • Updated: 2019-03-11
  • Resolved: 2019-03-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdResolved
Related Reports
Duplicate :  
Relates :  
Description
In current implementation of RamdomAccessFile.java, it seeks to the end of a file to get the file size, which brings different problems to windows and *nix platforms.

On windows:
When using FILE_FLAG_NO_BUFFERING to open a file, you cannot, in general, get the file size by doing a zero seek relative to FILE_END. The end of the file may not coincide with a sector boundary and trying to seek to a non-aligned position (even if you are just trying to find out what that position is) is illegal and will fail. The workaround is to use GetFileSizeEx instead.

On *nix:
It will throw IOException when checking /proc files. This is the same issue reported in jdk-7132461.

        RandomAccessFile raf = new RandomAccessFile("/proc/cpuinfo", "r");
        System.out.println("RF:" + raf.length());

Comments
This issue was fixed via JDK-4823133 Here's the verification (with the reproducer snippet provided in the description) to confirm this is not an issue, starting with JDK 9: igerasim@ivirt16:~/work/8022741$ ~/java6/jdk/bin/java T Exception in thread "main" java.io.IOException: Invalid argument at java.io.RandomAccessFile.length(Native Method) at T.main(T.java:7) igerasim@ivirt16:~/work/8022741$ ~/java7/jdk/bin/java T Exception in thread "main" java.io.IOException: Invalid argument at java.io.RandomAccessFile.length(Native Method) at T.main(T.java:7) igerasim@ivirt16:~/work/8022741$ ~/java8/jdk/bin/java T Exception in thread "main" java.io.IOException: Invalid argument at java.io.RandomAccessFile.length(Native Method) at T.main(T.java:7) igerasim@ivirt16:~/work/8022741$ ~/java9/jdk/bin/java T RF:0 igerasim@ivirt16:~/work/8022741$ ~/java10/jdk/bin/java T RF:0 igerasim@ivirt16:~/work/8022741$ ~/java11/jdk/bin/java T RF:0 igerasim@ivirt16:~/work/8022741$ ~/java12/jdk/bin/java T RF:0 igerasim@ivirt16:~/work/8022741$ ~/java13/jdk/bin/java T RF:0
11-03-2019