简体   繁体   English

使用Java从命名管道连续读取

[英]Read continuously from a named pipe using Java

I am trying to read continuously from a named pipe using java. 我正在尝试使用Java从命名管道连续读取。 This question answers it for python/bash. 这个问题回答了python / bash。

public class PipeProducer {
    private BufferedReader pipeReader;

public PipeProducer(String namedPipe) throws IOException {
    this.pipeReader = new BufferedReader(new FileReader(new File(namedPipe)));
} 

public void process() {
    while ((msg = this.pipeReader.readLine()) != null) {
          //Process
    }
}

public static void main(String args[]) throws JSONException,IOException {
       PipeProducer p = new PipeProducer("/tmp/testpipe");
       while(true) {
            p.process();              
            System.out.println("Encountered EOF");
            now = new Date();
            System.out.println("End : " + now);
        }       
}       

Questions 问题

  1. What happens if there is no data from pipe for some time ? 如果一段时间内没有管道的数据怎么办?
  2. Can Reader object be reused when EOF is encountered ? 遇到EOF时可以重用Reader对象吗?
  3. Is EOF is sent by pipe only when it terminate and not otherwise ? EOF是仅在终止时才通过管道发送的吗?
  4. Does pipe guarantees to be alive and working unless something really goes wrong ? 除非确实出现问题,否则管道是否可以保证存在并正常工作?

Environment is CentOS 6.7 with Java 7 环境是带有Java 7的 CentOS 6.7

This is tested and works fine but corner cases needs to be handled so that continuous operation is ensured. 这已经过测试并且可以正常工作,但是需要处理极端情况,以确保连续运行。

  1. What happens if there is no data from pipe for some time ? 如果一段时间内没有管道的数据怎么办?

The program blocks until there is data to read or until EOF is detected, just like a Reader connected to any other kind of file. 该程序将阻塞,直到有要读取的数据或直到检测到EOF为止,就像连接到任何其他类型文件的Reader一样。

  1. Can Reader object be reused when EOF is encountered ? 遇到EOF时可以重用Reader对象吗?

I wouldn't count on it. 我不会指望它。 It would be safer to close the Reader and create a new one in that case. 在这种情况下,关闭Reader并创建一个新的Reader会更安全。

  1. Is EOF is sent by pipe only when it terminate and not otherwise ? EOF是仅在终止时才通过管道发送的吗?

On Unix, EOF will be received from the pipe after it goes from having one writer to having zero, when no more data are available from it. 在Unix上,当没有可用数据时,从一个写入器变为零后,将从管道中接收EOF。 I am uncertain whether Windows named pipe semantics differ in this regard, but since you're on Linux, that doesn't matter to you. 我不确定Windows命名管道的语义在这方面是否有所不同,但是由于您使用的是Linux,所以这对您而言并不重要。

  1. Does pipe guarantees to be alive and working unless something really goes wrong ? 除非确实出现问题,否则管道是否可以保证存在并正常工作?

If the named pipe in fact exists on the file system and you have sufficient permission, then you should reliably be able to open it for reading, but that may block until there is at least one writer. 如果命名管道实际上存在于文件系统上,并且您具有足够的权限,那么您应该可以可靠地打开它以进行读取,但这可能会阻塞,直到至少有一个写入器为止。 Other than that, I'm not sure what you mean. 除此之外,我不确定您的意思。

What happens if there is no data from pipe for some time? 如果一段时间内没有管道中的数据怎么办?

Nothing. 没有。 It blocks. 它阻止了。

Can Reader object be reused when EOF is encountered? 遇到EOF时可以重用Reader对象吗?

Reused for what? 重用了什么? It's got to the end of the data. 到数据末尾了。 The question does not arise. 不会出现这个问题。

Is EOF is sent by pipe only when it terminate and not otherwise? EOF是仅在终止时才通过管道发送的吗?

It is sent when the peer closes its end of the pipe. 当对等方关闭管道末端时发送。

Does pipe guarantees to be alive and working unless something really goes wrong? 除非确实出现问题,否则管道是否可以保证存在并正常工作?

Nothing is guaranteed, in pipes or in life, but in the absence of an error you should continue to read any data that is sent. 在管道或生命中,没有任何保证,但是在没有错误的情况下,您应该继续读取所发送的任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM