CrossJeeves Works!
This commit is contained in:
98
src/com/flaremicro/crossjeeves/net/StreamForwarder.java
Normal file
98
src/com/flaremicro/crossjeeves/net/StreamForwarder.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package com.flaremicro.crossjeeves.net;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import com.flaremicro.crossjeeves.net.packet.Packet7LogEntry;
|
||||
import com.flaremicro.util.Util;
|
||||
|
||||
/* TODO
|
||||
* Forward output from entire process, rather than just from started processes?
|
||||
*/
|
||||
public class StreamForwarder {
|
||||
private InnerThread innerThread;
|
||||
|
||||
public StreamForwarder(NetworkHandler handler, InputStream processInput, byte stdType) {
|
||||
innerThread = new InnerThread(handler, processInput, stdType);
|
||||
}
|
||||
|
||||
public void startAsync() {
|
||||
innerThread.start();
|
||||
}
|
||||
|
||||
public void end() {
|
||||
innerThread.stop();
|
||||
}
|
||||
|
||||
private class InnerThread implements Runnable {
|
||||
private Thread parent = null;
|
||||
private final NetworkHandler handler;
|
||||
private final InputStream processInput;
|
||||
private final byte stdType;
|
||||
private boolean isRunning = false;
|
||||
|
||||
public InnerThread(NetworkHandler handler, InputStream processInput, byte stdType) {
|
||||
this.handler = handler;
|
||||
this.processInput = processInput;
|
||||
this.stdType = stdType;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (!isRunning && parent != null)
|
||||
{
|
||||
isRunning = true;
|
||||
parent = new Thread(this);
|
||||
parent.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void print(String s)
|
||||
{
|
||||
if(stdType == Packet7LogEntry.STD_ERR)
|
||||
System.err.println(s);
|
||||
else
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
BufferedReader reader = null;
|
||||
try
|
||||
{
|
||||
reader = new BufferedReader(new InputStreamReader(processInput));
|
||||
String line;
|
||||
while (isRunning && (line = reader.readLine()) != null)
|
||||
{
|
||||
handler.enqueue(new Packet7LogEntry(stdType, line));
|
||||
print(line);
|
||||
}
|
||||
if (processInput.available() > 0)
|
||||
{
|
||||
byte[] remaining = new byte[processInput.available()];
|
||||
processInput.read(remaining);
|
||||
line = new String(remaining);
|
||||
print(line);
|
||||
handler.enqueue(new Packet7LogEntry(stdType, line));
|
||||
}
|
||||
parent = null;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
handler.enqueue(new Packet7LogEntry(stdType, "Error occured:" + ex.toString() + ", logging ended."));
|
||||
print("Error occured, logging ended.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.cleanClose(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user