public class ReadAheadInputStream
extends java.io.InputStream
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.
This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19.
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
activeBuffer |
private java.util.concurrent.locks.Condition |
asyncReadComplete |
private boolean |
endOfStream |
private java.util.concurrent.ExecutorService |
executorService |
private boolean |
isClosed |
private boolean |
isReading |
private boolean |
isUnderlyingInputStreamBeingClosed |
private java.util.concurrent.atomic.AtomicBoolean |
isWaiting |
private static java.lang.ThreadLocal<byte[]> |
oneByte |
private boolean |
readAborted |
private java.nio.ByteBuffer |
readAheadBuffer |
private java.lang.Throwable |
readException |
private boolean |
readInProgress |
private boolean |
shutdownExecutorService |
private java.util.concurrent.locks.ReentrantLock |
stateChangeLock |
private java.io.InputStream |
underlyingInputStream |
Modifier | Constructor and Description |
---|---|
|
ReadAheadInputStream(java.io.InputStream inputStream,
int bufferSizeInBytes)
Creates an instance with the specified buffer size and read-ahead threshold
|
|
ReadAheadInputStream(java.io.InputStream inputStream,
int bufferSizeInBytes,
java.util.concurrent.ExecutorService executorService)
Creates an instance with the specified buffer size and read-ahead threshold
|
private |
ReadAheadInputStream(java.io.InputStream inputStream,
int bufferSizeInBytes,
java.util.concurrent.ExecutorService executorService,
boolean shutdownExecutorService)
Creates an instance with the specified buffer size and read-ahead threshold
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
private void |
checkReadException() |
void |
close() |
private void |
closeUnderlyingInputStreamIfNecessary() |
private boolean |
isEndOfStream() |
private static java.util.concurrent.ExecutorService |
newExecutorService()
Creates a new daemon executor service.
|
private static java.lang.Thread |
newThread(java.lang.Runnable r)
Creates a new daemon thread.
|
int |
read() |
int |
read(byte[] b,
int offset,
int len) |
private void |
readAsync()
Read data from underlyingInputStream to readAheadBuffer asynchronously.
|
private void |
signalAsyncReadComplete() |
long |
skip(long n) |
private long |
skipInternal(long n)
Internal skip function which should be called only from skip().
|
private void |
swapBuffers()
Flips the active and read ahead buffer
|
private void |
waitForAsyncReadComplete() |
private static final java.lang.ThreadLocal<byte[]> oneByte
private final java.util.concurrent.locks.ReentrantLock stateChangeLock
private java.nio.ByteBuffer activeBuffer
private java.nio.ByteBuffer readAheadBuffer
private boolean endOfStream
private boolean readInProgress
private boolean readAborted
private java.lang.Throwable readException
private boolean isClosed
private boolean isUnderlyingInputStreamBeingClosed
private boolean isReading
private final java.util.concurrent.atomic.AtomicBoolean isWaiting
private final java.io.InputStream underlyingInputStream
private final java.util.concurrent.ExecutorService executorService
private final boolean shutdownExecutorService
private final java.util.concurrent.locks.Condition asyncReadComplete
public ReadAheadInputStream(java.io.InputStream inputStream, int bufferSizeInBytes)
inputStream
- The underlying input stream.bufferSizeInBytes
- The buffer size.public ReadAheadInputStream(java.io.InputStream inputStream, int bufferSizeInBytes, java.util.concurrent.ExecutorService executorService)
inputStream
- The underlying input stream.bufferSizeInBytes
- The buffer size.executorService
- An executor service for the read-ahead thread.private ReadAheadInputStream(java.io.InputStream inputStream, int bufferSizeInBytes, java.util.concurrent.ExecutorService executorService, boolean shutdownExecutorService)
inputStream
- The underlying input stream.bufferSizeInBytes
- The buffer size.executorService
- An executor service for the read-ahead thread.shutdownExecutorService
- Whether or not to shutdown the given ExecutorService on close.private static java.util.concurrent.ExecutorService newExecutorService()
private static java.lang.Thread newThread(java.lang.Runnable r)
r
- the thread's runnable.public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
private void checkReadException() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
private void closeUnderlyingInputStreamIfNecessary()
private boolean isEndOfStream()
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int offset, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
private void readAsync() throws java.io.IOException
java.io.IOException
private void signalAsyncReadComplete()
public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
private long skipInternal(long n) throws java.io.IOException
n
- the number of bytes to be skipped.java.io.IOException
private void swapBuffers()
private void waitForAsyncReadComplete() throws java.io.IOException
java.io.IOException