LimitedLengthInputStreampublic 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. |
Fields Summary |
---|
private final long | mEndThe end of the stream where we don't want to allow more data to be read. | private long | mOffsetCurrent offset in the stream. |
Constructors Summary |
---|
public LimitedLengthInputStream(InputStream in, long offset, long length)
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 int | read()
if (mOffset >= mEnd) {
return -1;
}
mOffset++;
return super.read();
| public int | read(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 int | read(byte[] buffer)
return read(buffer, 0, buffer.length);
|
|