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:
System.getProperty("java.io.tmpdir")
fits the bill;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:
IOException (is a directory)
at FAIL_READ
;IOException (is a directory)
at FAIL_READ
;IOException (is a directory)
at FAIL_READ
;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"...
My observations (sorry, no other systems around here atm, later I might add ARM):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With