Package com.itextpdf.io.source
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 boolean
A flag to allow unmapping hack for cleaning mapped bufferprivate final ByteBuffer
Internal cache of memory mapped buffersprivate static final BufferCleaner
Reference to a BufferCleaner that does unmapping;null
if not supported.static final boolean
true
, if this platform supports unmapping mmapped files. -
Constructor Summary
ConstructorsConstructorDescriptionByteBufferRandomAccessSource
(ByteBuffer byteBuffer) Constructs a newByteBufferRandomAccessSource
based on the specified ByteBuffer -
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
clean
(ByteBuffer buffer) invokes the clean method on the ByteBuffer's cleanerprivate static boolean
cleanByUnmapping
(ByteBuffer buffer) void
close()
Closes this source.static void
Disables ByteBuffer memory unmapping hackstatic void
Enables ByteBuffer memory unmapping hackint
get
(long position) Gets a byte at the specified positionint
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.long
length()
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;null
if not supported.
-
-
Constructor Details
-
ByteBufferRandomAccessSource
Constructs a newByteBufferRandomAccessSource
based 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:
get
in 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:
get
in 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:
length
in interfaceIRandomAccessSource
- Returns:
- the length of this source
-
close
Description copied from interface:IRandomAccessSource
Closes this source. The underlying data structure or source (if any) will also be closed- Specified by:
close
in 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
-