import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class EngineUI_SWT extends Thread {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(EngineUI_SWT.class);
private StyledText styledText;
protected Shell shell;
private Display display;
private PrintStream printStream;
//
Object syn = new Object();
String context = "开始测试";
private boolean keepRunning = true;
private PipedOutputStream pipedOutputStream = new PipedOutputStream();
private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() {
public void close() {
keepRunning = false;
try {
super.close();
pipedOutputStream.close();
} catch (IOException e) {
System.exit(1);// 记录错误或其他处理;为简单计,此处我们直接结束
}
}
};
private PipedInputStream pipedInputStream = new PipedInputStream() {
public void close() {
keepRunning = false;
try {
super.close();
} catch (IOException e) {
System.exit(1);// 记录错误或其他处理;为简单计,此处我们直接结束
}
}
}; protected EngineUI_SWT() throws IOException {
pipedOutputStream.connect(pipedInputStream);
this.printStream = new PrintStream(byteArrayOutputStream);
System.setOut(this.printStream);
System.setErr(this.printStream);
startByteArrayReaderThread();
// startConsoleReaderThread(pipedInputStream);
} /**
 * Launch the application
 * 
 * @param args
 */
public static void main(String[] args) {
try {
EngineUI_SWT instance = new EngineUI_SWT();
instance.start();
// ########
// instance.startConsoleReaderThread(instance.pipedInputStream);
//
// startWriterTestThread("写操作线程 #1", System.out, 1050, 10);
// startWriterTestThread("写操作线程 #2", System.err, 1000, 10);
// ########
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void run() {
EngineUI_SWT window;
try {
window = new EngineUI_SWT();
window.open();
} catch (IOException e) {
e.printStackTrace();
}
} /**
 * Open the window
 */
public void open() {
display = Display.getDefault();
createContents();
//
new Thread(new Runnable() {
public void run() {
while (true) {
try {
synchronized (syn) {
syn.wait();
}
} catch (InterruptedException e) {
}
display.asyncExec(new Thread() {
@Override
public void run() {
if (context != null) styledText.append(context + "\n");
}
});
}
}
}).start();
new ReaderThread();
//
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
} /**
 * Create contents of the window
 */
protected void createContents() {
shell = new Shell();
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setSize(500, 375);
shell.setText("SWT Application");
styledText = new StyledText(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
final GridData gd_styledText = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
gd_styledText.widthHint = 483;
styledText.setLayoutData(gd_styledText);
final Button systemoutButton = new Button(shell, SWT.NONE);
systemoutButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent arg0) {
startWriterTestThread("写操作线程 #1", System.out, 1000, 1000);
}
});
final GridData gd_systemoutButton = new GridData(SWT.FILL, SWT.CENTER, true, false);
systemoutButton.setLayoutData(gd_systemoutButton);
systemoutButton.setText("system.out");
final Button systemerrButton = new Button(shell, SWT.NONE);
systemerrButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
startWriterTestThread("写操作线程 #2", System.err, 100, 10);
}
});
systemerrButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
systemerrButton.setText("system.err");
final Button button2 = new Button(shell, SWT.NONE);
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent arg0) {
startConsoleReaderThread(pipedInputStream);
}
});
button2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
button2.setText("reader thread");
} void startByteArrayReaderThread() {
new Thread(new Runnable() {
public void run() {
while (keepRunning) {
if (byteArrayOutputStream.size() > 0) {// 检查流里面的字节数
byte[] buffer = null;
synchronized (byteArrayOutputStream) {
buffer = byteArrayOutputStream.toByteArray();
System.err.println(buffer.toString() + "\t" + buffer.length);
byteArrayOutputStream.reset(); // 清除缓冲区
}
try {
pipedOutputStream.write(buffer, 0, buffer.length);// 把提取到的数据发送给PipedOutputStream
} catch (IOException e) {
System.exit(1);// 记录错误或其他处理;为简单计,此处我们直接结束
}
} else
// 没有数据可用,线程进入睡眠状态
try {
Thread.sleep(1000);// 每隔1秒查看ByteArrayOutputStream检查新数据
} catch (InterruptedException e) {
}
}
}
}).start();
} // startByteArrayReaderThread() void startConsoleReaderThread(final InputStream inputStream) { display.asyncExec(new Thread() {
@Override
public void run() {
try {
if (inputStream.available() > 0) {
final BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String context;
while ((context = bufferReader.readLine()) != null) {
stringBuffer.setLength(0);
stringBuffer.append(context);
// styledText.append(context + "\n");
}
} else {
System.err.println("haha");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
} // startConsoleReaderThread() static void startWriterTestThread(final String name, final PrintStream ps, final int delay, final int count) {
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= count; ++i) {
// ps.println("***" + name + ", hello !, i=" + i);
// logger.info("***" + name + ", hello !, i=" + i);
try {
Thread.sleep(delay);
logger.info("***" + name + ", hello !, i=" + i);
} catch (InterruptedException e) {
}
}
}
}).start();
} // startWriterTestThread() class ReaderThread extends Thread {
public ReaderThread() {
start();
} public void run() {
while (true) {
synchronized (this) {
try {
Thread.sleep(1000);
synchronized (syn) {
if (pipedInputStream.available() > 0) {
final BufferedReader bufferReader = new BufferedReader(new InputStreamReader(pipedInputStream));
context = bufferReader.readLine();
syn.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}