Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8263898

(fs) Files.newOutputStream on the "NUL" special device throws FileSystemException: "nul: Incorrect function" (win)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8, 11, 16
    • Fix Version/s: 17
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      b16
    • OS:
      windows

      Description


      Consider the following trivial Java code:

      import java.io.*;
      import java.nio.file.*;

      public class FileTest {
      public static void main(final String[] args) throws Exception {
      System.getProperties().list(System.out);
      final File f = new File("nul");
      try (final OutputStream os = Files.newOutputStream(f.toPath())) {
      os.write(0x02);
      System.out.println("Files.newOutputStream(Path) - wrote a byte to " + f);
      }
      }
      }

      Compiling and runnning this code on Windows OS throws the following exception:

      Exception in thread "main" java.nio.file.FileSystemException: nul: Incorrect function
      at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
      at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
      at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
      at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:236)
      at java.base/java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:478)
      at java.base/java.nio.file.Files.newOutputStream(Files.java:224)
      at FileTest.main(FileTest.java:9)


      The usage of Files.newOutputStream(f.toPath()) for a file which represents null device on Windows throws this exception.


      Replacing Files.newOutputStream(f.toPath()) with either of the following works fine and the output stream is opened and data is written out without errors:

      new FileOutputStream(f)
      Files.newOutputStream(f.toPath(), StandardOpenOption.WRITE)
      Files.newOutputStream(f.toPath(), StandardOpenOption.APPEND)
      Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE)

      The javadoc of Files.newOutputStream(Path) states[1] that:

      "If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present".

      So it looks like the (default usage) of StandardOpenOption.TRUNCATE_EXISTING is what triggers this exception. In fact, replacing Files.newOutputStream(f.toPath()) with Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE) does indeed reproduce the same exception.

      This is reproducible with Java 8, 11 and even latest released Java 16. I was able to reproduce this on Windows Server 2016, but others have reproduced this on even Windows 10 (Professional 20H2).

      Please note that this only happens on Windows OS against the null device. Using "/dev/null" on Linux in place of "nul" in the above program doesn't cause any issues and the program successfully opens the output stream and writes out the data.

      [1] https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/file/Files.html#newOutputStream(java.nio.file.Path,java.nio.file.OpenOption...)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              bpb Brian Burkhalter
              Reporter:
              jpai Jaikiran Pai
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: