java nio和stdin,stdout,stderr的流的末尾 [英] java nio and end of stream for stdin, stdout, stderr

查看:108
本文介绍了java nio和stdin,stdout,stderr的流的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序A,它通过Pipe.SourceChannel在ByteBuffer中接收另一个程序B的标准输出.

I have a program A that receives stdout of another program B in a ByteBuffer through a Pipe.SourceChannel.

程序将从ByteBuffer中读取stdout,直到到达流的末尾(也就是说,直到ByteBuffer的read()返回-1为止).

The program will read from the ByteBuffer for stdout until it reaches the end of stream (that is, until ByteBuffer's read() return -1).

程序B的stdout可以在程序的执行中多次打开和关闭吗?也就是说,read()是否有可能返回-1,但是以后的程序B具有附加的stdout,在这种情况下,程序A应该对ByteBuffer进行另一个read()?

Can stdout of program B open and close multiple times in a program's execution? That is, is it possible that read() return -1, but later program B has additional stdout, in which case program A should do another read() of the ByteBuffer?

谢谢.

推荐答案

在Linux下,您可以将NIO用于stdin,stdout和stderr,因为它们与某些字符设备相关联,可以作为文件进行访问.为此,您可以分别打开/proc/self/fd/012.并非适用于所有流类型,也不适用于非Unix系统,但这可能是一个选择.

Under Linux, you can use NIO for stdin, stdout and stderr, because they are tied to certain character devices, which can be accessed as files. To do this you can open /proc/self/fd/0, 1 or 2 respectively. This doesn't work for all stream types, or on non-Unix systems, but it may be an option.

流关闭后,除非它是一个文件并且您能够获取文件名,否则无法将其重新打开.

Once a stream has closed, it is not possible to reopen it, unless it was a file and you were able to obtain the file name.

这篇关于java nio和stdin,stdout,stderr的流的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆