Class RandomAccessSourceFactory
java.lang.Object
com.itextpdf.io.source.RandomAccessSourceFactory
Factory to create
IRandomAccessSource objects based on various types of sources-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether the underlying file should have a RW lock on it or just an R lockprivate booleanWhether the full content of the source should be read into memory at constructionprivate static booleanThe default value for the forceRead flagprivate booleanWhetherRandomAccessFileshould be used instead of aFileChannel, where applicable -
Constructor Summary
ConstructorsConstructorDescriptionCreates a factory that will give preference to accessing the underling data source using memory mapped files -
Method Summary
Modifier and TypeMethodDescriptioncreateBestSource(String filename) Creates aIRandomAccessSourcebased on a filename string.createBestSource(FileChannel channel) Creates aIRandomAccessSourcebased on memory mapping a file channel.private IRandomAccessSourcecreateByReadingToMemory(InputStream stream) Creates a newIRandomAccessSourceby reading the specified file/resource into memoryprivate IRandomAccessSourcecreateByReadingToMemory(String filename) Creates a newIRandomAccessSourceby reading the specified file/resource into memorycreateRanged(IRandomAccessSource source, long[] ranges) createSource(byte[] data) Creates aIRandomAccessSourcebased on a byte arraycreateSource(InputStream inputStream) Creates aIRandomAccessSourcebased on anInputStream.createSource(URL url) Creates aIRandomAccessSourcebased on a URL.private static booleanUtility method that determines whether a given java.io.IOException is the result of a failure to map a memory mapped file.extractOrCreateSource(InputStream inputStream) Creates or extracts aIRandomAccessSourcebased on anInputStream.setExclusivelyLockFile(boolean exclusivelyLockFile) setForceRead(boolean forceRead) Determines whether the full content of the source will be read into memorystatic voidsetForceReadDefaultValue(boolean forceRead) Determines the default value for the forceRead flagsetUsePlainRandomAccess(boolean usePlainRandomAccess) Determines whetherRandomAccessFileshould be used as the primary data access mechanism
-
Field Details
-
forceReadDefaultValue
private static boolean forceReadDefaultValueThe default value for the forceRead flag -
forceRead
private boolean forceReadWhether the full content of the source should be read into memory at construction -
usePlainRandomAccess
private boolean usePlainRandomAccessWhetherRandomAccessFileshould be used instead of aFileChannel, where applicable -
exclusivelyLockFile
private boolean exclusivelyLockFileWhether the underlying file should have a RW lock on it or just an R lock
-
-
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
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
Determines whetherRandomAccessFileshould be used as the primary data access mechanism- Parameters:
usePlainRandomAccess- whetherRandomAccessFileshould be used as the primary data access mechanism- Returns:
- this object (this allows chaining of method calls)
-
setExclusivelyLockFile
-
createSource
Creates aIRandomAccessSourcebased on a byte array- Parameters:
data- the byte array- Returns:
- the newly created
IRandomAccessSource
-
createSource
- Throws:
IOException
-
createSource
Creates aIRandomAccessSourcebased on a URL. The data available at the URL is read into memory and used as the source for theIRandomAccessSource- Parameters:
url- the url to read from- Returns:
- the newly created
IRandomAccessSource - Throws:
IOException- in case of any I/O error.
-
extractOrCreateSource
Creates or extracts aIRandomAccessSourcebased on anInputStream.If the InputStream is an instance of
RASInputStreamthen extracts the source from it. Otherwise The full content of the InputStream is read into memory and used as the source for theIRandomAccessSource- Parameters:
inputStream- the stream to read from- Returns:
- the newly created or extracted
IRandomAccessSource - Throws:
IOException- in case of any I/O error.
-
createSource
Creates aIRandomAccessSourcebased on anInputStream.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
Creates aIRandomAccessSourcebased 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 (ifforceReadis true), opened using memory mapped file channel (if usePlainRandomAccess is false), or opened usingRandomAccessFileaccess (if usePlainRandomAccess is true) This call will automatically fail over to usingRandomAccessFileif the memory map operation fails- Parameters:
filename- the name of the file or resource to create theIRandomAccessSourcefor- Returns:
- the newly created
IRandomAccessSource - Throws:
IOException- in case of any I/O error
-
createBestSource
Creates aIRandomAccessSourcebased on memory mapping a file channel. Unless you are explicitly working with aFileChannelalready, it is better to usecreateBestSource(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 theIRandomAccessSourcefor- Returns:
- the newly created
IRandomAccessSource - Throws:
IOException- in case of any I/O error
-
createRanged
public IRandomAccessSource createRanged(IRandomAccessSource source, long[] ranges) throws IOException - Throws:
IOException
-
createByReadingToMemory
Creates a newIRandomAccessSourceby reading the specified file/resource into memory- Parameters:
filename- the name of the resource to read- Returns:
- the newly created
IRandomAccessSource - Throws:
IOException- if reading the underling file or stream fails
-
createByReadingToMemory
Creates a newIRandomAccessSourceby reading the specified file/resource into memory- Parameters:
stream- the name of the resource to read- Returns:
- the newly created
IRandomAccessSource - Throws:
IOException- if reading the underling file or stream fails
-
exceptionIsMapFailureException
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
-