java.lang.Object | |||
↳ | java.io.InputStream | ||
↳ | java.io.FilterInputStream | ||
↳ | java.io.BufferedInputStream |
Wraps an existing InputStream
and buffers the input.
Expensive interaction with the underlying input stream is minimized, since
most (smaller) requests can be satisfied by accessing the buffer alone. The
drawback is that some extra space is required to hold the buffer and that
copying takes place when filling that buffer, but this is usually outweighed
by the performance benefits.
BufferedInputStream buf = new BufferedInputStream(new FileInputStream("file.java"));
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
buf | The buffer containing the current bytes read from the target InputStream. | ||||||||||
count | The total number of bytes inside the byte array buf . | ||||||||||
marklimit | The current limit, which when passed, invalidates the current mark. | ||||||||||
markpos | The currently marked position. | ||||||||||
pos | The current position within the byte array buf . |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
BufferedInputStream , providing in with a buffer
of 8192 bytes. | |||||||||||
Constructs a new
BufferedInputStream , providing in with size bytes
of buffer. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns an estimated number of bytes that can be read or skipped without blocking for more
input.
| |||||||||||
Closes this stream.
| |||||||||||
Sets a mark position in this stream.
| |||||||||||
Indicates whether
BufferedInputStream supports the mark()
and reset() methods. | |||||||||||
Reads a single byte from this stream and returns it as an integer in the
range from 0 to 255.
| |||||||||||
Reads at most
length bytes from this stream and stores them in
byte array buffer starting at offset offset . | |||||||||||
Resets this stream to the last marked location.
| |||||||||||
Skips
amount number of bytes in this stream. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
The buffer containing the current bytes read from the target InputStream.
The current limit, which when passed, invalidates the current mark.
The currently marked position. -1 indicates no mark has been set or the mark has been invalidated.
Constructs a new BufferedInputStream
, providing in
with a buffer
of 8192 bytes.
Warning: passing a null source creates a closed
BufferedInputStream
. All read operations on such a stream will
fail with an IOException.
in | the InputStream the buffer reads from.
|
---|
Constructs a new BufferedInputStream
, providing in
with size
bytes
of buffer.
Warning: passing a null source creates a closed
BufferedInputStream
. All read operations on such a stream will
fail with an IOException.
in | the InputStream the buffer reads from. |
---|---|
size | the size of buffer in bytes. |
Public Methods
public
synchronized
int
available
()
Since: API Level 1
Returns an estimated number of bytes that can be read or skipped without blocking for more
input. This method returns the number of bytes available in the buffer
plus those available in the source stream, but see
available()
for
important caveats.
Returns
the estimated number of bytes available
public
void
close
()
Since: API Level 1
Closes this stream. The source stream is closed and any resources
associated with it are released.
public
synchronized
void
mark
(int readlimit)
Since: API Level 1
Sets a mark position in this stream. The parameter
readlimit
indicates how many bytes can be read before a mark is invalidated.
Calling reset()
will reposition the stream back to the marked
position if readlimit
has not been surpassed. The underlying
buffer may be increased in size to allow readlimit
number of
bytes to be supported.
Parameters
readlimit
| the number of bytes that can be read before the mark is
invalidated. |
---|
public
boolean
markSupported
()
Since: API Level 1
Indicates whether
BufferedInputStream
supports the mark()
and reset()
methods.
Returns
true
for BufferedInputStreams.
public
synchronized
int
read
()
Since: API Level 1
Reads a single byte from this stream and returns it as an integer in the
range from 0 to 255. Returns -1 if the end of the source string has been
reached. If the internal buffer does not contain any available bytes then
it is filled from the source stream and the first byte is returned.
Returns
the byte read or -1 if the end of the source stream has been
reached.
public
synchronized
int
read
(byte[] buffer, int offset, int length)
Since: API Level 1
Reads at most
length
bytes from this stream and stores them in
byte array buffer
starting at offset offset
. Returns the
number of bytes actually read or -1 if no bytes were read and the end of
the stream was encountered. If all the buffered bytes have been used, a
mark has not been set and the requested number of bytes is larger than
the receiver's buffer size, this implementation bypasses the buffer and
simply places the results directly into buffer
.
Parameters
buffer
| the byte array in which to store the bytes read. |
---|---|
offset
| the initial position in |
length
| the maximum number of bytes to store in |
Returns
the number of bytes actually read or -1 if end of stream.
Throws
IndexOutOfBoundsException
| if |
---|---|
IOException
| if the stream is already closed or another IOException
occurs.
|
public
synchronized
void
reset
()
Since: API Level 1
Resets this stream to the last marked location.
Throws
IOException
| if this stream is closed, no mark has been set or the mark is
no longer valid because more than |
---|
public
synchronized
long
skip
(long amount)
Since: API Level 1
Skips
amount
number of bytes in this stream. Subsequent
read()
's will not return these bytes unless reset()
is
used.
Parameters
amount
| the number of bytes to skip. |
---|
Returns
the number of bytes actually skipped.