sb.delete(0, sb.length());
sb = new StringBuffer();
    public OutputStreamViewer(final String title, final String date,        final String ext)    {        OutputStream errout;        final PipedInputStream in;        try        {            final DateFormat dfi = DateFormat.getInstance();            String logdir = new StringBuffer().append(                JHack.JHACK_DIR.toString()).append(File.separator).append(                "logs").toString();            File logdirf = new File(logdir);            if (!logdirf.exists())                logdirf.mkdir();            String logfile = new StringBuffer().append(logdir).append(                File.separator).append(date).append(".").append(ext).append(                ".log").toString();            FileWriter l = null;            try            {                l = new FileWriter(logfile);            }            catch (IOException fioe)            {                JOptionPane.showMessageDialog(null, "Unable to open file "                    + logfile + "\n for writing.", "Logging error",                    JOptionPane.WARNING_MESSAGE);            }            final FileWriter log = (l == null ? null : l);            //            PipedInputStream tmp;            //            in = new InputStreamReader(tmp = new PipedInputStream());            errout = new PipedOutputStream(in = new PipedInputStream());            errps = new PrintStream(errout);            errDia = new JFrame(title);            errArea = new JTextArea(20, 60);            errArea.setEditable(false);            errArea.append(dfi.format(new Date()) + ": ");            errDia.getContentPane().setLayout(new BorderLayout());            errDia.getContentPane().add(new JScrollPane(errArea),                BorderLayout.CENTER);            JButton close = new JButton("Close");            errDia.addWindowListener(new WindowAdapter()            {                public void windowClosing(WindowEvent e)                {                    errArea.append("\n\n------------------------\n"                        + "Window closed." + "\n------------------------\n\n");                }                public void windowOpened(WindowEvent e)                {                    synchronized (sbb)                    {                        errArea.append(sbb.toString());                        sbb.delete(0, sbb.length());                    }                }            });            close.addActionListener(new ActionListener()            {                public void actionPerformed(ActionEvent e)                {                    errDia.setVisible(false);                    errArea.append("\n\n------------------------\n"                        + "Window closed." + "\n------------------------\n\n");                }            });            errDia.getContentPane().add(close, BorderLayout.SOUTH);            errDia.pack();            t = new Thread()            {                StringBuffer sb = new StringBuffer();                int wait = 0;                private void appendToErrArea()                {                    String sbs = sb.toString();                    if (log != null)                    {                        try                        {                            log.write(sbs);                            log.flush();                        }                        catch (IOException e)                        {                            System.out.println("An error occured writing "                                + "to log file.");                        }                    }                    sb.delete(0, sb.length());                    if (!errDia.isVisible() && isEnabled())                    {                        errDia.setVisible(true);                        errArea.append(sbs);                        Rectangle r = errArea.getBounds();                        errArea.scrollRectToVisible(new Rectangle(0, r.height,                            1, 1));                    }                    else                    {                        synchronized (sbb)                        {                            sbb.append(sbs);                        }                    }                }                public void run()                {                    while (run)                    {                        try                        {                            if (in.available() > 0)                            {                                wait = 0;                                char c = (char) in.read();                                sb.append(Character.toString(c));                                if (c == '\n')                                {                                    sb.append(dfi.format(new Date()) + ": ");                                    send = error; //send if this is error                                    // output                                    appendToErrArea();                                }                            }                            else                            {                                if (sb.length() > 0)                                    appendToErrArea();                                if (send)                                {                                    wait++;                                    /*                                     * Output has to stop for 4 seconds before                                     * it is considered finished.                                     */                                    if (wait > 2)                                    {                                        sendError();                                        send = false;                                        wait = 0;                                    }                                }                                sleep(2000);                            }                        }                        catch (Exception e)                        {                            //e.printStackTrace(System.err);                        }                    }                    try                    {                        log.close();                    }                    catch (IOException e)                    {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            };            t.setDaemon(true);            t.setPriority(Thread.MIN_PRIORITY);            t.start();        }        catch (IOException e)        {            JOptionPane.showMessageDialog(null, "Unable to init " + title + "."                + "Subsequent messages will only be displayed to the console.",                "Error Initing Display", JOptionPane.WARNING_MESSAGE);            e.printStackTrace();        }    }