Bruno Boettcher wrote:
> 
> hello,
> 
> i am in the process of porting a C++ program to java....
> 
> this program uses at some point unix-fifos....
> is there a way to open and write/read from unix fifos out of java?
> i looked at the PipeReader etc. but this seem to be a java-intern mechanism....

You bet. Here's bits of a spike I wrote a few months ago.  It fires
off a child process and sends commands to it, reading the results.
The child process is a commandline shell for managing a firewall
product we sell.

One thing to keep in mind is that when unix sees a pipe, it
automatically turns on IO buffering.  So, you can send a command
to your process, and your process sends it results, but the shell
buffers the results until there are 512 bytes of it to view...
So make sure your child turns off buffering specifically.
I had to make a custom verson of our command line to disable the
buffering.

(I wrote this very early in my java learning curve, so please be 
gentle...  I'll be rewriting this before putting it into my real
application.  This was just my proof of concept.)

  public Communicator(String newIpAddress) throws java.io.IOException {
    ipAddress = newIpAddress;


    available = 0;  // number of lines ready to read
    ready = false;
    child = Runtime.getRuntime().exec( displayCommandBinary );
    input = new InputStreamReader(child.getInputStream());
    error = new InputStreamReader(child.getErrorStream());
    output = new PrintWriter(child.getOutputStream());
// read the initial prompts and copyrights


    while (input.ready() || ! done) {
      charsRead = input.read(cbuff);
      line.append(cbuff,0,charsRead);
      if (cbuff[charsRead - 1] == '>') {
        done = true;
      }
    }
    temp = line.toString();
    System.out.println("Initial Read:\n" + temp);
    line.setLength(0);

    // send the connect command
    System.out.println("Sending: connect " + ipAddress);
    output.println("connect " + ipAddress);
    output.flush();

//  read the connected message and the next prompt.
    done = false;
    while (input.ready() || ! done) {
      charsRead = input.read(cbuff);
//      System.out.println("Read " + charsRead + ": " + cbuff + '\n');
      line.append(cbuff,0,charsRead);
      if (cbuff[charsRead - 1] == '>') {
        done = true;
      }
    }
    System.out.println("Initial Read:\n" + line.toString());

    System.out.println("Connected, I think.");
    line.setLength(0);
  }

  public void sendCommand( String command ) throws java.io.IOException {
     System.out.println("sendComand sent " + command);

     output.println(command);
     output.flush();
  }
  public boolean ready() throws java.io.IOException {
    ready = input.ready();
    System.out.print("Ready:  ");
    System.out.println(ready);
    return ready;
  }
  public void closeConnection( ) throws java.io.IOException {
    System.out.println("Closing the connection.");
    output.println("QUIT");
    output.flush();
    System.out.println("closeConnection: " + this.getOutput());
    input.close();
    output.close();
    error.close();
  }
  public String getOutput( ) throws java.io.IOException {
    line.setLength(0);
    done = false;
    while ( input.ready() || ! done) {
      charsRead = input.read(cbuff);
      if (charsRead == -1) {
        System.out.println("EOF from child");
        done = true;
      } else {
        line.append(cbuff,0,charsRead);
        if ( cbuff[charsRead - 1] == '>' ) {
          done = true;
        }
      }
    }
    String temp = line.toString();
    // chop off the last line, which is the command prompt and which does not
    // end with a newline.  Include the last found newline in the returned
    // results.
    int i = temp.lastIndexOf("\n");
    temp = temp.substring(0,i+1);
    line.setLength(0);
    return temp;
  }


--
Joi Ellis                    Software Engineer
Aravox Technologies          [EMAIL PROTECTED], [EMAIL PROTECTED]

No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried.  Anything
that kicks a monopoly in the pants has got to be good for something.
           - Chris Johnson


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to