Turning a JTextArea into a InputStream

Discussion in 'Java' started by cokofreedom@gmail.com, Jul 1, 2008.

  1. Guest

    I've been trying to convert a JTextArea into an InputStream with
    almost no success. After trying to google for a few results any that I
    have found have been of little use.

    The idea is to use the JSch to SSH using a JTextArea as an Input and
    another as an Output (with the possibility in the future of linking
    them together). I have been able to make the Output work with the
    following coe segments:

    PrintStream out = new PrintStream(new
    TextAreaOutputStream(txtOutput));

    final class TextAreaOutputStream extends OutputStream {
    private final JTextArea textArea;
    private final StringBuilder sb = new StringBuilder();
    public TextAreaOutputStream(final JTextArea textArea) {
    this.textArea = textArea;
    }
    @Override
    public void write(int b) throws IOException {
    if (b == '\r') { return; }
    if (b == '\n') {
    textArea.append(sb.toString());
    sb.setLength(0);
    }
    sb.append((char)b);
    }
    }

    However my attempts with something similar for an InputStream have
    been largely unsuccessful. Based upon a forum entry on the
    java.sun.com forum I tried to implement the following:

    final class TextAreaInputStream extends InputStream implements
    KeyListener {
    String lastLine;
    int lastKeyCode;
    JTextArea txtInput;
    Object mKeyLock = new Object();
    Object mLineLock = new Object();
    private TextAreaInputStream(JTextArea txtInput) {
    this.txtInput = txtInput;
    }
    public String readLine() {
    synchronized(mLineLock) {
    try {
    mLineLock.wait();
    } catch (InterruptedException ex) {
    System.err.println(ex.getMessage());
    }
    }
    return lastLine;
    }
    @Override
    public int read() {
    synchronized(mKeyLock) {
    try {
    mKeyLock.wait();
    } catch (InterruptedException ex) {
    System.err.println(ex.getMessage());
    }
    }
    return lastKeyCode;
    }
    public void keyReleased(KeyEvent e) { }
    public void keyTyped(KeyEvent e) { }
    public void keyPressed(KeyEvent e) {
    System.err.println("keyPressed");
    lastKeyCode = e.getKeyCode();
    if (lastKeyCode == KeyEvent.VK_ENTER) {
    String txt = txtInput.getText();
    int idx = txt.lastIndexOf(
    \n', txtInput.getCaretPosition() - 1);
    lastLine = txt.substring(
    idx != -1 ? idx : 0,
    txtInput.getCaretPosition());
    }
    }
    public InputStream toInputStream() {
    return new MyInputStream();
    }
    private class MyInputStream extends InputStream {
    public int read() {
    return TextAreaInputStream.this.read();
    }
    }
    }

    However I do not think this does anything...So I really just want to
    be able to send commands from a JTextArea through the SSH and get the
    reply, and presently I can only get the Reply if I use System.in...

    If someone can guide me to a step I have missed, or perhaps get me
    back on track that would be of great help to me!

    Thanks

    Coko
    , Jul 1, 2008
    #1
    1. Advertising

  2. On 2008-07-01 11:33 +0100, allegedly wrote:
    > I've been trying to convert a JTextArea into an InputStream with
    > almost no success. After trying to google for a few results any that I
    > have found have been of little use.
    >
    > The idea is to use the JSch to SSH using a JTextArea as an Input and
    > another as an Output (with the possibility in the future of linking
    > them together). I have been able to make the Output work with the
    > following coe segments:
    >
    > PrintStream out = new PrintStream(new
    > TextAreaOutputStream(txtOutput));
    >
    > final class TextAreaOutputStream extends OutputStream {
    > private final JTextArea textArea;
    > private final StringBuilder sb = new StringBuilder();
    > public TextAreaOutputStream(final JTextArea textArea) {
    > this.textArea = textArea;
    > }
    > @Override
    > public void write(int b) throws IOException {
    > if (b == '\r') { return; }
    > if (b == '\n') {
    > textArea.append(sb.toString());
    > sb.setLength(0);
    > }
    > sb.append((char)b);
    > }
    > }
    >
    > However my attempts with something similar for an InputStream have
    > been largely unsuccessful. Based upon a forum entry on the
    > java.sun.com forum I tried to implement the following:
    >
    > final class TextAreaInputStream extends InputStream implements
    > KeyListener {
    > String lastLine;
    > int lastKeyCode;
    > JTextArea txtInput;
    > Object mKeyLock = new Object();
    > Object mLineLock = new Object();
    > private TextAreaInputStream(JTextArea txtInput) {
    > this.txtInput = txtInput;
    > }
    > public String readLine() {
    > synchronized(mLineLock) {
    > try {
    > mLineLock.wait();
    > } catch (InterruptedException ex) {
    > System.err.println(ex.getMessage());
    > }
    > }
    > return lastLine;
    > }
    > @Override
    > public int read() {
    > synchronized(mKeyLock) {
    > try {
    > mKeyLock.wait();
    > } catch (InterruptedException ex) {
    > System.err.println(ex.getMessage());
    > }
    > }
    > return lastKeyCode;
    > }
    > public void keyReleased(KeyEvent e) { }
    > public void keyTyped(KeyEvent e) { }
    > public void keyPressed(KeyEvent e) {
    > System.err.println("keyPressed");
    > lastKeyCode = e.getKeyCode();
    > if (lastKeyCode == KeyEvent.VK_ENTER) {
    > String txt = txtInput.getText();
    > int idx = txt.lastIndexOf(
    > \n', txtInput.getCaretPosition() - 1);
    > lastLine = txt.substring(
    > idx != -1 ? idx : 0,
    > txtInput.getCaretPosition());
    > }
    > }
    > public InputStream toInputStream() {
    > return new MyInputStream();
    > }
    > private class MyInputStream extends InputStream {
    > public int read() {
    > return TextAreaInputStream.this.read();
    > }
    > }
    > }
    >
    > However I do not think this does anything...So I really just want to
    > be able to send commands from a JTextArea through the SSH and get the
    > reply, and presently I can only get the Reply if I use System.in...
    >
    > If someone can guide me to a step I have missed, or perhaps get me
    > back on track that would be of great help to me!
    >
    > Thanks
    >
    > Coko


    - Add a few listeners to the TextArea's underlying Document model to
    track text input. Write text additions to a simple ByteArrayOutputStream.
    - Use Piped(Output|Input)Streams (wrap the BAOUS into a POS, connect
    that pipe to a PIS, voilĂ ).

    Note: your approach is a bit flawed inasmuch as you won't be able to
    deal with text deletions and additions elsewhere than at the end.

    --
    DF.
    to reply privately, change the top-level domain
    in the FROM address from "invalid" to "net"
    Daniele Futtorovic, Jul 1, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik Cruz

    Turning rows into columns

    Erik Cruz, Sep 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,837
    Hermit Dave
    Sep 25, 2004
  2. Replies:
    0
    Views:
    638
  3. R
    Replies:
    5
    Views:
    2,113
    Kevin McMurtrie
    Mar 13, 2005
  4. Yodai
    Replies:
    5
    Views:
    583
    Yodai
    Oct 12, 2003
  5. Ale
    Replies:
    3
    Views:
    5,740
Loading...

Share This Page