Package com.itextpdf.io.source
Class PagedChannelRandomAccessSource
java.lang.Object
com.itextpdf.io.source.GroupedRandomAccessSource
com.itextpdf.io.source.PagedChannelRandomAccessSource
- All Implemented Interfaces:
IRandomAccessSource
class PagedChannelRandomAccessSource
extends GroupedRandomAccessSource
implements IRandomAccessSource
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-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
The size of each of the buffers to use when mapping files into memory.private final FileChannel
The channel this source is based onstatic final int
static final int
private final PagedChannelRandomAccessSource.MRU
<IRandomAccessSource> Most recently used list used to hold a number of mapped pages open at a time -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newPagedChannelRandomAccessSource
based on the specified FileChannel, with a default buffer configuration.PagedChannelRandomAccessSource
(FileChannel channel, int totalBufferSize, int maxOpenBuffers) Constructs a newPagedChannelRandomAccessSource
based on the specified FileChannel, with a specific buffer size -
Method Summary
Modifier and TypeMethodDescriptionprivate static IRandomAccessSource[]
buildSources
(FileChannel channel, int bufferSize) Constructs a set ofMappedChannelRandomAccessSource
s for each page (of size bufferSize) of the underlying channelvoid
close()
Closes this source.protected int
getStartingSourceIndex
(long offset) For a given offset, return the index of the source that contains the specified offset.protected void
sourceInUse
(IRandomAccessSource source) Called when a given source is about to become the active source.protected void
sourceReleased
(IRandomAccessSource source) Called when a given source is no longer the active source.Methods inherited from class com.itextpdf.io.source.GroupedRandomAccessSource
get, get, length
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.itextpdf.io.source.IRandomAccessSource
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 newPagedChannelRandomAccessSource
based 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 newPagedChannelRandomAccessSource
based on the specified FileChannel, with a specific buffer size- Parameters:
channel
- the channel to use as the backing storetotalBufferSize
- total buffer sizemaxOpenBuffers
- open buffers- Throws:
IOException
- if the channel cannot be opened or mapped
-
-
Method Details
-
buildSources
private static IRandomAccessSource[] buildSources(FileChannel channel, int bufferSize) throws IOException Constructs a set ofMappedChannelRandomAccessSource
s 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) For 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 exampleFileChannelRandomAccessSource
takes advantage of fixed size page buffers to compute the index)- Overrides:
getStartingSourceIndex
in classGroupedRandomAccessSource
- Parameters:
offset
- the offset- Returns:
- the index of the input source that contains the specified offset, or 0 if unknown
-
sourceReleased
Called when a given source is no longer the active source. This gives subclasses the abilty to release resources, if appropriate. For now, close the source that is no longer being used. In the future, we may implement an MRU that allows multiple pages to be opened at a time- Overrides:
sourceReleased
in classGroupedRandomAccessSource
- Parameters:
source
- the source that is no longer the active source- Throws:
IOException
- if there are any problems
-
sourceInUse
Called when a given source is about to become the active source. This gives subclasses the abilty to retrieve resources, if appropriate. Ensure that the source is mapped. In the future, we may implement an MRU that allows multiple pages to be opened at a time- Overrides:
sourceInUse
in classGroupedRandomAccessSource
- Parameters:
source
- the source that is about to become the active source- Throws:
IOException
- if there are any problems
-
close
Closes this source. The underlying data structure or source (if any) will also be closed
Closes all of the underlying sources. Cleans the mapped bytebuffers and closes the channel- Specified by:
close
in interfaceIRandomAccessSource
- Overrides:
close
in classGroupedRandomAccessSource
- Throws:
IOException
- in case of any reading error.
-