Class RecyclableBufferedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class RecyclableBufferedInputStream extends java.io.FilterInputStreamWraps 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"));
-
-
Constructor Summary
Constructors Constructor Description RecyclableBufferedInputStream(java.io.InputStream in, ArrayPool byteArrayPool)
-
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.voidfixMarkLimit()Reduces the mark limit to match the current buffer length to prevent the buffer from continuing to increase in size.voidmark(int readlimit)Sets a mark position in this stream.booleanmarkSupported()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 offset, int byteCount)Reads at mostbyteCountbytes from this stream and stores them in byte arraybufferstarting at offsetoffset.voidrelease()voidreset()Resets this stream to the last marked location.longskip(long byteCount)SkipsbyteCountbytes in this stream.
-
-
-
Constructor Detail
-
RecyclableBufferedInputStream
public RecyclableBufferedInputStream(@NonNull java.io.InputStream in, @NonNull ArrayPool byteArrayPool)
-
-
Method Detail
-
available
public int available() throws java.io.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 classjava.io.FilterInputStream- Returns:
- the estimated number of bytes available
- Throws:
java.io.IOException- if this stream is closed or an error occurs
-
fixMarkLimit
public void fixMarkLimit()
Reduces the mark limit to match the current buffer length to prevent the buffer from continuing to increase in size.Subsequent calls to
mark(int)will be obeyed and may cause the buffer size to increase.
-
release
public void release()
-
close
public void close() throws java.io.IOExceptionCloses this stream. The source stream is closed and any resources associated with it are released.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.FilterInputStream- Throws:
java.io.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 classjava.io.FilterInputStream- Parameters:
readlimit- the number of bytes that can be read before the mark is invalidated.- See Also:
reset()
-
markSupported
public boolean markSupported()
-
read
public int read() throws java.io.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 classjava.io.FilterInputStream- Returns:
- the byte read or -1 if the end of the source stream has been reached.
- Throws:
java.io.IOException- if this stream is closed or another IOException occurs.
-
read
public int read(@NonNull byte[] buffer, int offset, int byteCount) throws java.io.IOExceptionReads at mostbyteCountbytes from this stream and stores them in byte arraybufferstarting at offsetoffset. 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 put 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 intobuffer.- Overrides:
readin classjava.io.FilterInputStream- Parameters:
buffer- the byte array in which to store the bytes read.- Returns:
- the number of bytes actually read or -1 if end of stream.
- Throws:
java.lang.IndexOutOfBoundsException- ifoffset < 0orbyteCount < 0, or ifoffset + byteCountis greater than the size ofbuffer.java.io.IOException- if the stream is already closed or another IOException occurs.
-
reset
public void reset() throws java.io.IOExceptionResets this stream to the last marked location.- Overrides:
resetin classjava.io.FilterInputStream- Throws:
java.io.IOException- if this stream is closed, no mark has been put 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 java.io.IOExceptionSkipsbyteCountbytes in this stream. Subsequent calls toread()will not return these bytes unlessreset()is used.- Overrides:
skipin classjava.io.FilterInputStream- Parameters:
byteCount- the number of bytes to skip. This method does nothing and returns 0 ifbyteCountis less than zero.- Returns:
- the number of bytes actually skipped.
- Throws:
java.io.IOException- if this stream is closed or another IOException occurs.
-
-