public class LengthClampedInputStream
extends java.io.FilterInputStream
Constructor and Description |
---|
LengthClampedInputStream(java.io.InputStream inputStream,
int length)
Creates a LengthClampedInputStream with the specified
InputStream
and the maximum number of bytes that can be read from the stream. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
caller of a method for this input stream.
|
void |
close()
Closes this input stream and releases any system resources
associated with the stream.
|
void |
mark(int readlimit)
Marks the current position in this input stream.
|
int |
read()
Reads the next byte of data from this input stream.
|
void |
reset()
Repositions this stream to the position at the time the
mark method was last called on this input stream. |
void |
setClampedLength(int clampedLength)
Sets a new clamped length value.
|
public LengthClampedInputStream(java.io.InputStream inputStream, int length)
InputStream
and the maximum number of bytes that can be read from the stream.inputStream
- The input stream to clamp.length
- The maximum number of bytes that can be read before end of stream is reached.java.lang.NullPointerException
- inputStream is null.java.lang.IllegalArgumentException
- length is less than 0.public int read() throws java.io.IOException
int
in the range
0
to 255
. If no byte is available
because the end of the stream has been reached, the value
-1
is returned. This method blocks until input data
is available, the end of the stream is detected, or an exception
is thrown.read
in class java.io.FilterInputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.public int available() throws java.io.IOException
available
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurs.public void mark(int readlimit)
reset
method repositions this stream at
the last marked position so that subsequent reads re-read the same bytes.
The readlimit
argument tells this input stream to
allow that many bytes to be read before the mark position gets
invalidated.
mark
in class java.io.FilterInputStream
readlimit
- the maximum limit of bytes that can be read before
the mark position becomes invalid.public void reset() throws java.io.IOException
mark
method was last called on this input stream.
Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.
reset
in class java.io.FilterInputStream
java.io.IOException
- if the stream has not been marked, if the
mark has been invalidated or marking is not supported.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterInputStream
java.io.IOException
- if an I/O error occurs.public void setClampedLength(int clampedLength)
clampedLength
- The new clamped length value.java.lang.IllegalArgumentException
- clampedLength is less than 0.