1 Reply Latest reply on Feb 27, 2012 11:25 PM by EJP

    Unexpected output with PipedInputStream/PipedOutputStream

      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

      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.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");

                int Attempts = 5;
                int bytes = 0;

                byte[] blk = new byte[4096];

                while (Attempts > 0) {
                     while (in.available() > 0) {
                          bytes = in.read(blk, 0, blk.length);
                          if (bytes < 0)
                          strbuf.append(new String(blk, 0, bytes));
                          try {
                          } catch (Exception e) {
        • 1. Re: Unexpected output with PipedInputStream/PipedOutputStream
          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.