Package java.io
Class BufferedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.BufferedInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class BufferedInputStream extends FilterInputStream
Wraps an existingInputStreamand 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. A typical application pattern for the class looks like this:BufferedInputStream buf = new BufferedInputStream(new FileInputStream("file.java"));- See Also:
BufferedOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe buffer containing the current bytes read from the target InputStream.protected intcountThe total number of bytes inside the byte arraybuf.static intDEFAULT_BUFFER_SIZEThe default buffer size if it is not specified.protected intmarklimitThe current limit, which when passed, invalidates the current mark.protected intmarkposThe currently marked position. -1 indicates no mark has been set or the mark has been invalidated.protected intposThe current position within the byte arraybuf.-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description BufferedInputStream(InputStream in)Constructs a newBufferedInputStream, providinginwith a buffer of 8192 bytes.BufferedInputStream(InputStream in, int size)Constructs a newBufferedInputStream, providinginwithsizebytes of buffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for more input.voidclose()Closes this stream.voidmark(int readlimit)Sets a mark position in this stream.booleanmarkSupported()Indicates whetherBufferedInputStreamsupports themark()andreset()methods.intread()Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.voidreset()Resets this stream to the last marked location.longskip(long byteCount)SkipsbyteCountbytes in this stream.-
Methods inherited from class java.io.InputStream
read
-
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size if it is not specified.- See Also:
- Constant Field Values
-
buf
protected volatile byte[] buf
The buffer containing the current bytes read from the target InputStream.
-
count
protected int count
The total number of bytes inside the byte arraybuf.
-
marklimit
protected int marklimit
The current limit, which when passed, invalidates the current mark.
-
markpos
protected int markpos
The currently marked position. -1 indicates no mark has been set or the mark has been invalidated.
-
pos
protected int pos
The current position within the byte arraybuf.
-
-
Constructor Detail
-
BufferedInputStream
public BufferedInputStream(InputStream in)
Constructs a newBufferedInputStream, providinginwith 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.- Parameters:
in- theInputStreamthe buffer reads from.
-
BufferedInputStream
public BufferedInputStream(InputStream in, int size)
Constructs a newBufferedInputStream, providinginwithsizebytes of buffer.Warning: passing a null source creates a closed
BufferedInputStream. All read operations on such a stream will fail with an IOException.- Parameters:
in- theInputStreamthe buffer reads from.size- the size of buffer in bytes.- Throws:
IllegalArgumentException- ifsize <= 0.
-
-
Method Detail
-
available
public int available() throws IOExceptionReturns 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 seeInputStream.available()for important caveats.- Overrides:
availablein classFilterInputStream- Returns:
- the estimated number of bytes available
- Throws:
IOException- if this stream is closed or an error occurs
-
close
public void close() throws IOExceptionCloses this stream. The source stream is closed and any resources associated with it are released.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException- if an error occurs while closing this stream.
-
mark
public void mark(int readlimit)
Sets a mark position in this stream. The parameterreadlimitindicates how many bytes can be read before a mark is invalidated. Callingreset()will reposition the stream back to the marked position ifreadlimithas not been surpassed. The underlying buffer may be increased in size to allowreadlimitnumber of bytes to be supported.- Overrides:
markin classFilterInputStream- Parameters:
readlimit- the number of bytes that can be read before the mark is invalidated.- See Also:
reset()
-
markSupported
public boolean markSupported()
Indicates whetherBufferedInputStreamsupports themark()andreset()methods.- Overrides:
markSupportedin classFilterInputStream- Returns:
truefor BufferedInputStreams.- See Also:
mark(int),reset()
-
read
public int read() throws IOExceptionReads 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.- Overrides:
readin classFilterInputStream- Returns:
- the byte read or -1 if the end of the source stream has been reached.
- Throws:
IOException- if this stream is closed or another IOException occurs.
-
read
public int read(byte[] buffer, int byteOffset, int byteCount) throws IOExceptionDescription copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Overrides:
readin classFilterInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
reset
public void reset() throws IOExceptionResets this stream to the last marked location.- Overrides:
resetin classFilterInputStream- Throws:
IOException- if this stream is closed, no mark has been set or the mark is no longer valid because more thanreadlimitbytes have been read since setting the mark.- See Also:
mark(int)
-
skip
public long skip(long byteCount) throws IOExceptionSkipsbyteCountbytes in this stream. Subsequent calls toreadwill not return these bytes unlessresetis used.- Overrides:
skipin classFilterInputStream- Parameters:
byteCount- the number of bytes to skip.skipdoes nothing and returns 0 ifbyteCountis less than zero.- Returns:
- the number of bytes actually skipped.
- Throws:
IOException- if this stream is closed or another IOException occurs.- See Also:
FilterInputStream.mark(int),FilterInputStream.reset()
-
-