FileDocCategorySizeDatePackage
LimitedLengthInputStream.javaAPI DocAndroid 5.1 API2442Thu Mar 12 22:22:10 GMT 2015android.content.pm

LimitedLengthInputStream

public class LimitedLengthInputStream extends FilterInputStream
A class that limits the amount of data that is read from an InputStream. When the specified length is reached, the stream returns an EOF even if the underlying stream still has more data.
hide

Fields Summary
private final long
mEnd
The end of the stream where we don't want to allow more data to be read.
private long
mOffset
Current offset in the stream.
Constructors Summary
public LimitedLengthInputStream(InputStream in, long offset, long length)

param
in underlying stream to wrap
param
offset offset into stream where data starts
param
length length of data at offset
throws
IOException if an error occurred with the underlying stream

        super(in);

        if (in == null) {
            throw new IOException("in == null");
        }

        if (offset < 0) {
            throw new IOException("offset < 0");
        }

        if (length < 0) {
            throw new IOException("length < 0");
        }

        if (length > Long.MAX_VALUE - offset) {
            throw new IOException("offset + length > Long.MAX_VALUE");
        }

        mEnd = offset + length;

        skip(offset);
        mOffset = offset;
    
Methods Summary
public synchronized intread()

        if (mOffset >= mEnd) {
            return -1;
        }

        mOffset++;
        return super.read();
    
public intread(byte[] buffer, int offset, int byteCount)

        if (mOffset >= mEnd) {
            return -1;
        }

        final int arrayLength = buffer.length;
        Arrays.checkOffsetAndCount(arrayLength, offset, byteCount);

        if (mOffset > Long.MAX_VALUE - byteCount) {
            throw new IOException("offset out of bounds: " + mOffset + " + " + byteCount);
        }

        if (mOffset + byteCount > mEnd) {
            byteCount = (int) (mEnd - mOffset);
        }

        final int numRead = super.read(buffer, offset, byteCount);
        mOffset += numRead;

        return numRead;
    
public intread(byte[] buffer)

        return read(buffer, 0, buffer.length);