java - Files.newInputStream() inconsequential behaviour when the target Path is a directory? -
note: please run exact code below; no adaptations of it, in particular, not use file
, bug tied new java.nio.file
api
ok, not "question in need of answer" rather call witnesses...
scenario:
- have directory on os, whatever is, know have privileges access -- in unix parlance, have @ least read access (which means can list entries in it); in code below, supposed path represented
system.getproperty("java.io.tmpdir")
fits bill; - have oracle jdk, or openjdk, 7+ installed; have
java.nio.file
@ disposal.
now, code below pretty simple: tries open new inputstream
on 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, when run code, happens following jre/os combinations:
- linux x86_64, oracle jdk 1.8.0_25:
ioexception (is directory)
@fail_read
; - linux x86_64, oracle jdk 1.7.0_72:
ioexception (is directory)
@fail_read
; - mac os x x86_64, oracle jdk 1.8.0_25:
ioexception (is directory)
@fail_read
; - windows 7, oracle jdk 1.8.0_25:
accessdeniedexception
@fail_open
(!!).
honestly, don't know piece of code. said in introduction, looking witnesses here. open bug openjdk this, seems pretty serious. mailed nio-dev mailing list problem.
well, question i'd have one: isdirectoryexception
in jdk (inheriting filesystemexception
)? have defined in one of projects account such problem. not sure why problem not considered "java guys"...
my observations (sorry, no other systems around here atm, later might add arm):
- jdk 1.8.0_25, linux x86_64:
java.io.ioexception: directory
@// fail_read
.
i agree behavior unexpected, should not possible create inputstream directory in first place. i suggest file bug. if files.newinputstream
doesn't state explicitly, behavior inconsistent rest of api.
Comments
Post a Comment