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

1

9

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"...

Extenuatory answered 6/12, 2014 at 2:51 Comment(6)
Creating a new FileInputStream directly seems (I've only tried on mac) to catch the fact that it is a directory right away.Kyl
@SotiriosDelimanolis this seems to contradict the other testimony I have on Mac OS X; care to share the exact source and stack trace (in particular, the line where it says that the path "is a directory")?Extenuatory
Just final InputStream in = new FileInputStream(path.toFile()) produces Exception in thread "main" java.io.FileNotFoundException: /var/folders/7z/ndxp48z14636frp1_rrr8x_8002l2v/T (Is a directory) . Sorry for code in comments.Kyl
@SotiriosDelimanolis this is not the code that I run at all! Please use the exact code; no File, please. File has its own problemsExtenuatory
No, I get the same results as you when I run your code. I'm just saying using FileInputStream would seemingly detect the directory problem earlier.Kyl
@SotiriosDelimanolis I don't care about FileInputStream ;) I want to know the results specifically with classes of java.nio.file. I have dropped File usage for a while nowExtenuatory
B
1

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.

Bridgers answered 20/12, 2014 at 13:19 Comment(1)
"I suggest you file this as a bug" <-- well, I'm on the nio-dev OpenJDK mailing list and reported this there; there's a patchset which is ready to go but I don't know when it will make it however.Extenuatory

© 2022 - 2024 — McMap. All rights reserved.