Back to index

lightning-sunbird  0.9+nobinonly
Console.java
Go to the documentation of this file.
00001 /*
00002        Console.java
00003        
00004        Simple console that lets an Applet attach to the system console.
00005        
00006        by Patrick C. Beard.
00007  */
00008 
00009 package netscape.console;
00010 
00011 import java.io.*;
00012 import java.awt.*;
00013 
00014 public class Console {
00015        // Save primordial System streams.
00016        private static InputStream in;
00017        private static PrintStream out;
00018        private static PrintStream err;
00019        private static TextArea console;
00020 
00021        private static class Input extends InputStream {
00022               byte[] buffer = new byte[1024];
00023               int position = 0;
00024               int count = 0;
00025               
00026               private void fillBuffer() throws EOFException {
00027                      // int length = readLine(buffer, 0, buffer.length);
00028                      int length = 1024;
00029                      if (length == -1)
00030                             throw new EOFException();
00031                      count = length;
00032                      position = 0;
00033               }
00034               
00035            public int read() throws IOException {
00036               synchronized(this) {
00037                      if (position >= count)
00038                             fillBuffer();
00039                      return buffer[position++];
00040               }
00041            }
00042 
00043            public int read(byte[] b, int offset, int length) throws IOException {
00044                      synchronized(this) {
00045                             // only fill the buffer at the outset, always returns at most one line of data.
00046                      if (position >= count)
00047                             fillBuffer();
00048                      int initialOffset = offset;
00049                      while (offset < length && position < count) {
00050                             b[offset++] = buffer[position++];
00051                      }
00052                      return (offset - initialOffset);
00053                   }
00054            }
00055        }
00056        
00057        private static class Output extends OutputStream implements Runnable {
00058               StringBuffer buffer = new StringBuffer();
00059               
00060               public Output() {
00061                      Thread flusher = new Thread(this, getClass().getName() + "-Flusher");
00062                      flusher.setDaemon(true);
00063                      flusher.start();
00064               }
00065        
00066               public synchronized void write(int b) throws IOException {
00067                      this.buffer.append((char)b);
00068                      notify();
00069            }
00070 
00071               public synchronized void write(byte[] buffer, int offset, int count) throws IOException {
00072                      this.buffer.append(new String(buffer, 0, offset, count));
00073                      notify();
00074               }
00075 
00076            public synchronized void flush() throws IOException {
00077               String value = this.buffer.toString();
00078               console.append(value);
00079               this.buffer.setLength(0);
00080        }
00081               
00086        public synchronized void run() {
00087                      for (;;) {
00088                             try {
00089                             wait();
00090                             flush();
00091                      } catch (InterruptedException ie) {
00092                      } catch (IOException ioe) {
00093                      }
00094               }
00095        }
00096        }
00097        
00098        private static class Error extends Output {}
00099 
00100        public static void init(TextArea text) {
00101               in = System.in;
00102               out = System.out;
00103               err = System.err;
00104               console = text;
00105 
00106               System.setIn(new Input());
00107               System.setOut(new PrintStream(new Output()));
00108               System.setErr(new PrintStream(new Error()));
00109        }
00110        
00111        public static void dispose() {
00112               System.setIn(in);
00113               System.setOut(out);
00114               System.setErr(err);
00115               
00116               console = null;
00117        }
00118 }