This discussion is archived
1 Reply Latest reply: Feb 27, 2012 3:25 PM by EJP RSS

Unexpected output with PipedInputStream/PipedOutputStream

920329 Newbie
Currently Being Moderated
I am using a pair of connected PipedStream (StdinREnd, StdinWEnd) and (StdoutREnd, StdoutWEnd). StdinREnd is plugged to Channel's input stream and StdoutWEnd is plugged to channel's outputStream.

When I write a command ( for example echo "abc" ) to StdinWEnd, command gets executed on remote server. However when I read output of this command from StdoutREnd, then I get following output

echo abc
abc

I am expected only output of command abc in above example. Why I get command at the beginning? Physically my pair of connected pipes are independent.


          rhost.channel = rhost.session.openChannel("shell");

          rhost.stdinWEnd = new PipedOutputStream();
          rhost.stdinREnd = new PipedInputStream(rhost.stdinWEnd);

          rhost.stdoutWEnd = new PipedOutputStream();
          rhost.stdoutREnd = new PipedInputStream(rhost.stdoutWEnd);

          rhost.channel.setOutputStream(rhost.stdoutWEnd);
          rhost.channel.setInputStream(rhost.stdinREnd);
          rhost.channel.connect(3 * 1000);

rhost.stdin = new PrintWriter(new OutputStreamWriter(rhost.stdinWEnd,"utf-8"));

......

          // * Check shell channel
          if (channel.isClosed()) {
               throw new Exception("Shell channel is not in opened-state");
          }

          stdin.println(Command);
          ///////stdin.print(Command+"\n");
          stdin.flush();
          Thread.sleep(1000);
......
strbuf.setLength(0);
          int Attempts = 5;
          int bytes = 0;

          byte[] blk = new byte[4096];
          strbuf.setLength(0);

          while (Attempts > 0) {
               while (in.available() > 0) {
                    bytes = in.read(blk, 0, blk.length);
                    if (bytes < 0)
                         break;
                    strbuf.append(new String(blk, 0, bytes));
                    try {
                         Thread.sleep(1000);
                    } catch (Exception e) {
                    }
               }
               Attempts--;
          }
  • 1. Re: Unexpected output with PipedInputStream/PipedOutputStream
    EJP Guru
    Currently Being Moderated
    This has nothing do with PipedInputStream/PipedOutputStream or Java. The remote system is echoing the input and then producing the result. You might be able to find an option to disable local echo somewhere.

    BTW Channel.isClosed() only tells you whether you have closed the channel, and I don't see why you are using pipes here, there is no multithreading in evidence.