Package org.apache.commons.io.input
Class MemoryMappedFileInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.AbstractInputStream
-
- org.apache.commons.io.input.MemoryMappedFileInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class MemoryMappedFileInputStream extends AbstractInputStream
AnInputStreamthat 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, use
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
- See Also:
MemoryMappedFileInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMemoryMappedFileInputStream.BuilderBuilds a newMemoryMappedFileInputStream.
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBufferbufferprivate intbufferSizeprivate java.nio.channels.FileChannelchannelprivate static intDEFAULT_BUFFER_SIZEDefault size of the sliding memory mapped buffer.private static java.nio.ByteBufferEMPTY_BUFFERprivate longnextBufferPositionThe starting position (within the file) of the next sliding buffer.
-
Constructor Summary
Constructors Modifier Constructor Description privateMemoryMappedFileInputStream(MemoryMappedFileInputStream.Builder builder)Constructs a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()static MemoryMappedFileInputStream.Builderbuilder()Constructs a newMemoryMappedFileInputStream.Builder.private voidcleanBuffer()voidclose()(package private) intgetBufferSize()private voidnextBuffer()intread()intread(byte[] b, int off, int len)longskip(long n)-
Methods inherited from class org.apache.commons.io.input.AbstractInputStream
checkOpen, isClosed, setClosed
-
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE
Default 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:
- Constant Field Values
-
EMPTY_BUFFER
private static final java.nio.ByteBuffer EMPTY_BUFFER
-
bufferSize
private final int bufferSize
-
channel
private final java.nio.channels.FileChannel channel
-
buffer
private java.nio.ByteBuffer buffer
-
nextBufferPosition
private long nextBufferPosition
The starting position (within the file) of the next sliding buffer.
-
-
Constructor Detail
-
MemoryMappedFileInputStream
private MemoryMappedFileInputStream(MemoryMappedFileInputStream.Builder builder) throws java.io.IOException
Constructs a new instance.- Parameters:
builder- The builder.- Throws:
java.io.IOException- If an I/O error occurs.
-
-
Method Detail
-
builder
public static MemoryMappedFileInputStream.Builder builder()
Constructs a newMemoryMappedFileInputStream.Builder.- Returns:
- a new
MemoryMappedFileInputStream.Builder. - Since:
- 2.12.0
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
cleanBuffer
private void cleanBuffer()
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classAbstractInputStream- Throws:
java.io.IOException
-
getBufferSize
int getBufferSize()
-
nextBuffer
private void nextBuffer() throws java.io.IOException- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
-