
import java.io.*;

public class ResultReaderEx implements Runnable {

    public ResultReaderEx() { }

    public synchronized void run() {
	try {
	    m_reading = true;
	    while (true) {
		int ic = m_data.read();
		if (ic == -1) break; 
		char c = (char) ic;
		m_buffer.append(c);
	    }
	} catch (IOException e) {
	    System.out.println("RESULTREADEREX ERROR!!");
	    e.printStackTrace();
	}
	m_reading = false;
	notify();
    }

    public synchronized void startReading(InputStream is) {
	m_reading = false;
	m_buffer = new StringBuffer();
	m_data = new InputStreamReader(is);
	Thread readthread = new Thread(this);
	readthread.start();
    }

    public synchronized StringBuffer endReading() {
	StringBuffer cbuffer = new StringBuffer();
	try { if (m_reading) { System.out.println("RESULT: WAITING"); wait();}  } catch (InterruptedException e) { }
	cbuffer.append(m_buffer);
	return cbuffer;
    }

    private boolean m_reading=false;
    private StringBuffer m_buffer;
    private InputStreamReader m_data;

}
