Package com.itextpdf.io.source
Class ByteBufferRandomAccessSource
- java.lang.Object
-
- com.itextpdf.io.source.ByteBufferRandomAccessSource
-
- All Implemented Interfaces:
IRandomAccessSource
class ByteBufferRandomAccessSource extends java.lang.Object implements IRandomAccessSource
A RandomAccessSource that is based on an underlyingByteBuffer
. This class takes steps to ensure that the byte buffer is completely freed from memory duringclose()
if unmapping functionality is enabled
-
-
Field Summary
Fields Modifier and Type Field Description private static boolean
allowUnmapping
A flag to allow unmapping hack for cleaning mapped bufferprivate java.nio.ByteBuffer
byteBuffer
Internal cache of memory mapped buffersprivate static BufferCleaner
CLEANER
Reference to a BufferCleaner that does unmapping;null
if not supported.static boolean
UNMAP_SUPPORTED
true
, if this platform supports unmapping mmapped files.
-
Constructor Summary
Constructors Constructor Description ByteBufferRandomAccessSource(java.nio.ByteBuffer byteBuffer)
Constructs a newByteBufferRandomAccessSource
based on the specified ByteBuffer
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
clean(java.nio.ByteBuffer buffer)
invokes the clean method on the ByteBuffer's cleanerprivate static boolean
cleanByUnmapping(java.nio.ByteBuffer buffer)
void
close()
Closes this source.static void
disableByteBufferMemoryUnmapping()
Disables ByteBuffer memory unmapping hackstatic void
enableByteBufferMemoryUnmapping()
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 Detail
-
allowUnmapping
private static boolean allowUnmapping
A flag to allow unmapping hack for cleaning mapped buffer
-
byteBuffer
private final java.nio.ByteBuffer byteBuffer
Internal cache of memory mapped buffers
-
UNMAP_SUPPORTED
public static final boolean UNMAP_SUPPORTED
true
, if this platform supports unmapping mmapped files.
-
CLEANER
private static final BufferCleaner CLEANER
Reference to a BufferCleaner that does unmapping;null
if not supported.
-
-
Constructor Detail
-
ByteBufferRandomAccessSource
public ByteBufferRandomAccessSource(java.nio.ByteBuffer byteBuffer)
Constructs a newByteBufferRandomAccessSource
based on the specified ByteBuffer- Parameters:
byteBuffer
- the buffer to use as the backing store
-
-
Method Detail
-
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
public void close() throws java.io.IOException
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:
java.io.IOException
- in case of any reading error.- See Also:
Cleans the mapped bytebuffers and closes the channel if unmapping functionality is enabled
-
clean
private static boolean clean(java.nio.ByteBuffer buffer)
invokes the clean method on the ByteBuffer's cleaner- Parameters:
buffer
- ByteBuffer- Returns:
- boolean true on success
-
cleanByUnmapping
private static boolean cleanByUnmapping(java.nio.ByteBuffer buffer)
-
-