package edu.calpoly.cpe205.fetter;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.lang.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.JTextArea;
/**
* This class is a specialized version of the OutputStream that
* redirects its output to a JTextArea.
*/
// Author: Wes Strickland
// Version History:
// Nov 18, 2000 - comments/pseudocode
// Nov 30, 2000 - (Mike Hebron) changed class description
// Jan 12, 2001 - Implemented class
// Feb 8, 2001 - Wes Strickland Added comments and moved braces for Stage Two release
public class OutputAreaStream extends OutputStream
{
/**
* Class constructs the data to be sent to the output stream.
*
* Pre-conditions: none
* Post-conditions: output set to be display of type JTextArea
* @param out Output stream
*/
public OutputAreaStream(JTextArea display, String logFile)
{
// SET out to display
out = display;
debugLogFile = new File(logFile);
try {
if (debugLogFile.exists())
debugLogFile.delete();
debugLogFile.createNewFile();
} catch (IOException exc) {
debugLogFile = null;
exc.printStackTrace();
} catch (SecurityException exc) {
debugLogFile = null;
exc.printStackTrace();
}
}
/**
* Writes the specified byte to this output stream
*
* Pre-conditions: none
* Post-conditions: string of b is appended to text area
*/
public void write(int b)
{
// CREATE byte array from b
// CREATE string from byte array
// APPEND string to text area
String msg = new String(new char[] {(char)b}); //Change char to byte array
out.append(msg);
if (debugLogFile == null || !debugLogFile.exists())
return;
try {
BufferedWriter logWriter = new BufferedWriter(
new FileWriter(debugLogFile.toString(), true));
// append
logWriter.write(msg, 0, msg.length());
logWriter.close();
} catch (IOException exc) {
debugLogFile = null;
exc.printStackTrace();
}
}
/**
* Writes a byte array to this output stream
*
* Pre-conditions: none
* Post-conditions: string is written to output stream
*/
public void write(byte[] b)
{
// CREATE string from byte array
// APPEND string to text area
char[] charArray = new char[b.length]; //New byte array to convert to String later
for(int ndx = 0; ndx < b.length; ndx++)
{
charArray[ndx] = (char)b[ndx];
}
String msg = new String(charArray); //Convert to String
out.append(msg);
if (debugLogFile == null || !debugLogFile.exists())
return;
try {
BufferedWriter logWriter = new BufferedWriter(
new FileWriter(debugLogFile.toString(), true));
// append
logWriter.write(msg, 0, msg.length());
logWriter.close();
} catch (IOException exc) {
debugLogFile = null;
exc.printStackTrace();
}
}
/**
* Writes a byte array to this output stream
*
* Pre-conditions: none
* Post-conditions: none
*/
public void write(byte[] b, int off, int len)
{
// CREATE byte array from b with offset off
// CREATE string from byte array
// APPEND string to text area
char[] charArray = new char[len]; //Create byte array with chars
for(int ndx = 0; ndx < len; ndx++)
{
charArray[ndx] = (char)b[off + ndx];
}
String msg = new String(charArray); //Store byte array as String
out.append(msg);
if (debugLogFile == null || !debugLogFile.exists())
return;
try {
BufferedWriter logWriter = new BufferedWriter(
new FileWriter(debugLogFile.toString(), true));
logWriter.write(msg, 0, msg.length());
logWriter.close();
} catch (IOException exc) {
debugLogFile = null;
exc.printStackTrace();
}
}
/**
* Text output area
*/
protected JTextArea out; //Create instance for text output in GUI
protected File debugLogFile;
}