Class MemoryMappedFileInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class MemoryMappedFileInputStream
    extends java.io.InputStream
    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
    • Method Detail

      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException