Class PagedChannelRandomAccessSource
java.lang.Object
com.itextpdf.text.io.GroupedRandomAccessSource
com.itextpdf.text.io.PagedChannelRandomAccessSource
- All Implemented Interfaces:
RandomAccessSource
class PagedChannelRandomAccessSource
extends GroupedRandomAccessSource
implements RandomAccessSource
A RandomAccessSource that is based on an underlying
FileChannel. The channel is mapped into memory using a paging scheme to allow for efficient reads of very large files.
As an implementation detail, we use GroupedRandomAccessSource functionality, but override to make determination of the underlying
mapped page more efficient - and to close each page as another is opened- Since:
- 5.3.5
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intThe size of each of the buffers to use when mapping files into memory.private final FileChannelThe channel this source is based onstatic final intstatic final intprivate final PagedChannelRandomAccessSource.MRU<RandomAccessSource> Most recently used list used to hold a number of mapped pages open at a time -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newPagedChannelRandomAccessSourcebased on the specified FileChannel, with a default buffer configuration.PagedChannelRandomAccessSource(FileChannel channel, int totalBufferSize, int maxOpenBuffers) Constructs a newPagedChannelRandomAccessSourcebased on the specified FileChannel, with a specific buffer size -
Method Summary
Modifier and TypeMethodDescriptionprivate static RandomAccessSource[]buildSources(FileChannel channel, int bufferSize) Constructs a set ofMappedChannelRandomAccessSources for each page (of size bufferSize) of the underlying channelvoidclose()Closes this source.protected intgetStartingSourceIndex(long offset) For a given offset, return the index of the source that contains the specified offset.protected voidsourceInUse(RandomAccessSource source) Called when a given source is about to become the active source.protected voidsourceReleased(RandomAccessSource source) Called when a given source is no longer the active source.Methods inherited from class GroupedRandomAccessSource
get, get, lengthMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface RandomAccessSource
get, get, length
-
Field Details
-
DEFAULT_TOTAL_BUFSIZE
public static final int DEFAULT_TOTAL_BUFSIZE- See Also:
-
DEFAULT_MAX_OPEN_BUFFERS
public static final int DEFAULT_MAX_OPEN_BUFFERS- See Also:
-
bufferSize
private final int bufferSizeThe size of each of the buffers to use when mapping files into memory. This must be greater than 0 and less thanInteger.MAX_VALUE -
channel
The channel this source is based on -
mru
Most recently used list used to hold a number of mapped pages open at a time
-
-
Constructor Details
-
PagedChannelRandomAccessSource
Constructs a newPagedChannelRandomAccessSourcebased on the specified FileChannel, with a default buffer configuration. The default buffer configuration is currently 2^26 total paged bytes, spread across a maximum of 16 active buffers. This arrangement resulted in a 24% speed improvement over the single buffer case in parametric tests extracting text from a 2.3 GB file.- Parameters:
channel- the channel to use as the backing store- Throws:
IOException- if the channel cannot be opened or mapped
-
PagedChannelRandomAccessSource
public PagedChannelRandomAccessSource(FileChannel channel, int totalBufferSize, int maxOpenBuffers) throws IOException Constructs a newPagedChannelRandomAccessSourcebased on the specified FileChannel, with a specific buffer size- Parameters:
channel- the channel to use as the backing storebufferSize- the size of the buffers to use- Throws:
IOException- if the channel cannot be opened or mapped
-
-
Method Details
-
buildSources
private static RandomAccessSource[] buildSources(FileChannel channel, int bufferSize) throws IOException Constructs a set ofMappedChannelRandomAccessSources for each page (of size bufferSize) of the underlying channel- Parameters:
channel- the underlying channelbufferSize- the size of each page (the last page may be shorter)- Returns:
- a list of sources that represent the pages of the channel
- Throws:
IOException- if IO fails for any reason
-
getStartingSourceIndex
protected int getStartingSourceIndex(long offset) Description copied from class:GroupedRandomAccessSourceFor a given offset, return the index of the source that contains the specified offset. This is an optimization feature to help optimize the access of the correct source without having to iterate through every single source each time. It is safe to always return 0, in which case the full set of sources will be searched. Subclasses should override this method if they are able to compute the source index more efficiently (for exampleFileChannelRandomAccessSourcetakes advantage of fixed size page buffers to compute the index)- Overrides:
getStartingSourceIndexin classGroupedRandomAccessSource- Parameters:
offset- the offset- Returns:
- the index of the input source that contains the specified offset, or 0 if unknown
-
sourceReleased
Description copied from class:GroupedRandomAccessSourceCalled when a given source is no longer the active source. This gives subclasses the abilty to release resources, if appropriate.- Overrides:
sourceReleasedin classGroupedRandomAccessSource- Parameters:
source- the source that is no longer the active source- Throws:
IOException- if there are any problems
-
sourceInUse
Description copied from class:GroupedRandomAccessSourceCalled when a given source is about to become the active source. This gives subclasses the abilty to retrieve resources, if appropriate.- Overrides:
sourceInUsein classGroupedRandomAccessSource- Parameters:
source- the source that is about to become the active source- Throws:
IOException- if there are any problems
-
close
Description copied from class:GroupedRandomAccessSourceCloses this source. The underlying data structure or source (if any) will also be closed Closes all of the underlying sources- Specified by:
closein interfaceRandomAccessSource- Overrides:
closein classGroupedRandomAccessSource- Throws:
IOException
-