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 underlyingFileChannel
. The channel is mapped into memory using a paging scheme to allow for efficient reads of very large files. As an implementation detail, we useGroupedRandomAccessSource
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 Modifier and Type Class Description private static class
PagedChannelRandomAccessSource.MRU<E>
-
Field Summary
Fields Modifier and Type Field Description private int
bufferSize
The size of each of the buffers to use when mapping files into memory.private java.nio.channels.FileChannel
channel
The channel this source is based onstatic int
DEFAULT_MAX_OPEN_BUFFERS
static int
DEFAULT_TOTAL_BUFSIZE
private PagedChannelRandomAccessSource.MRU<IRandomAccessSource>
mru
Most recently used list used to hold a number of mapped pages open at a time
-
Constructor Summary
Constructors Constructor Description PagedChannelRandomAccessSource(java.nio.channels.FileChannel channel)
Constructs a newPagedChannelRandomAccessSource
based on the specified FileChannel, with a default buffer configuration.PagedChannelRandomAccessSource(java.nio.channels.FileChannel channel, int totalBufferSize, int maxOpenBuffers)
Constructs a newPagedChannelRandomAccessSource
based on the specified FileChannel, with a specific buffer size
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static IRandomAccessSource[]
buildSources(java.nio.channels.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 Detail
-
DEFAULT_TOTAL_BUFSIZE
public static final int DEFAULT_TOTAL_BUFSIZE
- See Also:
- Constant Field Values
-
DEFAULT_MAX_OPEN_BUFFERS
public static final int DEFAULT_MAX_OPEN_BUFFERS
- See Also:
- Constant Field Values
-
bufferSize
private final int bufferSize
The 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
private final java.nio.channels.FileChannel channel
The channel this source is based on
-
mru
private final PagedChannelRandomAccessSource.MRU<IRandomAccessSource> mru
Most recently used list used to hold a number of mapped pages open at a time
-
-
Constructor Detail
-
PagedChannelRandomAccessSource
public PagedChannelRandomAccessSource(java.nio.channels.FileChannel channel) throws java.io.IOException
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:
java.io.IOException
- if the channel cannot be opened or mapped
-
PagedChannelRandomAccessSource
public PagedChannelRandomAccessSource(java.nio.channels.FileChannel channel, int totalBufferSize, int maxOpenBuffers) throws java.io.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:
java.io.IOException
- if the channel cannot be opened or mapped
-
-
Method Detail
-
buildSources
private static IRandomAccessSource[] buildSources(java.nio.channels.FileChannel channel, int bufferSize) throws java.io.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:
java.io.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
protected void sourceReleased(IRandomAccessSource source) throws java.io.IOException
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:
java.io.IOException
- if there are any problems
-
sourceInUse
protected void sourceInUse(IRandomAccessSource source) throws java.io.IOException
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:
java.io.IOException
- if there are any problems
-
close
public void close() throws java.io.IOException
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:
java.io.IOException
- in case of any reading error.
-
-