Class MappedChannelRandomAccessSource

  • All Implemented Interfaces:
    RandomAccessSource

    class MappedChannelRandomAccessSource
    extends java.lang.Object
    implements RandomAccessSource
    A RandomAccessSource that represents a memory mapped section of an underlying FileChannel. This source can be closed and will automatically re-open as needed. This class is an internal implementation detail of the FileChannelRandomAccessSource class and shouldn't be used by general iText users.
    Since:
    5.3.5
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.channels.FileChannel channel
      The underlying channel
      private long length
      The number of bytes this source maps to
      private long offset
      The offset into the channel that this source maps to
      private ByteBufferRandomAccessSource source
      If the map is active, the actual map.
    • Constructor Summary

      Constructors 
      Constructor Description
      MappedChannelRandomAccessSource​(java.nio.channels.FileChannel channel, long offset, long length)
      Create a new source based on the channel.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this source.
      private static boolean exceptionIsMapFailureException​(java.io.IOException e)
      Utility method that determines whether a given IOException is the result of a failure to map a memory mapped file.
      int get​(long position)
      Gets a byte at the specified position
      int get​(long position, byte[] bytes, int off, int len)
      Gets an array at the specified position.
      long length()
      (package private) void open()
      Map the region of the channel
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • channel

        private final java.nio.channels.FileChannel channel
        The underlying channel
      • offset

        private final long offset
        The offset into the channel that this source maps to
      • length

        private final long length
        The number of bytes this source maps to
    • Constructor Detail

      • MappedChannelRandomAccessSource

        public MappedChannelRandomAccessSource​(java.nio.channels.FileChannel channel,
                                               long offset,
                                               long length)
        Create a new source based on the channel. Mapping will not occur until data is actually read.
        Parameters:
        channel - the underlying channel
        offset - the offset of the map
        length - the length of the map
    • Method Detail

      • open

        void open()
           throws java.io.IOException
        Map the region of the channel
        Throws:
        java.io.IOException - if there is a problem with creating the map
      • exceptionIsMapFailureException

        private static boolean exceptionIsMapFailureException​(java.io.IOException e)
        Utility method that determines whether a given IOException is the result of a failure to map a memory mapped file. It would be better if the runtime provided a special exception for this case, but it doesn't, so we have to rely on parsing the exception message.
        Parameters:
        e - the exception to check
        Returns:
        true if the exception was the result of a failure to map a memory mapped file
      • get

        public int get​(long position)
                throws java.io.IOException
        Gets a byte at the specified position
        Specified by:
        get in interface RandomAccessSource
        Returns:
        the byte, or -1 if EOF is reached
        Throws:
        java.io.IOException
      • get

        public int get​(long position,
                       byte[] bytes,
                       int off,
                       int len)
                throws java.io.IOException
        Gets an array at the specified position. If the number of bytes requested cannot be read, the bytes that can be read will be placed in bytes and the number actually read will be returned.
        Specified by:
        get in interface RandomAccessSource
        Parameters:
        position - the position in the RandomAccessSource to read from
        bytes - output buffer
        off - offset into the output buffer where results will be placed
        len - the number of bytes to read
        Returns:
        the number of bytes actually read, or -1 if the file is at EOF
        Throws:
        java.io.IOException
      • length

        public long length()
        Specified by:
        length in interface RandomAccessSource
        Returns:
        the length of this source
      • close

        public void close()
                   throws java.io.IOException
        Closes this source. The underlying data structure or source (if any) will also be closed
        Specified by:
        close in interface RandomAccessSource
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object