FileDocCategorySizeDatePackage
AsyncInputStream.javaAPI DocExample4386Tue Jan 28 17:15:26 GMT 1997None

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
Constructors Summary
protected AsyncInputStream(InputStream in, int bufsize)

        super(in);
        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 synchronized intavailable()

        return reslen;
    
public synchronized voidclose()

        reslen = 0;      // Clear Buffer
        EOF = true;      // Mark End Of File
        notifyAll();     // Alert all Threads
    
private synchronized bytegetChar()

        byte c = result[0];
        System.arraycopy(result, 1, result, 0, --reslen);
        return c;
    
private synchronized byte[]getChars(int chars)

        byte c[] = new byte[chars];
        System.arraycopy(c, 0, result, 0, chars);
        reslen -= chars;
        System.arraycopy(result, chars, result, 0, reslen);
        return c;
    
public synchronized voidmark(int readlimit)

    
public booleanmarkSupported()

        return false;
    
private synchronized voidputChar(byte c)

        if (reslen < result.length) {
            result[reslen++] = c;
            notify();
        }
    
public synchronized intread()

        while (reslen == 0) {
            try {
                if (EOF) return(-1);
                if (IOError != null) throw IOError;
                wait();
            } catch (InterruptedException e) {}
        }
        return (int) getChar();
    
public synchronized intread(byte[] b)

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

        while (reslen == 0) {
            try {
                if (EOF) return(-1);
                if (IOError != null) throw IOError;
                wait();
            } catch (InterruptedException e) {}
        }

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

    
public voidrun()

        try {
            while (true) {
                int c = in.read();
                synchronized (this) {
                    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
                    }
                }
            }

        } catch (IOException e) {
            synchronized (this) {
                IOError = e;              // Store Exception
            }
            return;
        } finally {
            synchronized (this) {
                notifyAll();              // Alert all Threads
            }
        }
    
public synchronized longskip(long n)

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