Package org.apache.commons.io.input
Class ReadAheadInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.ReadAheadInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Implements
InputStream to asynchronously read ahead from an underlying input stream when a specified amount of data has been read from the current
buffer. It does so by maintaining two buffers: an active buffer and a read ahead buffer. The active buffer contains data which should be returned when a
read() call is issued. The read ahead buffer is used to asynchronously read from the underlying input stream. When the current active buffer is exhausted, we
flip the two buffers so that we can start reading from the read ahead buffer without being blocked by disk I/O.
To build an instance, use ReadAheadInputStream.Builder.
This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19.
- Since:
- 2.9.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteBufferprivate final Conditionprivate static final ThreadLocal<byte[]> private booleanprivate final ExecutorServiceprivate booleanprivate booleanprivate booleanprivate final AtomicBooleanprivate booleanprivate ByteBufferprivate Throwableprivate booleanprivate final booleanprivate final ReentrantLockFields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsModifierConstructorDescriptionReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes) Deprecated.ReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes, ExecutorService executorService) Deprecated.privateReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes, ExecutorService executorService, boolean shutdownExecutorService) Constructs an instance with the specified buffer size and read-ahead threshold -
Method Summary
Modifier and TypeMethodDescriptionintstatic ReadAheadInputStream.Builderbuilder()Constructs a newReadAheadInputStream.Builder.private voidvoidclose()private voidprivate booleanprivate static ThreadConstructs a new daemon thread.private static ExecutorServiceConstructs a new daemon executor service.intread()intread(byte[] b, int offset, int len) private voidRead data from underlyingInputStream to readAheadBuffer asynchronously.private voidlongskip(long n) private longskipInternal(long n) Internal skip function which should be called only from skip().private voidFlips the active and read ahead bufferprivate voidMethods inherited from class java.io.FilterInputStream
mark, markSupported, read, reset
-
Field Details
-
BYTE_ARRAY_1
-
stateChangeLock
-
activeBuffer
-
readAheadBuffer
-
endOfStream
private boolean endOfStream -
readInProgress
private boolean readInProgress -
readAborted
private boolean readAborted -
readException
-
isClosed
private boolean isClosed -
isUnderlyingInputStreamBeingClosed
private boolean isUnderlyingInputStreamBeingClosed -
isReading
private boolean isReading -
isWaiting
-
executorService
-
shutdownExecutorService
private final boolean shutdownExecutorService -
asyncReadComplete
-
-
Constructor Details
-
ReadAheadInputStream
Deprecated.Constructs an instance with the specified buffer size and read-ahead threshold- Parameters:
inputStream- The underlying input stream.bufferSizeInBytes- The buffer size.
-
ReadAheadInputStream
@Deprecated public ReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes, ExecutorService executorService) Deprecated.Constructs an instance with the specified buffer size and read-ahead threshold- Parameters:
inputStream- The underlying input stream.bufferSizeInBytes- The buffer size.executorService- An executor service for the read-ahead thread.
-
ReadAheadInputStream
private ReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes, ExecutorService executorService, boolean shutdownExecutorService) Constructs an instance with the specified buffer size and read-ahead threshold- Parameters:
inputStream- The underlying input stream.bufferSizeInBytes- The buffer size.executorService- An executor service for the read-ahead thread.shutdownExecutorService- Whether or not to shut down the given ExecutorService on close.
-
-
Method Details
-
builder
Constructs a newReadAheadInputStream.Builder.- Returns:
- a new
ReadAheadInputStream.Builder. - Since:
- 2.12.0
-
newDaemonThread
Constructs a new daemon thread.- Parameters:
r- the thread's runnable.- Returns:
- a new daemon thread.
-
newExecutorService
Constructs a new daemon executor service.- Returns:
- a new daemon executor service.
-
available
- Overrides:
availablein classFilterInputStream- Throws:
IOException
-
checkReadException
- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException
-
closeUnderlyingInputStreamIfNecessary
private void closeUnderlyingInputStreamIfNecessary() -
isEndOfStream
private boolean isEndOfStream() -
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
readAsync
Read data from underlyingInputStream to readAheadBuffer asynchronously.- Throws:
IOException- if an I/O error occurs.
-
signalAsyncReadComplete
private void signalAsyncReadComplete() -
skip
- Overrides:
skipin classFilterInputStream- Throws:
IOException
-
skipInternal
Internal skip function which should be called only from skip(). The assumption is that the stateChangeLock is already acquired in the caller before calling this function.- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-
swapBuffers
private void swapBuffers()Flips the active and read ahead buffer -
waitForAsyncReadComplete
- Throws:
IOException
-
builder(),ReadAheadInputStream.Builder, andReadAheadInputStream.Builder.get()