Class RandomAccessSourceFactory


  • public final class RandomAccessSourceFactory
    extends java.lang.Object
    Factory to create IRandomAccessSource objects based on various types of sources
    • Field Detail

      • forceReadDefaultValue

        private static boolean forceReadDefaultValue
        The default value for the forceRead flag
      • forceRead

        private boolean forceRead
        Whether the full content of the source should be read into memory at construction
      • usePlainRandomAccess

        private boolean usePlainRandomAccess
        Whether RandomAccessFile should be used instead of a FileChannel, where applicable
      • exclusivelyLockFile

        private boolean exclusivelyLockFile
        Whether the underlying file should have a RW lock on it or just an R lock
    • Constructor Detail

      • RandomAccessSourceFactory

        public RandomAccessSourceFactory()
        Creates a factory that will give preference to accessing the underling data source using memory mapped files
    • Method Detail

      • setForceReadDefaultValue

        public static void setForceReadDefaultValue​(boolean forceRead)
        Determines the default value for the forceRead flag
        Parameters:
        forceRead - true if by default the full content will be read, false otherwise
      • setForceRead

        public RandomAccessSourceFactory setForceRead​(boolean forceRead)
        Determines whether the full content of the source will be read into memory
        Parameters:
        forceRead - true if the full content will be read, false otherwise
        Returns:
        this object (this allows chaining of method calls)
      • setUsePlainRandomAccess

        public RandomAccessSourceFactory setUsePlainRandomAccess​(boolean usePlainRandomAccess)
        Determines whether RandomAccessFile should be used as the primary data access mechanism
        Parameters:
        usePlainRandomAccess - whether RandomAccessFile should be used as the primary data access mechanism
        Returns:
        this object (this allows chaining of method calls)
      • createSource

        public IRandomAccessSource createSource​(java.io.RandomAccessFile raf)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • createSource

        public IRandomAccessSource createSource​(java.net.URL url)
                                         throws java.io.IOException
        Creates a IRandomAccessSource based on a URL. The data available at the URL is read into memory and used as the source for the IRandomAccessSource
        Parameters:
        url - the url to read from
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - in case of any I/O error.
      • extractOrCreateSource

        public IRandomAccessSource extractOrCreateSource​(java.io.InputStream inputStream)
                                                  throws java.io.IOException
        Creates or extracts a IRandomAccessSource based on an InputStream.

        If the InputStream is an instance of RASInputStream then extracts the source from it. Otherwise The full content of the InputStream is read into memory and used as the source for the IRandomAccessSource

        Parameters:
        inputStream - the stream to read from
        Returns:
        the newly created or extracted IRandomAccessSource
        Throws:
        java.io.IOException - in case of any I/O error.
      • createSource

        public IRandomAccessSource createSource​(java.io.InputStream inputStream)
                                         throws java.io.IOException
        Creates a IRandomAccessSource based on an InputStream.

        The full content of the InputStream is read into memory and used as the source for the IRandomAccessSource

        Parameters:
        inputStream - the stream to read from
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - in case of any I/O error.
      • createBestSource

        public IRandomAccessSource createBestSource​(java.lang.String filename)
                                             throws java.io.IOException
        Creates a IRandomAccessSource based on a filename string. If the filename describes a URL, a URL based source is created If the filename describes a file on disk, the contents may be read into memory (if forceRead is true), opened using memory mapped file channel (if usePlainRandomAccess is false), or opened using RandomAccessFile access (if usePlainRandomAccess is true) This call will automatically fail over to using RandomAccessFile if the memory map operation fails
        Parameters:
        filename - the name of the file or resource to create the IRandomAccessSource for
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - in case of any I/O error
      • createBestSource

        public IRandomAccessSource createBestSource​(java.nio.channels.FileChannel channel)
                                             throws java.io.IOException
        Creates a IRandomAccessSource based on memory mapping a file channel. Unless you are explicitly working with a FileChannel already, it is better to use createBestSource(String). If the file is large, it will be opened using a paging strategy.
        Parameters:
        channel - the name of the file or resource to create the IRandomAccessSource for
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - in case of any I/O error
      • createByReadingToMemory

        private IRandomAccessSource createByReadingToMemory​(java.lang.String filename)
                                                     throws java.io.IOException
        Creates a new IRandomAccessSource by reading the specified file/resource into memory
        Parameters:
        filename - the name of the resource to read
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - if reading the underling file or stream fails
      • createByReadingToMemory

        private IRandomAccessSource createByReadingToMemory​(java.io.InputStream stream)
                                                     throws java.io.IOException
        Creates a new IRandomAccessSource by reading the specified file/resource into memory
        Parameters:
        stream - the name of the resource to read
        Returns:
        the newly created IRandomAccessSource
        Throws:
        java.io.IOException - if reading the underling file or stream fails
      • exceptionIsMapFailureException

        private static boolean exceptionIsMapFailureException​(java.io.IOException e)
        Utility method that determines whether a given java.io.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