0

Using JavaFX I am trying to redirect Jsch session.SetOutputStream to a TextArea in a form of a PrintStream. PrintStream works as excepted except for the session output.

TextArea PrintStream setup:

ConsoleClass console = new ConsoleClass(textArea);
PrintStream ps = new PrintStream(console, true);
System.setOut(ps);

public class ConsoleClass extends OutputStream {

    private TextArea output;

    public ConsoleClass(TextArea ta) {
        this.output = ta;
    }

    @Override
    public void write(int i) throws IOException {
        output.appendText(String.valueOf((char) i));
    }
    @Override
    public void write(byte[] b) throws IOException {
        output.appendText(String.valueOf(b);
    }

}

Now I pass this ps (PrintStream) to the Jsch class. and I set the OutputStream as follow:

session.SetOutputStream(ps) //Nothing gets displayed

Note:

The ps still displays anything within the jsch class but not within its session. 

To see what goes on in the session I have to

remove System.setOut(ps);
add session.SetOutputStream(System.out)
The same behavior applies to the channel.SetOutputStream

jsch:

jsch jsch = new Jsch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(passed);
session.connect(30000);
session.SetOutputStream(ps); // nothing gets displayed
//but when I set it to System.out it shows everything
session.SetOutputStream(System.out) // console shows the commands
if (session.isConnected()){
System.out.println(session.getHost() + "Session is Established");
//this shows in the TextArea
}
Channel channel=session.openChannel("shell");
System.out.println("Testing testing"); //shows in the TextArea

channel.disconnect();
System.out.println ("channel Status" + channel.getExitStatus); 
//shows in the TextArea

what am I missing? as you can see I am able to append System.out to the TextArea and I am having only the issue with session or channel .setOutputStream(ps)

.setOutputStream only shows what goes on in the session/channel 
when it's directed to System.out
Moe
  • 1,427
  • 4
  • 34
  • 54
  • 1
    Have you implemented the `write(byte b[])`? – kwjsksai Oct 25 '16 at 13:00
  • Your implementation of `write(int)` is wrong. The `int` is the value of a **byte** of output, not a character. If your session sends out UTF-8 information, it will be sending two separate bytes for anything that is not ASCII. Also, how is your text area going to support control characters? – RealSkeptic Oct 25 '16 at 13:11
  • @Immueggpain it shouldn't be necessary, as `write(byte b[])` is implemented at the base class as several consecutive calls to `write(int)`. – RealSkeptic Oct 25 '16 at 13:12
  • Immueggpain, I haven't done byte b[ ] can you give me an example of how to modify my code above? – Moe Oct 25 '16 at 13:14
  • @RealSkeptic you are right... it shouldn't be the problem here – kwjsksai Oct 25 '16 at 13:19
  • Anyway, from the sparse documentation on the Internet, `session.setOutputStream` doesn't do anything. You're supposed to set the output stream on the channel. – RealSkeptic Oct 25 '16 at 13:20
  • @RealSkeptic, I tried both, nothing is getting displayed to the PrintStream. – Moe Oct 25 '16 at 13:23
  • So perhaps you should debug it and see what byte values are actually passed to it. They could be values that you don't expect. – RealSkeptic Oct 25 '16 at 13:24
  • What is `System.setOut(ps);` for? Also if `session.SetOutputStream(System.out)` works but `session.SetOutputStream(ps)` doesn't, the problem is likely to be located in your UI codes. – kwjsksai Oct 25 '16 at 13:40
  • System.setOut(ps) --> to set System.out to printstream. I have tried the byte [] b and still not working. any thoughts on what to do next? – Moe Oct 25 '16 at 13:50
  • How did you get the `textArea`? Also is `appendText` called within UI thread? I don't know if it's necessary but it's generally recommended. – kwjsksai Oct 25 '16 at 14:05
  • updated the question with the jsch code snippet. – Moe Oct 25 '16 at 14:30
  • How about wrap `output.appendText(String.valueOf(b);` inside `Platform.runLater`? Does it work? – kwjsksai Oct 25 '16 at 14:48
  • nothing happens, it's still not printing... – Moe Oct 25 '16 at 17:28
  • do I need to consider StandardCharsets.UTF_8? if so how can i integrate it? – Moe Oct 25 '16 at 18:06
  • use `new String(bytes, charset)` – kwjsksai Oct 26 '16 at 03:03

0 Answers0