Top Description Fields Constructors Methods
org.apache.derby.impl.io.vfmem

public Class BlockedByteArray

extends Object
Class Inheritance
Imports
org.apache.derby.shared.common.sanity.SanityManager

Stores data in blocks, and supports reading/writing data from/into these blocks.

The blocked array is expanded and shrunk as required.

Field Summary

Modifier and TypeField and Description
private static final int
_16K

Constant for 16 KB.

private static final int
_32K

Constant for 32 KB.

private static final int
_4K

Constant for 4 KB.

private static final int
_8K

Constant for 8 KB.

private int
allocatedBlocks

The number of allocated blocks.

private byte[][]
blocks

References to blocks of data.

private int
blockSize

The size of a block of data (the allocation unit).

private static final int
DEFAULT_BLOCKSIZE

The default block size.

private static final int
INITIAL_BLOCK_HOLDER_SIZE

The default number of slots for holding a block of data.

private long
length

The number of bytes stored in the blocked byte array.

private static final int
MIN_HOLDER_GROWTH

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

AccessConstructor and Description
public
BlockedByteArray()

Creates a new blocked byte array with the default number of slots to hold byte arrays (blocks).

Method Summary

Modifier and TypeMethod and Description
private void
checkBlockSize(int
the requested number of bytes to be written
len
)

Tries to optimize the block size by setting it equal to the the page size used by the database.

pack-priv synchronized BlockedByteArrayInputStream

Returns:

An InputStream-object.
getInputStream
()

Returns an input stream serving the data in the blocked byte array.

pack-priv synchronized BlockedByteArrayOutputStream

Returns:

An OutputStream-object.
getOutputStream
(long
initial position of the output stream
pos
)

Returns an output stream writing data into the blocked byte array.

private void
increaseCapacity(long
the index that must fit into the array
lastIndex
)

Increases the capacity of this blocked byte array by allocating more blocks.

public synchronized long

Returns:

Bytes allocated.
length
()

Returns the number of bytes allocated.

public synchronized int

Returns:

A single byte.
read
(long
position to read from
pos
)

Returns the byte at the given position.

public synchronized int

Returns:

The number of bytes read.
read
(long
the position to start reading at
pos
,
byte[]
the destination buffer
buf
,
int
offset into the destination buffer
offset
,
int
the number of bytes to read
len
)

Reads up to len bytes.

pack-priv synchronized void
release()

Releases this array.

public synchronized void
setLength(final long
the new length of the allocated data in bytes
newLength
)

Changes the allocated length of the data.

public synchronized int

Returns:

1, which is the number of bytes written.
writeByte
(long
the position to write the byte at
pos
,
byte
the byte to write
b
)

Writes the given byte into the blocked byte array.

public synchronized int

Returns:

The number of bytes written.
writeBytes
(final long
the position to start writing at
pos
,
final byte[]
the source buffer
buf
,
int
the offset into the source buffer
offset
,
final int
the number of bytes to write
len
)

Writes the given bytes into the blocked byte array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_16Kback to summary
private static final int _16K

Constant for 16 KB.

_32Kback to summary
private static final int _32K

Constant for 32 KB.

_4Kback to summary
private static final int _4K

Constant for 4 KB.

_8Kback to summary
private static final int _8K

Constant for 8 KB.

allocatedBlocksback to summary
private int allocatedBlocks

The number of allocated blocks.

blocksback to summary
private byte[][] blocks

References to blocks of data.

blockSizeback to summary
private int blockSize

The size of a block of data (the allocation unit).

DEFAULT_BLOCKSIZEback to summary
private static final int DEFAULT_BLOCKSIZE

The default block size.

INITIAL_BLOCK_HOLDER_SIZEback to summary
private static final int INITIAL_BLOCK_HOLDER_SIZE

The default number of slots for holding a block of data.

lengthback to summary
private long length

The number of bytes stored in the blocked byte array.

MIN_HOLDER_GROWTHback to summary
private static final int MIN_HOLDER_GROWTH

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 Detail

BlockedByteArrayback to summary
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
INITIAL_BLOCK_HOLDER_SIZE

Method Detail

checkBlockSizeback to summary
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:int

the requested number of bytes to be written

getInputStreamback to summary
pack-priv synchronized BlockedByteArrayInputStream getInputStream()

Returns an input stream serving the data in the blocked byte array.

Returns:BlockedByteArrayInputStream

An InputStream-object.

getOutputStreamback to summary
pack-priv synchronized BlockedByteArrayOutputStream getOutputStream(long pos)

Returns an output stream writing data into the blocked byte array.

Parameters
pos:long

initial position of the output stream

Returns:BlockedByteArrayOutputStream

An OutputStream-object.

increaseCapacityback to summary
private void increaseCapacity(long lastIndex)

Increases the capacity of this blocked byte array by allocating more blocks.

Parameters
lastIndex:long

the index that must fit into the array

lengthback to summary
public synchronized long length()

Returns the number of bytes allocated.

Returns:long

Bytes allocated.

readback to summary
public synchronized int read(long pos)

Returns the byte at the given position.

Parameters
pos:long

position to read from

Returns:int

A single byte.

readback to summary
public synchronized int read(long pos, byte[] buf, int offset, int len)

Reads up to len bytes.

Parameters
pos:long

the position to start reading at

buf:byte[]

the destination buffer

offset:int

offset into the destination buffer

len:int

the number of bytes to read

Returns:int

The number of bytes read.

releaseback to summary
pack-priv synchronized void release()

Releases this array.

setLengthback to summary
public synchronized void setLength(final 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:long

the new length of the allocated data in bytes

writeByteback to summary
public synchronized int writeByte(long pos, byte b)

Writes the given byte into the blocked byte array.

Parameters
pos:long

the position to write the byte at

b:byte

the byte to write

Returns:int

1, which is the number of bytes written.

writeBytesback to summary
public synchronized int writeBytes(final long pos, final byte[] buf, int offset, final int len)

Writes the given bytes into the blocked byte array.

Parameters
pos:long

the position to start writing at

buf:byte[]

the source buffer

offset:int

the offset into the source buffer

len:int

the number of bytes to write

Returns:int

The number of bytes written.