从 getInputStream 正确关闭 Java Process InputStream

发布于 2024-11-29 20:47:10 字数 213 浏览 1 评论 0原文

我在文档中找不到对此的说明。 但是,当我们有一个 Process 对象并调用 getInputStream() 时,

我们是否会得到一个新的流,当我们完成它时,我们应该显式关闭它吗? 或者 我们是否得到了已经存在的与进程关联的流,我们不应该关闭它,但进程会负责关闭它?

基本上,我们应该如何与从 Process.getInputStream() 获取的流进行交互?关闭还是不关闭?

I could not find clarification of this in the documentation.
But when we have a Process object and call getInputStream(),

Do we get a new stream that we should explicitly close when we are done with it?
or
do we get the stream that is already there, associated with the Process, that we should not close, but the Process would take care of closing it?

Basically, how should we interact with the stream we get from Process.getInputStream()? close or not to close?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橘和柠 2024-12-06 20:47:10

通过阅读 UNIXProcess.java,会发生这样的情况:

我们需要区分两种状态:进程仍然存活,或者已死亡。

如果进程处于活动状态,则通过关闭 OutputStream(转到进程的标准输入),您可以告诉进程不再有输入。通过关闭InputStreams(进程的stdout、stderr),进程将不再写入这些内容(如果尝试,它将收到SIGPIPE)。

当进程死亡时,Java 将缓冲来自 stdout/stderr 的剩余数据,并为您关闭所有三个流(它正在运行“进程收割者”线程,该线程会在进程死亡时收到通知)。任何写入 OutputStream 的尝试都会失败。从 InputStream 读取将返回缓冲数据(如果有)。关闭其中任何一个都没有任何好处,但也不会造成任何损害。 (此时底层文件描述符已关闭)。

From reading UNIXProcess.java, this is what happens:

We need to distinguish between two states: either process is still alive, or it is dead.

If the process is alive, by closing OutputStream (goes to stdin of the process), you are telling the process that there is no more input for it. By closing InputStreams (stdout, stderr of the process), process is no longer to write to these (it will get SIGPIPE if it tries).

When process dies, Java will buffer remaining data from stdout/stderr, and close all three streams for you (it is running "process reaper" thread, which is notified on process death). Any attempt to write to OutputStream will fail. Reading from InputStream will return buffered data, if any. Closing any of them has no benefit, but also causes no harm. (Underlying file descriptors are closed by this time).

小帐篷 2024-12-06 20:47:10

我的第一反应是关闭它,你总是关闭你打开的流。我确实意识到文档没有达到标准,但由于它们没有明确声明不要关闭,这对我来说意味着遵循良好的编程实践。

InputStream is = process.getInputStream()
try {
    // your code
} finally {
    try { is.close(); } catch (Exception ignore) {}
}

如果您需要确保这没有问题,只需编写一个快速测试用例,在其中从输入流中进行数十次,每次打开和关闭 InputStream。

My first reactions was to close it, you always close streams that you open. I do realize the documentation is not up to par, but since they don't explicitly state do not close that to me means follow good programming practices.

InputStream is = process.getInputStream()
try {
    // your code
} finally {
    try { is.close(); } catch (Exception ignore) {}
}

If you need to make sure this isn't problematic, just write a quick test case where you great from the input stream a few dozen times, each time opening and closing the InputStream.

烟若柳尘 2024-12-06 20:47:10

当您调用 Process.getInputStream() 时,您将获得为流程设置的现有输入流。当进程终止时,该输入流不会自动消失 - 将其视为仍然可以读取的缓冲区。管道的进程一端可能是封闭的,但你的一端却没有。尽管 GC 最终会得到它,但您有责任关闭它。

您还应该关闭其他两个:getErrorStream()getOutputStream()

When you call Process.getInputStream() you get an existing input stream that was set up for the process. When the process dies, that input stream does not go away automatically - think of it as a buffer that you can still read from. The process's end of the pipe might be closed, but your end is not. It is your responsibility to close it, though GC will eventually get it.

You should also close the other two: getErrorStream() and getOutputStream().

隔岸观火 2024-12-06 20:47:10

你没有关闭你没有打开的流——这是一个令人讨厌的副作用。
如果您创建了该进程,请先终止它,然后关闭流。

You do not close streams, that you did not open - that's a nasty side effect.
If you created the process, kill it first and close streams after that.

微暖i 2024-12-06 20:47:10

我总是关闭它们!我不是 100% 确定,但据我所知,如果您保持输入流打开,该文件将一直打开,直到您关闭它!所以遵循“标准规则”并关闭它!举个例子:
进程生成器 waitFor() 问题和打开文件限制

I always close them! I am not 100% sure, but as far as I know if you leave the inputstream open, the file will be open until you close it!! So follow the "standard rules" and close it! follow an example:
Process Builder waitFor() issue and Open file limitations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文