简体   繁体   English

当目标路径是目录时,Files.newInputStream()无关紧要的行为?

[英]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: 场景:

  • 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); 你的操作系统上有一个目录,无论它是什么,你知道你有权访问它 - 用Unix的说法,你至少有读取权限(这意味着你可以在其中列出条目); in the code below, it is supposed that the path represented by System.getProperty("java.io.tmpdir") fits the bill; 在下面的代码中,假设System.getProperty("java.io.tmpdir")表示的路径符合条件;
  • have a Oracle JDK, or OpenJDK, 7+ installed ; 安装了Oracle JDK或OpenJDK,7+ ; so that you have 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组合会发生这种情况:

  • Linux x86_64, Oracle JDK 1.8.0_25: IOException (is a directory) at FAIL_READ ; Linux x86_64,Oracle JDK 1.8.0_25: FAIL_READ IOException (is a directory) ;
  • Linux x86_64, Oracle JDK 1.7.0_72: IOException (is a directory) at FAIL_READ ; Linux x86_64,Oracle JDK 1.7.0_72: FAIL_READ IOException (is a directory) ;
  • Mac OS X x86_64, Oracle JDK 1.8.0_25: IOException (is a directory) at FAIL_READ ; 的Mac OS X x86_64的,甲骨文JDK 1.8.0_25: IOException (is a directory)FAIL_READ ;
  • Windows 7, Oracle JDK 1.8.0_25: AccessDeniedException at FAIL_OPEN (!!). Windows 7,Oracle JDK 1.8.0_25: 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):

  • JDK 1.8.0_25, Linux x86_64: java.io.IOException: Is a directory at // FAIL_READ . JDK 1.8.0_25,Linux x86_64: 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.

相关问题 Files.newInputStream 创建缓慢的 InputStream - Files.newInputStream creates slow InputStream 带有StandardOpenOption.CREATE的Files.newInputStream中的NoSuchFileException - NoSuchFileException in Files.newInputStream with StandardOpenOption.CREATE 包含“点目录”时的path.relativize行为 - Path.relativize behaviour when “dot directory” is included 更改ecplise目标目录以获取相对路径解析 - Changing ecplise target directory for relative path resolutions 在 Maven (Java) 中获取目标目录路径 - Get the target directory path in Maven (Java) 仅知道目录的相对路径时如何读取目录中的文件 - How to read the files inside a directory when Only Relative path of directory is known java.nio.file.Files.newInputStream(myfile.toPath())比新的FileInputStream(文件)更好吗? - Is java.nio.file.Files.newInputStream(myfile.toPath()) better than new FileInputStream(file)? 找不到方法 java.nio.file.Files.newInputStream,从方法 okio.Okio.source 引用 - Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source 我应该关闭用java.nio.file.Files.newInputStream创建的Streams吗? - Should I close Streams created with java.nio.file.Files.newInputStream? vscode在目标路径中没有类文件但有包路径,为什么? - vscode no class files in target path but have package path, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM