Package org.apache.commons.io.input
Class MemoryMappedFileInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.io.input.MemoryMappedFileInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
An
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());
- Since:
- 2.12.0
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteBuffer
private final int
private final FileChannel
private boolean
private static final int
Default size of the sliding memory mapped buffer.private static final ByteBuffer
private long
The starting position (within the file) of the next sliding buffer. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
MemoryMappedFileInputStream
(Path file, int bufferSize) Constructs a new instance. -
Method Summary
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
-
Field Details
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZEDefault size of the sliding memory mapped buffer. We use 256K, equal to 65536 pages (given a 4K page size). Increasing the value beyond the default size will generally not provide any increase in throughput.- See Also:
-
EMPTY_BUFFER
-
bufferSize
private final int bufferSize -
channel
-
buffer
-
closed
private boolean closed -
nextBufferPosition
private long nextBufferPositionThe starting position (within the file) of the next sliding buffer.
-
-
Constructor Details
-
MemoryMappedFileInputStream
Constructs a new instance.- Parameters:
file
- The path of the file to open.bufferSize
- Size of the sliding buffer.- Throws:
IOException
- If an I/O error occurs.
-
-
Method Details
-
builder
Constructs a newMemoryMappedFileInputStream.Builder
.- Returns:
- a new
MemoryMappedFileInputStream.Builder
. - Since:
- 2.12.0
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
cleanBuffer
private void cleanBuffer() -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
ensureOpen
- Throws:
IOException
-
getBufferSize
int getBufferSize() -
nextBuffer
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-