+ // and a thread to append text to the textarea
+ textAppender = new Thread(this);
+ textAppender.setDaemon(true);
+ textAppender.start();
+ }
+
+ PipedOutputStream pout = null, perr = null;
+
+ public void redirectStreams()
+ {
+ if (pout == null)
+ {
+ try
+ {
+ pout = new PipedOutputStream(this.pin);
+ System.setOut(new PrintStream(pout, true));
+ } catch (java.io.IOException io)
+ {
+ textArea.append("Couldn't redirect STDOUT to this console\n"
+ + io.getMessage());
+ io.printStackTrace(stderr);
+ } catch (SecurityException se)
+ {
+ textArea.append("Couldn't redirect STDOUT to this console\n"
+ + se.getMessage());
+ se.printStackTrace(stderr);
+ }
+
+ try
+ {
+ perr = new PipedOutputStream(this.pin2);
+ System.setErr(new PrintStream(perr, true));
+ } catch (java.io.IOException io)
+ {
+ textArea.append("Couldn't redirect STDERR to this console\n"
+ + io.getMessage());
+ io.printStackTrace(stderr);
+ } catch (SecurityException se)
+ {
+ textArea.append("Couldn't redirect STDERR to this console\n"
+ + se.getMessage());
+ se.printStackTrace(stderr);
+ }
+ }
+ }
+
+ public void unredirectStreams()
+ {
+ if (pout != null)
+ {
+ try
+ {
+ System.setOut(stdout);
+ pout.flush();
+ pout.close();
+ pin = new PipedInputStream();
+ pout = null;
+ } catch (java.io.IOException io)
+ {
+ textArea.append("Couldn't unredirect STDOUT to this console\n"
+ + io.getMessage());
+ io.printStackTrace(stderr);
+ } catch (SecurityException se)
+ {
+ textArea.append("Couldn't unredirect STDOUT to this console\n"
+ + se.getMessage());
+ se.printStackTrace(stderr);
+ }
+
+ try
+ {
+ System.setErr(stderr);
+ perr.flush();
+ perr.close();
+ pin2 = new PipedInputStream();
+ perr = null;
+ } catch (java.io.IOException io)
+ {
+ textArea.append("Couldn't unredirect STDERR to this console\n"
+ + io.getMessage());
+ io.printStackTrace(stderr);
+ } catch (SecurityException se)
+ {
+ textArea.append("Couldn't unredirect STDERR to this console\n"
+ + se.getMessage());
+ se.printStackTrace(stderr);
+ }
+ }