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)
atFAIL_READ
; - Linux x86_64, Oracle JDK 1.7.0_72:
IOException (is a directory)
atFAIL_READ
; - Mac OS X x86_64, Oracle JDK 1.8.0_25:
IOException (is a directory)
atFAIL_READ
; - Windows 7, Oracle JDK 1.8.0_25:
AccessDeniedException
atFAIL_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"...
FileInputStream
directly seems (I've only tried on mac) to catch the fact that it is a directory right away. – Kylfinal InputStream in = new FileInputStream(path.toFile())
producesException in thread "main" java.io.FileNotFoundException: /var/folders/7z/ndxp48z14636frp1_rrr8x_8002l2v/T (Is a directory)
. Sorry for code in comments. – KylFile
, please.File
has its own problems – ExtenuatoryFileInputStream
would seemingly detect the directory problem earlier. – KylFileInputStream
;) I want to know the results specifically with classes ofjava.nio.file
. I have droppedFile
usage for a while now – Extenuatory