public final class MemoryMappedFileInputStream extends 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 | Method and Description |
---|---|
int |
available() |
static MemoryMappedFileInputStream.Builder |
builder()
Constructs a new
MemoryMappedFileInputStream.Builder . |
void |
close() |
int |
read() |
int |
read(byte[] b,
int off,
int len) |
long |
skip(long n) |
mark, markSupported, read, reset
public static MemoryMappedFileInputStream.Builder builder()
MemoryMappedFileInputStream.Builder
.MemoryMappedFileInputStream.Builder
.public int available() throws IOException
available
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public long skip(long n) throws IOException
skip
in class InputStream
IOException
Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.