public final class MemoryMappedFileInputStream
extends java.io.InputStream
InputStream
that utilizes memory mapped files to improve performance. A sliding window of the file is
mapped to memory to avoid mapping the entire file to memory at one time. The size of the sliding buffer is
configurable.
For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data. From the standpoint of performance. it is generally only worth mapping relatively large files into memory.
Note: Use of this class does not necessarily obviate the need to use a BufferedInputStream
. Depending on the
use case, the use of buffering may still further improve performance. For example:
To build an instance, see MemoryMappedFileInputStream.Builder
.
BufferedInputStream s = new BufferedInputStream(new GzipInputStream(
MemoryMappedFileInputStream.builder()
.setPath(path)
.setBufferSize(256 * 1024)
.get()));
should outperform:
new GzipInputStream(new MemoryMappedFileInputStream(path))
GzipInputStream s = new GzipInputStream(
MemoryMappedFileInputStream.builder()
.setPath(path)
.setBufferSize(256 * 1024)
.get());
Modifier and Type | Class and Description |
---|---|
static class |
MemoryMappedFileInputStream.Builder
Builds a new
MemoryMappedFileInputStream instance. |
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
buffer |
private int |
bufferSize |
private java.nio.channels.FileChannel |
channel |
private boolean |
closed |
private static int |
DEFAULT_BUFFER_SIZE
Default size of the sliding memory mapped buffer.
|
private static java.nio.ByteBuffer |
EMPTY_BUFFER |
private long |
nextBufferPosition
The starting position (within the file) of the next sliding buffer.
|
Modifier | Constructor and Description |
---|---|
private |
MemoryMappedFileInputStream(java.nio.file.Path file,
int bufferSize)
Constructs a new instance.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
static MemoryMappedFileInputStream.Builder |
builder()
Constructs a new
MemoryMappedFileInputStream.Builder . |
private void |
cleanBuffer() |
void |
close() |
private void |
ensureOpen() |
(package private) int |
getBufferSize() |
private void |
nextBuffer() |
int |
read() |
int |
read(byte[] b,
int off,
int len) |
long |
skip(long n) |
private static final int DEFAULT_BUFFER_SIZE
private static final java.nio.ByteBuffer EMPTY_BUFFER
private final int bufferSize
private final java.nio.channels.FileChannel channel
private java.nio.ByteBuffer buffer
private boolean closed
private long nextBufferPosition
private MemoryMappedFileInputStream(java.nio.file.Path file, int bufferSize) throws java.io.IOException
file
- The path of the file to open.bufferSize
- Size of the sliding buffer.java.io.IOException
- If an I/O error occurs.public static MemoryMappedFileInputStream.Builder builder()
MemoryMappedFileInputStream.Builder
.MemoryMappedFileInputStream.Builder
.public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
private void cleanBuffer()
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 ensureOpen() throws java.io.IOException
java.io.IOException
int getBufferSize()
private void nextBuffer() throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException