package lws;
abstract public class Filter implements Runnable{
protected Pipe input_;
protected Pipe output_;
private boolean is_started_ = false;
public Filter(Pipe input, Pipe output){
input_ = input;
output_ = output;
}
//to start a new thread to do the transform work
public void start(){
if(!is_started_){
is_started_ = true;
Thread thread = new Thread(this);
thread.start();
}
}
//to stop the running thread
public void stop(){
is_started_ = false;
}
//the method of the thread
public void run(){
transform();
}
//method to do the transform work of the current instance of Filter
abstract protected void transform();
}
package lws;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;
public class Pipe{
private PipedReader reader_;
private PipedWriter writer_;
public Pipe() throws IOException{
writer_ = new PipedWriter();
reader_ = new PipedReader();
writer_.connect(reader_);
}
public void write(int c) throws IOException{
writer_.write(c);
}
public int read() throws IOException{
return reader_.read();
}
public void closeWriter() throws IOException{
writer_.flush();
writer_.close();
}
public void closeReader() throws IOException{
reader_.close();
}
}