F
Felix Dejavu
Hello y'all,
Here's a little applet that streams to a TextArea control. The important
class is TextAreaOutputStream which allows a TextArea to be written to
like a stream. You can see that it's useful in adapting a console oriented
program to an applet, or for displaying debugging messages.
Please tell me what you think about it. Am I reinventing the wheel? Is
there an better or established way to stream to TextArea's? Thanks for
your input.
-fd
---- Start StreamToTextArea.java ----
import java.applet.*;
import java.awt.*;s
import java.io.*;
import java.net.*;
class TextAreaOutputStream extends OutputStream {
TextArea ta;
public TextAreaOutputStream(TextArea ta) {
this.ta = ta;
}
public void write(byte[] b) {
write(b, 0, b.length);
}
public void write(byte[] b, int off, int len) {
synchronized(this) {
ta.append(new String(b, off, len));
}
}
public void write(int b) {
byte[] tmp = {(byte)b};
write(tmp);
}
}
class QuickAndDirtyPipe implements Runnable {
private InputStream i;
private OutputStream o;
public QuickAndDirtyPipe(InputStream i, OutputStream o, boolean
block) throws IOException {
this.i = i;
this.o = o;
if(block) pipeData();
else new Thread(this).start();
}
public void run() {
try {
pipeData();
} catch(IOException ex) {
throw new Error(ex);
}
}
public void pipeData() throws IOException {
int n;
byte[] b = new byte[2048];
while(true) {
n = i.read(b);
if(n == -1) break;
o.write(b, 0, n);
}
}
}
// Make our own stderr because unsigned Applets can't redirect System.err
class Debug {
public static PrintStream out = System.err;
public static void redirect(OutputStream o) {
out = new PrintStream(o);
}
}
public class StreamToTextAreaTest extends Applet {
// can't use this example because unsigned Applets can't make
connections to 3rd party
public static void getPage(OutputStream dst) {
Debug.out.println("\n---- Getting Page ----");
try {
Socket s = new Socket("www.google.com", 80);
s.getOutputStream().write(new String("GET /
HTTP/1.0\r\n\r\n").getBytes("US-ASCII"));
new QuickAndDirtyPipe(s.getInputStream(), dst,
true);
s.close();
} catch(IOException ex) {
throw new Error(ex);
}
Debug.out.println("\n---- Page Complete ----");
}
public static void getFakePage(OutputStream dst) {
String fakePage =
"<html>\r\n" +
"<head>\r\n" +
"<title>My Fake Page</title>\r\n" +
"</head>\r\n" +
"<body>\r\n" +
"<h1>This is my fake page.</h1>\r\n" +
"</body>\r\n" +
"</html>";
Debug.out.println("\n---- Getting Page ----");
try {
ByteArrayInputStream src = new
ByteArrayInputStream(new String(fakePage).getBytes("US-ASCII"));
new QuickAndDirtyPipe(src, dst, true);
} catch(Exception ex) {
throw new Error(ex);
}
Debug.out.println("\n---- Page Complete ----");
}
public void init() {
setLayout(new GridLayout());
TextArea ta = new TextArea();
add(ta);
validate();
TextAreaOutputStream tout = new TextAreaOutputStream(ta);
Debug.redirect(tout);
getFakePage(tout);
}
public static void main(String[] args) {
getPage(System.out);
}
}
---- End of File ----
Here's a little applet that streams to a TextArea control. The important
class is TextAreaOutputStream which allows a TextArea to be written to
like a stream. You can see that it's useful in adapting a console oriented
program to an applet, or for displaying debugging messages.
Please tell me what you think about it. Am I reinventing the wheel? Is
there an better or established way to stream to TextArea's? Thanks for
your input.
-fd
---- Start StreamToTextArea.java ----
import java.applet.*;
import java.awt.*;s
import java.io.*;
import java.net.*;
class TextAreaOutputStream extends OutputStream {
TextArea ta;
public TextAreaOutputStream(TextArea ta) {
this.ta = ta;
}
public void write(byte[] b) {
write(b, 0, b.length);
}
public void write(byte[] b, int off, int len) {
synchronized(this) {
ta.append(new String(b, off, len));
}
}
public void write(int b) {
byte[] tmp = {(byte)b};
write(tmp);
}
}
class QuickAndDirtyPipe implements Runnable {
private InputStream i;
private OutputStream o;
public QuickAndDirtyPipe(InputStream i, OutputStream o, boolean
block) throws IOException {
this.i = i;
this.o = o;
if(block) pipeData();
else new Thread(this).start();
}
public void run() {
try {
pipeData();
} catch(IOException ex) {
throw new Error(ex);
}
}
public void pipeData() throws IOException {
int n;
byte[] b = new byte[2048];
while(true) {
n = i.read(b);
if(n == -1) break;
o.write(b, 0, n);
}
}
}
// Make our own stderr because unsigned Applets can't redirect System.err
class Debug {
public static PrintStream out = System.err;
public static void redirect(OutputStream o) {
out = new PrintStream(o);
}
}
public class StreamToTextAreaTest extends Applet {
// can't use this example because unsigned Applets can't make
connections to 3rd party
public static void getPage(OutputStream dst) {
Debug.out.println("\n---- Getting Page ----");
try {
Socket s = new Socket("www.google.com", 80);
s.getOutputStream().write(new String("GET /
HTTP/1.0\r\n\r\n").getBytes("US-ASCII"));
new QuickAndDirtyPipe(s.getInputStream(), dst,
true);
s.close();
} catch(IOException ex) {
throw new Error(ex);
}
Debug.out.println("\n---- Page Complete ----");
}
public static void getFakePage(OutputStream dst) {
String fakePage =
"<html>\r\n" +
"<head>\r\n" +
"<title>My Fake Page</title>\r\n" +
"</head>\r\n" +
"<body>\r\n" +
"<h1>This is my fake page.</h1>\r\n" +
"</body>\r\n" +
"</html>";
Debug.out.println("\n---- Getting Page ----");
try {
ByteArrayInputStream src = new
ByteArrayInputStream(new String(fakePage).getBytes("US-ASCII"));
new QuickAndDirtyPipe(src, dst, true);
} catch(Exception ex) {
throw new Error(ex);
}
Debug.out.println("\n---- Page Complete ----");
}
public void init() {
setLayout(new GridLayout());
TextArea ta = new TextArea();
add(ta);
validate();
TextAreaOutputStream tout = new TextAreaOutputStream(ta);
Debug.redirect(tout);
getFakePage(tout);
}
public static void main(String[] args) {
getPage(System.out);
}
}
---- End of File ----