Package com.itextpdf.text.io
Class ByteBufferRandomAccessSource
- java.lang.Object
-
- com.itextpdf.text.io.ByteBufferRandomAccessSource
-
- All Implemented Interfaces:
RandomAccessSource
class ByteBufferRandomAccessSource extends java.lang.Object implements RandomAccessSource
A RandomAccessSource that is based on an underlyingByteBuffer
. This class takes steps to ensure that the byte buffer is completely freed from memory duringclose()
- Since:
- 5.3.5
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBuffer
byteBuffer
Internal cache of memory mapped buffers
-
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 cleanervoid
close()
Closes this source.int
get(long position)
Gets a byte at the specified positionint
get(long position, byte[] bytes, int off, int len)
Gets an array at the specified position.long
length()
-
-
-
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
-
get
public int get(long position) throws java.io.IOException
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 interfaceRandomAccessSource
- 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
- Throws:
java.io.IOException
-
get
public int get(long position, byte[] bytes, int off, int len) throws java.io.IOException
Gets an array at the specified position. If the number of bytes requested cannot be read, the bytes that can be read will be placed in bytes and the number actually read will be returned.Note: Because ByteBuffers don't support long indexing, the position must be a valid positive int
- Specified by:
get
in interfaceRandomAccessSource
- 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
- Throws:
java.io.IOException
-
length
public long length()
- Specified by:
length
in interfaceRandomAccessSource
- Returns:
- the length of this source
-
close
public void close() throws java.io.IOException
Description copied from interface:RandomAccessSource
Closes this source. The underlying data structure or source (if any) will also be closed- Specified by:
close
in interfaceRandomAccessSource
- Throws:
java.io.IOException
- See Also:
Cleans the mapped bytebuffers and closes the channel
-
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
-
-