Class ByteBufferRandomAccessSource
java.lang.Object
com.itextpdf.io.source.ByteBufferRandomAccessSource
- All Implemented Interfaces:
IRandomAccessSource
A RandomAccessSource that is based on an underlying
ByteBuffer. This class takes steps to ensure
that the byte buffer
is completely freed from memory during close() if unmapping functionality is enabled-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static booleanA flag to allow unmapping hack for cleaning mapped bufferprivate final ByteBufferInternal cache of memory mapped buffersprivate static final BufferCleanerReference to a BufferCleaner that does unmapping;nullif not supported.static final booleantrue, if this platform supports unmapping mmapped files. -
Constructor Summary
ConstructorsConstructorDescriptionByteBufferRandomAccessSource(ByteBuffer byteBuffer) Constructs a newByteBufferRandomAccessSourcebased on the specified ByteBuffer -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanclean(ByteBuffer buffer) invokes the clean method on the ByteBuffer's cleanerprivate static booleancleanByUnmapping(ByteBuffer buffer) voidclose()Closes this source.static voidDisables ByteBuffer memory unmapping hackstatic voidEnables ByteBuffer memory unmapping hackintget(long position) Gets a byte at the specified positionintget(long position, byte[] bytes, int off, int len) Read an array of bytes of specified length from the specified position of source to the buffer applying the offset.longlength()Gets the length of the source
-
Field Details
-
allowUnmapping
private static boolean allowUnmappingA flag to allow unmapping hack for cleaning mapped buffer -
byteBuffer
Internal cache of memory mapped buffers -
UNMAP_SUPPORTED
public static final boolean UNMAP_SUPPORTEDtrue, if this platform supports unmapping mmapped files. -
CLEANER
Reference to a BufferCleaner that does unmapping;nullif not supported.
-
-
Constructor Details
-
ByteBufferRandomAccessSource
Constructs a newByteBufferRandomAccessSourcebased on the specified ByteBuffer- Parameters:
byteBuffer- the buffer to use as the backing store
-
-
Method Details
-
enableByteBufferMemoryUnmapping
public static void enableByteBufferMemoryUnmapping()Enables ByteBuffer memory unmapping hack -
disableByteBufferMemoryUnmapping
public static void disableByteBufferMemoryUnmapping()Disables ByteBuffer memory unmapping hack -
get
public int get(long position) Gets a byte at the specified positionNote: Because ByteBuffers don't support long indexing, the position must be a valid positive int
- Specified by:
getin interfaceIRandomAccessSource- Parameters:
position- the position to read the byte from - must be less than Integer.MAX_VALUE- Returns:
- the byte, or -1 if EOF is reached
-
get
public int get(long position, byte[] bytes, int off, int len) Read an array of bytes of specified length from the specified position of source to the buffer applying the offset. If the number of bytes requested cannot be read, all the possible bytes will be read to the buffer, and the number of actually read bytes will be returned.Note: Because ByteBuffers don't support long indexing, the position must be a valid positive int
- Specified by:
getin interfaceIRandomAccessSource- Parameters:
position- the position to read the byte from - must be less than Integer.MAX_VALUEbytes- output bufferoff- offset into the output buffer where results will be placedlen- the number of bytes to read- Returns:
- the number of bytes actually read, or -1 if the file is at EOF
-
length
public long length()Gets the length of the source- Specified by:
lengthin interfaceIRandomAccessSource- Returns:
- the length of this source
-
close
Description copied from interface:IRandomAccessSourceCloses this source. The underlying data structure or source (if any) will also be closed- Specified by:
closein interfaceIRandomAccessSource- Throws:
IOException- in case of any reading error.- See Also:
-
clean
invokes the clean method on the ByteBuffer's cleaner- Parameters:
buffer- ByteBuffer- Returns:
- boolean true on success
-
cleanByUnmapping
-