FileDocCategorySizeDatePackage
NonBlockingBufferedInputStream.javaAPI DocApache Axis 1.45924Sat Apr 22 18:57:28 BST 2006org.apache.axis.transport.http

NonBlockingBufferedInputStream

public class NonBlockingBufferedInputStream extends InputStream

Fields Summary
private InputStream
in
private int
remainingContent
private byte[]
buffer
private int
offset
private int
numbytes
Constructors Summary
Methods Summary
public intavailable()
return the number of bytes available to be read without blocking

return
the number of bytes

        if (in == null) return 0;

        // return buffered + available from the stream
        return (numbytes-offset) + in.available();
    
public voidclose()
disassociate from the underlying input stream

        setInputStream(null);
    
public intpeek()
Just like read except byte is not removed from the buffer. the data is buffered for efficiency. Was added to support multiline http headers. ;-)

return
the byte read

        if (in == null) return -1;
        if (offset >= numbytes) refillBuffer();
        if (offset >= numbytes) return -1;
        return buffer[offset] & 0xFF;
    
public intread()
Read a byte from the input stream, blocking if necessary. Internally the data is buffered for efficiency.

return
the byte read

        if (in == null) return -1;
        if (offset >= numbytes) refillBuffer();
        if (offset >= numbytes) return -1;
        return buffer[offset++] & 0xFF;
    
public intread(byte[] dest)
Read bytes from the input stream. This is guaranteed to return at least one byte or throw an exception. When possible, it will return more bytes, up to the length of the array, as long as doing so would not require waiting on bytes from the input stream.

param
dest byte array to read into
return
the number of bytes actually read

        return read(dest, 0, dest.length);
    
public intread(byte[] dest, int off, int len)
Read a specified number of bytes from the input stream. This is guaranteed to return at least one byte or throw an execption. When possible, it will return more bytes, up to the length specified, as long as doing so would not require waiting on bytes from the input stream.

param
dest byte array to read into
param
off starting offset into the byte array
param
len maximum number of bytes to read
return
the number of bytes actually read

        int ready = numbytes - offset;

        if (ready >= len) {
            System.arraycopy(buffer, offset, dest, off, len);
            offset += len;
            return len;
        } else if (ready>0) {
            System.arraycopy(buffer, offset, dest, off, ready);
            offset = numbytes;
            return ready;
        } else {
            if (in == null) return -1;
            refillBuffer();
            if (offset >= numbytes) return -1;
            return read(dest,off,len);
        }
    
private voidrefillBuffer()
Replenish the buffer with data from the input stream. This is guaranteed to read atleast one byte or throw an exception. When possible, it will read up to the length of the buffer the data is buffered for efficiency.

return
the byte read

        if (remainingContent <= 0 || in == null) return;

        // determine number of bytes to read
        numbytes = in.available();
        if (numbytes > remainingContent) numbytes=remainingContent;
        if (numbytes > buffer.length) numbytes=buffer.length;
        if (numbytes <= 0) numbytes = 1;

        // actually attempt to read those bytes
        numbytes = in.read(buffer, 0, numbytes);

        // update internal state to reflect this read
        remainingContent -= numbytes;
        offset = 0;
    
public voidsetContentLength(int value)
set the maximum number of bytes allowed to be read from this input stream.

param
value the Content Length

        if (in != null) this.remainingContent = value - (numbytes-offset);
    
public voidsetInputStream(java.io.InputStream in)
set the input stream to be used for subsequent reads

param
in the InputStream

   // number of valid bytes in this buffer

                       
         
        this.in = in;
        numbytes = 0;
        offset = 0;
        remainingContent = (in==null)? 0 : Integer.MAX_VALUE;
    
public intskip(int len)
skip over (and discard) a specified number of bytes in this input stream

param
len the number of bytes to be skipped
return
the action number of bytes skipped

        int count = 0;
        while (len-->0 && read()>=0) count++;
        return count;