Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Turning a JTextArea into a InputStream

Reply
Thread Tools

Turning a JTextArea into a InputStream

 
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      07-01-2008
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
 
Reply With Quote
 
 
 
 
Daniele Futtorovic
Guest
Posts: n/a
 
      07-01-2008
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert XML file into InputStream Ale Java 3 10-29-2009 05:29 PM
FIle Upload and InputStream into Excel bvasanth123@rediffmail.com ASP .Net 0 10-14-2005 03:19 PM
confused: Socket InputStream != ServerSocker InputStream R Java 5 03-13-2005 07:26 AM
Turning rows into columns Erik Cruz ASP .Net 2 09-25-2004 08:13 AM
Someone know how to prevent icons in Favorites & Desktop (XP) from turning into Explorer "e"s over time? fourstriper Computer Support 2 02-05-2004 09:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57