1

I am trying to read continuously from a named pipe using java. This question answers it for 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 ?
  3. Is EOF is sent by pipe only when it terminate and not otherwise ?
  4. Does pipe guarantees to be alive and working unless something really goes wrong ?

Environment is CentOS 6.7 with Java 7

This is tested and works fine but corner cases needs to be handled so that continuous operation is ensured.

Albatross
  • 669
  • 7
  • 24

2 Answers2

4
  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.

  1. Can Reader object be reused when EOF is encountered ?

I wouldn't count on it. It would be safer to close the Reader and create a new one in that case.

  1. Is EOF is sent by pipe only when it terminate and not otherwise ?

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. I am uncertain whether Windows named pipe semantics differ in this regard, but since you're on Linux, that doesn't matter to you.

  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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

What happens if there is no data from pipe for some time?

Nothing. It blocks.

Can Reader object be reused when EOF is encountered?

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?

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Actually I was able to connect the dots. But only concern that I have is missing events. There are around multiple writers 150 writes and only single pipe. I am assuming pipe buffer to be 65536 bytes. Reader is running when the data is being sent. But I do not get all the data. – Albatross Jul 27 '17 at 20:00