Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files.newInputStream() inconsequential behaviour when the target Path is a directory?

NOTE: Please run the exact code below; no adaptations of it, in particular, do not use File, as this bug is tied to the new java.nio.file API

OK, this is not really a "question which is in need of an answer" but rather a call for witnesses...

Scenario:

  • have a directory on your OS, whatever it is, which you know you have privileges to access -- in Unix parlance, you have at least read access to it (which means you can list the entries in it); in the code below, it is supposed that the path represented by System.getProperty("java.io.tmpdir") fits the bill;
  • have a Oracle JDK, or OpenJDK, 7+ installed; so that you have java.nio.file at your disposal.

Now, what the code below does is pretty simple: it tries to open a new InputStream on this directory using Files.newInputStream(). Code (also available here; added comments mine):

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public final class Main
{
    public static void main(final String... args)
        throws IOException
    {
        final Path path = Paths.get(System.getProperty("java.io.tmpdir"));
        try (
            final InputStream in = Files.newInputStream(path); // FAIL_OPEN
        ) {
            final byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buf)) != -1) // FAIL_READ
                System.out.printf("%d bytes read\n", bytesRead);
        }
    }
}

OK, now when you run that code, this is what happens for the following JRE/OS combinations:

  • Linux x86_64, Oracle JDK 1.8.0_25: IOException (is a directory) at FAIL_READ;
  • Linux x86_64, Oracle JDK 1.7.0_72: IOException (is a directory) at FAIL_READ;
  • Mac OS X x86_64, Oracle JDK 1.8.0_25: IOException (is a directory) at FAIL_READ;
  • Windows 7, Oracle JDK 1.8.0_25: AccessDeniedException at FAIL_OPEN (!!).

Honestly, I don't know what to do with that piece of code. As I said in the introduction, I am looking for witnesses here. I will certainly open a bug to OpenJDK about this, it seems pretty serious. I also mailed the nio-dev mailing list about this problem.

Well, as to a question I'd have one: what about a IsDirectoryException in the JDK (inheriting FileSystemException)? I have actually defined it in one of my projects to account for such a problem. I am not sure why this problem was not considered by the "Java guys"...

like image 596
fge Avatar asked Dec 06 '14 02:12

fge


1 Answers

My observations (sorry, no other systems around here atm, later I might add ARM):

  • JDK 1.8.0_25, Linux x86_64: java.io.IOException: Is a directory at // FAIL_READ.

I agree that this behavior is unexpected, it should not be possible to create an InputStream from a directory in the first place. I suggest you file this as a bug. Even if Files.newInputStream doesn't state it explicitly, the behavior is inconsistent with the rest of the API.

like image 109
Christian Hujer Avatar answered Nov 10 '22 10:11

Christian Hujer