FileDocCategorySizeDatePackage
AsyncInputStream.javaAPI DocExample5291Thu Feb 04 16:10:38 GMT 1999None

AsyncInputStream

public class AsyncInputStream extends FilterInputStream implements Runnable

Fields Summary
private Thread
runner
private byte[]
result
private int
reslen
private boolean
EOF
private IOException
IOError
BusyFlag
lock
CondVar
empty
CondVar
full
Constructors Summary
protected AsyncInputStream(InputStream in, int bufsize)

		super(in);

		lock = new BusyFlag();		// Allocate sync variables
		empty = new CondVar(lock);
		full = new CondVar(lock);

		result = new byte[bufsize];	// Allocate Storage Area
		reslen = 0;			// and initialize variables
		EOF = false;
		IOError = null;
		runner = new Thread(this);	 // Start Reader Thread
		runner.start();
	
protected AsyncInputStream(InputStream in)

		this(in, 1024);
	
Methods Summary
public intavailable()

		return reslen;
	
public voidclose()

		try {
			lock.getBusyFlag();
			reslen = 0;		// Clear Buffer
			EOF = true;		// Mark End Of File
			empty.cvBroadcast();	// Alert all Threads
			full.cvBroadcast();
		} finally {
			lock.freeBusyFlag();
		}
	
private bytegetChar()

		try {
			lock.getBusyFlag();
			byte c = result[0];
			System.arraycopy(result, 1, result, 0, --reslen);
			full.cvSignal();
			return c;
		} finally {
			lock.freeBusyFlag();
		}
	
private byte[]getChars(int chars)

		try {
			lock.getBusyFlag();
			byte c[] = new byte[chars];
			System.arraycopy(result, 0, c, 0, chars);
			reslen -= chars;
			System.arraycopy(result, chars, result, 0, reslen);
			full.cvSignal();
			return c;
		} finally {
			lock.freeBusyFlag();
		}
	
public voidmark(int readlimit)

	
public booleanmarkSupported()

		return false;
	
private voidputChar(byte c)

		try {
			lock.getBusyFlag();
			while ((reslen == result.length) && (!EOF)) {
				try {
					full.cvWait();
				} catch (InterruptedException ie) {}
			}
			if (!EOF) {
				result[reslen++] = c;
				empty.cvSignal();
			}
		} finally {
			lock.freeBusyFlag();
		}
	
public intread()

		try {
			lock.getBusyFlag();
			while (reslen == 0) {
				try {
					if (EOF) return(-1);
					if (IOError != null) throw IOError;
					empty.cvWait();
				} catch (InterruptedException e) {}
			}
			return (int) getChar();
		} finally {
			lock.freeBusyFlag();
		}
	
public intread(byte[] b)

		return read(b, 0, b.length);
	
public intread(byte[] b, int off, int len)

		try {
			lock.getBusyFlag();
			while (reslen == 0) {
				try {
					if (EOF) return(-1);
					if (IOError != null) throw IOError;
					empty.cvWait();
				} catch (InterruptedException e) {}
			}

			int sizeread = Math.min(reslen, len);
			byte c[] = getChars(sizeread);
			System.arraycopy(c, 0, b, off, sizeread);
			return(sizeread);
		} finally {
			lock.freeBusyFlag();
		}
	
public voidreset()

	
public voidrun()

		try {
			while (true) {
				int c = in.read();
				try {
					lock.getBusyFlag();
					if ((c == -1) || (EOF)) {
						EOF = true;		// Mark End Of File
						in.close();		// Close Input Source
						return;			// End IO Thread
					} else {
						putChar((byte)c);  // Store the byte read
					}
					if (EOF) {
						in.close();		// Close Input Source
						return;			// End IO Thread
					}
				} finally {
					lock.freeBusyFlag();
				}
			}

		} catch (IOException e) {
			IOError = e;			  //  Store Exception
			return;
		} finally {
			try {
				lock.getBusyFlag();
				empty.cvBroadcast();			  //  Alert all Threads
			} finally {
				lock.freeBusyFlag();
			}
		}
	
public longskip(long n)

		try {
			lock.getBusyFlag();
			int sizeskip = Math.min(reslen, (int) n);
			if (sizeskip > 0) {
				byte c[] = getChars(sizeskip);
			}
			return((long)sizeskip);
		} finally {
			lock.freeBusyFlag();
		}