Package org.apache.derby.impl.io.vfmem
Class BlockedByteArray
java.lang.Object
org.apache.derby.impl.io.vfmem.BlockedByteArray
Stores data in blocks, and supports reading/writing data from/into these
blocks.
The blocked array is expanded and shrunk as required.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Constant for 16 KB.private static final int
Constant for 32 KB.private static final int
Constant for 4 KB.private static final int
Constant for 8 KB.private int
The number of allocated blocks.private byte[][]
References to blocks of data.private int
The size of a block of data (the allocation unit).private static final int
The default block size.private static final int
The default number of slots for holding a block of data.private long
The number of bytes stored in the blocked byte array.private static final int
Minimum number of holder slots to grow with when the block holder array has to grow to be able to reference all the data arrays. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new blocked byte array with the default number of slots to hold byte arrays (blocks). -
Method Summary
Modifier and TypeMethodDescriptionprivate void
checkBlockSize
(int len) Tries to optimize the block size by setting it equal to the the page size used by the database.(package private) BlockedByteArrayInputStream
Returns an input stream serving the data in the blocked byte array.(package private) BlockedByteArrayOutputStream
getOutputStream
(long pos) Returns an output stream writing data into the blocked byte array.private void
increaseCapacity
(long lastIndex) Increases the capacity of this blocked byte array by allocating more blocks.long
length()
Returns the number of bytes allocated.int
read
(long pos) Returns the byte at the given position.int
read
(long pos, byte[] buf, int offset, int len) Reads up tolen
bytes.(package private) void
release()
Releases this array.void
setLength
(long newLength) Changes the allocated length of the data.int
writeByte
(long pos, byte b) Writes the given byte into the blocked byte array.int
writeBytes
(long pos, byte[] buf, int offset, int len) Writes the given bytes into the blocked byte array.
-
Field Details
-
_4K
private static final int _4KConstant for 4 KB.- See Also:
-
_8K
private static final int _8KConstant for 8 KB.- See Also:
-
_16K
private static final int _16KConstant for 16 KB.- See Also:
-
_32K
private static final int _32KConstant for 32 KB.- See Also:
-
DEFAULT_BLOCKSIZE
private static final int DEFAULT_BLOCKSIZEThe default block size.- See Also:
-
INITIAL_BLOCK_HOLDER_SIZE
private static final int INITIAL_BLOCK_HOLDER_SIZEThe default number of slots for holding a block of data.- See Also:
-
MIN_HOLDER_GROWTH
private static final int MIN_HOLDER_GROWTHMinimum number of holder slots to grow with when the block holder array has to grow to be able to reference all the data arrays.- See Also:
-
blocks
private byte[][] blocksReferences to blocks of data. -
blockSize
private int blockSizeThe size of a block of data (the allocation unit). -
allocatedBlocks
private int allocatedBlocksThe number of allocated blocks. -
length
private long lengthThe number of bytes stored in the blocked byte array.
-
-
Constructor Details
-
BlockedByteArray
public BlockedByteArray()Creates a new blocked byte array with the default number of slots to hold byte arrays (blocks).No blocks are pre-allocated.
- See Also:
-
-
Method Details
-
read
public int read(long pos) Returns the byte at the given position.- Parameters:
pos
- position to read from- Returns:
- A single byte.
-
read
public int read(long pos, byte[] buf, int offset, int len) Reads up tolen
bytes.- Parameters:
pos
- the position to start reading atbuf
- the destination bufferoffset
- offset into the destination bufferlen
- the number of bytes to read- Returns:
- The number of bytes read.
-
length
public long length()Returns the number of bytes allocated.- Returns:
- Bytes allocated.
-
setLength
public void setLength(long newLength) Changes the allocated length of the data.If the new length is larger than the current length, the blocked byte array will be extended with new blocks. If the new length is smaller, existing (allocated) blocks will be removed if possible.
- Parameters:
newLength
- the new length of the allocated data in bytes
-
writeBytes
public int writeBytes(long pos, byte[] buf, int offset, int len) Writes the given bytes into the blocked byte array.- Parameters:
pos
- the position to start writing atbuf
- the source bufferoffset
- the offset into the source bufferlen
- the number of bytes to write- Returns:
- The number of bytes written.
-
writeByte
public int writeByte(long pos, byte b) Writes the given byte into the blocked byte array.- Parameters:
pos
- the position to write the byte atb
- the byte to write- Returns:
1
, which is the number of bytes written.
-
getInputStream
BlockedByteArrayInputStream getInputStream()Returns an input stream serving the data in the blocked byte array.- Returns:
- An
InputStream
-object.
-
getOutputStream
Returns an output stream writing data into the blocked byte array.- Parameters:
pos
- initial position of the output stream- Returns:
- An
OutputStream
-object.
-
release
void release()Releases this array. -
checkBlockSize
private void checkBlockSize(int len) Tries to optimize the block size by setting it equal to the the page size used by the database.Since we don't have a way of knowing which page size will be used, wait to set the block size until the first write request and see how many bytes are written then.
- Parameters:
len
- the requested number of bytes to be written
-
increaseCapacity
private void increaseCapacity(long lastIndex) Increases the capacity of this blocked byte array by allocating more blocks.- Parameters:
lastIndex
- the index that must fit into the array
-