JDK-8021977 : Opening a file using java.io can throw IOException on Windows
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows
  • Submitted: 2013-07-31
  • Updated: 2013-09-09
  • Resolved: 2013-08-09
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.
8 b105Fixed
FileInputStream.java, FileOutputStream.java, and RandomAccessFile.java are all mapping their native open() method to fileOpen function defined in src/windows/native/java/io/io_util_md.c. In the current version, fileOpen function calls winFileHandleOpen function to do the work. But inside the error handling part of winFileHandleOpen function, it can throw out not only FileNotFoundException but also IOException as shown in the following code snippet.

    253     if (h == INVALID_HANDLE_VALUE) {
    254         int error = GetLastError();
    255         if (error == ERROR_TOO_MANY_OPEN_FILES) {
    256             JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException",
    257                             "Too many open files");
    258             return -1;
    259         }
    260         throwFileNotFoundException(env, path);
    261         return -1;
    262     }

On other platforms, fileOpen function defined in src/solaris/native/java/io/io_util_md.c only throws out FileNotFoundException.

But in the corresponding java parts, all related methods are only defined to "throws FileNotFoundException", which is incorrect given what we have for the Windows platform. Basically, I think these java methods should be defined to "throws IOException" instead of FileNotFoundException unless we change the above error-handling codes on windows.

Here is the list of related java methods,

RandomAccessFile.open(String name, int mode)
RandomAccessFile(String name, String mode)
RandomAccessFile(File file, String mode)

FileOutputStream.open(String name, boolean append)
FileOutputStream(String name)
FileOutputStream(String name, boolean append)
FileOutputStream(File file)
FileOutputStream(File file, boolean append)

FileInputStream.open(String name)
FileInputStream(String name)
FileInputStream(File file)

Other java classes, which use the above methods, might also need to be scanned and changed.
After checking the history of the code, the code to throw IOException was added in jdk1.4. It seems that the people changed the native code did not realize the impact on the java side.