home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
InterleavedReader.java
< prev
next >
Wrap
Text File
|
1998-09-08
|
5KB
|
164 lines
package com.symantec.itools.io;
import java.io.*;
import java.util.*;
/**
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public class InterleavedReader
{
public InterleavedReader()
{
}
/**
* @param ins TODO
* @since VCafe 3.0
*/
public void addInputStream(InputStream ins)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
ReaderThread readerThread = new ReaderThread(reader, this);
inputRecords.addElement(new InputRecord(ins, readerThread));
readerThread.start(); // and he's off.....
}
public synchronized void removeInputStream(InputStream ins)
{
for (Enumeration e = inputRecords.elements(); e.hasMoreElements(); ) {
InputRecord record = (InputRecord) e.nextElement();
if (record.stream == ins) {
try {
record.stream.close(); // will hopefully cause thread to exit cleanly....
} catch (IOException exception) {
System.out.println("Unexpected IOException when removing input stream: " + exception);
}
inputRecords.removeElement(record);
return;
}
}
}
public synchronized String readLine()
throws IOException
{
String line;
if (inputRecords.size() == 0)
return null;
while (true) {
// Debug.println("Checking for input....");
if (readException != null) {
IOException e = readException;
readException = null;
throw e;
} else if (lastLineRead != null) {
line = lastLineRead;
lastLineRead = null;
break;
} else if (inputRecords.size() == 0) {
line = null;
break;
}
try {
wait(10); // do we want a (short) timeout?
} catch (InterruptedException e) {
}
}
notify();
return line;
}
public synchronized InputStream lastStreamRead()
{
for(Enumeration e = inputRecords.elements(); e.hasMoreElements(); ) {
InputRecord record = (InputRecord) e.nextElement();
if (record.thread == lastThreadRead)
return record.stream;
}
return null;
}
protected synchronized void lineWasRead(String line, ReaderThread thread)
{
while (lastLineRead != null) {
try {
wait(100);
} catch (InterruptedException e) {
}
}
lastThreadRead = thread;
lastLineRead = line;
notify();
}
protected synchronized void exceptionDuringRead(IOException e)
{
readException = e;
notify();
}
protected synchronized void inputDone(ReaderThread thd)
{
for (Enumeration e = inputRecords.elements(); e.hasMoreElements(); ) {
InputRecord record = (InputRecord) e.nextElement();
if (record.thread == thd)
inputRecords.removeElement(record);
}
notify();
}
protected class ReaderThread extends java.lang.Thread
{
public ReaderThread(BufferedReader rdr, InterleavedReader owner)
{
super("ReaderThread-" + (new Integer(threadCount++)).toString());
reader = rdr;
master = owner;
}
public void run()
{
try {
String s = reader.readLine();
while (s != null) {
//Debug.println("Got input: " + s);
master.lineWasRead(s, this);
s = reader.readLine();
}
} catch (IOException e) {
master.exceptionDuringRead(e);
}
// either input has been exhausted or an exception was reported, so no more work to do
master.inputDone(this);
// Debug.println("No more input. Exiting....");
}
private BufferedReader reader;
private InterleavedReader master;
}
protected class InputRecord {
public InputRecord(InputStream strm, ReaderThread th)
{
stream = strm;
thread = th;
}
public InputStream stream;
public ReaderThread thread;
}
protected Vector inputRecords = new Vector(4);
protected String lastLineRead;
protected ReaderThread lastThreadRead;
protected IOException readException;
static private int threadCount = 0;
}