[英]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 没有改编它,特别是不要使用
File
,因为这个bug与新的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; System.getProperty("java.io.tmpdir")
表示的路径符合条件; java.nio.file
at your disposal. java.nio.file
了。 Now, what the code below does is pretty simple: it tries to open a new InputStream
on this directory using Files.newInputStream()
. 现在,下面的代码非常简单:它尝试使用
Files.newInputStream()
在此目录上打开一个新的InputStream
。 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: 好的,现在当您运行该代码时,以下JRE / OS组合会发生这种情况:
IOException (is a directory)
at FAIL_READ
; FAIL_READ
IOException (is a directory)
; IOException (is a directory)
at FAIL_READ
; FAIL_READ
IOException (is a directory)
; IOException (is a directory)
at FAIL_READ
; IOException (is a directory)
在FAIL_READ
; AccessDeniedException
at FAIL_OPEN
(!!). FAIL_OPEN
(!!)中的AccessDeniedException
。 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.
我肯定会向OpenJDK公开一个关于此的错误,看起来非常严重。 I also mailed the nio-dev mailing list about this problem.
我还邮寄了关于这个问题的nio-dev邮件列表。
Well, as to a question I'd have one: what about a IsDirectoryException
in the JDK (inheriting FileSystemException
)? 好吧,至于一个问题,我有一个问题:JDK中的
IsDirectoryException
(继承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"...
我不确定为什么“Java人员”不考虑这个问题...
My observations (sorry, no other systems around here atm, later I might add ARM): 我的观察(对不起,周围没有其他系统,后来我可能会添加ARM):
java.io.IOException: Is a directory
at // FAIL_READ
. java.io.IOException: Is a directory
// FAIL_READ
java.io.IOException: Is a directory
。 I agree that this behavior is unexpected, it should not be possible to create an InputStream from a directory in the first place. 我同意这种行为是意外的,不应该首先从目录创建一个InputStream。 I suggest you file this as a bug.
我建议你把它归档为bug。 Even if
Files.newInputStream
doesn't state it explicitly, the behavior is inconsistent with the rest of the API. 即使
Files.newInputStream
没有明确说明它,该行为也与API的其余部分不一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.