java.io.File#toPath() on Windows doesn't understand "NUL:" (null device)?
David Holmes
david.holmes at oracle.com
Wed Mar 17 07:13:53 UTC 2021
https://bugs.openjdk.java.net/browse/JDK-8022671
Cheers,
David
On 17/03/2021 1:21 pm, Jaikiran Pai wrote:
> Please consider this trivial 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 InputStream fis = Files.newInputStream(f.toPath())) {
> int numBytes = 0;
> while (fis.read() != -1) {
> System.out.println("Files.newInputStream - Read a
> byte from " + f);
> numBytes++;
> }
> System.out.println("Files.newInputStream - Done reading
> " + numBytes + " bytes from " + f);
> }
> }
> }
>
>
> The code tries to read from NUL: on a Windows setup. This code runs into
> the following exception on Windows when the java.io.File#toPath() gets
> invoked:
>
>
> Exception in thread "main" java.nio.file.InvalidPathException: Illegal
> char <:> at index 3: NUL:
> at
> java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
>
> at
> java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
> at
> java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
> at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
> at
> java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:230)
> at java.base/java.io.File.toPath(File.java:2316)
> at FileTest.main(FileTest.java:18)
>
>
> So it looks like java.io.File.toPath() on Windows isn't able to
> recognize the null device construct?
>
> Just to make sure this issue resides only this specific API
> implementation, I switched the above code to use new FileInputStream(f)
> as follows and that works as expected - reads 0 bytes and completes
> successfully. So the NUL: construct is indeed understood by the other APIs.
>
>
> 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 FileInputStream fis = new FileInputStream(f)) {
> int numBytes = 0;
> while (fis.read() != -1) {
> System.out.println("FileInputStream() - Read a byte
> from " + f);
> numBytes++;
> }
> System.out.println("FileInputStream() - Done reading "
> + numBytes + " bytes from " + f);
> }
> }
> }
>
> Output:
>
> FileInputStream() - Done reading 0 bytes from NUL:
>
> Test results are from latest Java 16 release on a Windows setup.
>
> -Jaikiran
>
More information about the nio-dev
mailing list