Class BufferedFileChannelInputStream

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

    public final class BufferedFileChannelInputStream
    extends java.io.InputStream
    InputStream implementation which uses direct buffer to read a file to avoid extra copy of data between Java and native memory which happens when using BufferedInputStream. Unfortunately, this is not something already available in JDK, sun.nio.ch.ChannelInputStream supports reading a file using NIO, but does not support buffering.

    This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19 where it was called NioBufferedFileInputStream.

    Since:
    2.9.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.ByteBuffer byteBuffer  
      private static java.lang.Class<?> DIRECT_BUFFER_CLASS  
      private java.nio.channels.FileChannel fileChannel  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      private void clean​(java.nio.ByteBuffer buffer)
      Attempts to clean up a ByteBuffer if it is direct or memory-mapped.
      private void cleanDirectBuffer​(java.nio.ByteBuffer buffer)
      In Java 8, the type of DirectBuffer.cleaner() was sun.misc.Cleaner, and it was possible to access the method sun.misc.Cleaner.clean() to invoke it.
      void close()  
      private static java.lang.Class<?> getDirectBufferClass()  
      private static boolean isDirectBuffer​(java.lang.Object object)  
      int read()  
      int read​(byte[] b, int offset, int len)  
      private boolean refill()
      Checks whether data is left to be read from the input stream.
      long skip​(long n)  
      private long skipFromFileChannel​(long n)  
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • byteBuffer

        private final java.nio.ByteBuffer byteBuffer
      • fileChannel

        private final java.nio.channels.FileChannel fileChannel
      • DIRECT_BUFFER_CLASS

        private static final java.lang.Class<?> DIRECT_BUFFER_CLASS
    • Constructor Detail

      • BufferedFileChannelInputStream

        public BufferedFileChannelInputStream​(java.io.File file)
                                       throws java.io.IOException
        Constructs a new instance for the given File.
        Parameters:
        file - The file to stream.
        Throws:
        java.io.IOException - If an I/O error occurs
      • BufferedFileChannelInputStream

        public BufferedFileChannelInputStream​(java.io.File file,
                                              int bufferSizeInBytes)
                                       throws java.io.IOException
        Constructs a new instance for the given File and buffer size.
        Parameters:
        file - The file to stream.
        bufferSizeInBytes - buffer size.
        Throws:
        java.io.IOException - If an I/O error occurs
      • BufferedFileChannelInputStream

        public BufferedFileChannelInputStream​(java.nio.file.Path path)
                                       throws java.io.IOException
        Constructs a new instance for the given Path.
        Parameters:
        path - The path to stream.
        Throws:
        java.io.IOException - If an I/O error occurs
      • BufferedFileChannelInputStream

        public BufferedFileChannelInputStream​(java.nio.file.Path path,
                                              int bufferSizeInBytes)
                                       throws java.io.IOException
        Constructs a new instance for the given Path and buffer size.
        Parameters:
        path - The path to stream.
        bufferSizeInBytes - buffer size.
        Throws:
        java.io.IOException - If an I/O error occurs
    • Method Detail

      • getDirectBufferClass

        private static java.lang.Class<?> getDirectBufferClass()
      • isDirectBuffer

        private static boolean isDirectBuffer​(java.lang.Object object)
      • available

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

        private void clean​(java.nio.ByteBuffer buffer)
        Attempts to clean up a ByteBuffer if it is direct or memory-mapped. This uses an *unsafe* Sun API that will cause errors if one attempts to read from the disposed buffer. However, neither the bytes allocated to direct buffers nor file descriptors opened for memory-mapped buffers put pressure on the garbage collector. Waiting for garbage collection may lead to the depletion of off-heap memory or huge numbers of open files. There's unfortunately no standard API to manually dispose of these kinds of buffers.
        Parameters:
        buffer - the buffer to clean.
      • cleanDirectBuffer

        private void cleanDirectBuffer​(java.nio.ByteBuffer buffer)
        In Java 8, the type of DirectBuffer.cleaner() was sun.misc.Cleaner, and it was possible to access the method sun.misc.Cleaner.clean() to invoke it. The type changed to jdk.internal.ref.Cleaner in later JDKs, and the .clean() method is not accessible even with reflection. However sun.misc.Unsafe added a invokeCleaner() method in JDK 9+ and this is still accessible with reflection.
        Parameters:
        buffer - the buffer to clean. must be a DirectBuffer.
      • 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 offset,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • refill

        private boolean refill()
                        throws java.io.IOException
        Checks whether data is left to be read from the input stream.
        Returns:
        true if data is left, false otherwise
        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
      • skipFromFileChannel

        private long skipFromFileChannel​(long n)
                                  throws java.io.IOException
        Throws:
        java.io.IOException