Class RandomAccessSourceFactory

java.lang.Object
com.itextpdf.io.source.RandomAccessSourceFactory

public final class RandomAccessSourceFactory extends Object
Factory to create IRandomAccessSource objects based on various types of sources
  • Constructor Details

    • RandomAccessSourceFactory

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

    • 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)
    • setExclusivelyLockFile

      public RandomAccessSourceFactory setExclusivelyLockFile (boolean exclusivelyLockFile)
      Configures whether file backed sources are opened with a read-write lock.
      Parameters:
      exclusivelyLockFile - true to request an exclusive lock, false for read-only access
      Returns:
      this factory
    • createSource

      public IRandomAccessSource createSource (byte[] data)
      Creates a IRandomAccessSource based on a byte array
      Parameters:
      data - the byte array
      Returns:
      the newly created IRandomAccessSource
    • createSource

      public IRandomAccessSource createSource (RandomAccessFile raf) throws IOException
      Creates a source that reads from an already open random access file.

      The returned source owns and closes raf.

      Parameters:
      raf - the file to wrap
      Returns:
      a source backed by raf
      Throws:
      IOException - if the file length cannot be read
    • createSource

      public IRandomAccessSource createSource (URL url) throws 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:
      IOException - in case of any I/O error.
    • extractOrCreateSource

      public IRandomAccessSource extractOrCreateSource (InputStream inputStream) throws 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:
      IOException - in case of any I/O error.
    • createSource

      public IRandomAccessSource createSource (InputStream inputStream) throws 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:
      IOException - in case of any I/O error.
    • createBestSource

      public IRandomAccessSource createBestSource (String filename) throws 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:
      IOException - in case of any I/O error
    • createBestSource

      public IRandomAccessSource createBestSource (FileChannel channel) throws 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:
      IOException - in case of any I/O error
    • createRanged

      public IRandomAccessSource createRanged (IRandomAccessSource source, long[] ranges) throws IOException
      Creates a concatenated source from ranges of another source.

      Each adjacent pair in ranges is an offset and length. Closing the returned source closes the supplied source through each of its window views.

      Parameters:
      source - the source from which ranges are exposed
      ranges - offset-length pairs defining the exposed ranges
      Returns:
      a source whose bytes are the requested ranges in order
      Throws:
      IOException - if a source cannot be initialized