Class BlockedByteArray


  • public class BlockedByteArray
    extends java.lang.Object
    Stores data in blocks, and supports reading/writing data from/into these blocks.

    The blocked array is expanded and shrunk as required.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int _16K
      Constant for 16 KB.
      private static int _32K
      Constant for 32 KB.
      private static int _4K
      Constant for 4 KB.
      private static 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 int DEFAULT_BLOCKSIZE
      The default block size.
      private static 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 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

      Constructors 
      Constructor Description
      BlockedByteArray()
      Creates a new blocked byte array with the default number of slots to hold byte arrays (blocks).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private 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 getInputStream()
      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 to len 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_BLOCKSIZE

        private static final int DEFAULT_BLOCKSIZE
        The default block size.
        See Also:
        Constant Field Values
      • INITIAL_BLOCK_HOLDER_SIZE

        private static final int INITIAL_BLOCK_HOLDER_SIZE
        The default number of slots for holding a block of data.
        See Also:
        Constant Field Values
      • MIN_HOLDER_GROWTH

        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.
        See Also:
        Constant Field Values
      • blocks

        private byte[][] blocks
        References to blocks of data.
      • blockSize

        private int blockSize
        The size of a block of data (the allocation unit).
      • allocatedBlocks

        private int allocatedBlocks
        The number of allocated blocks.
      • length

        private long length
        The number of bytes stored in the blocked byte array.
    • Constructor Detail

      • 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:
        INITIAL_BLOCK_HOLDER_SIZE
    • Method Detail

      • 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 to len bytes.
        Parameters:
        pos - the position to start reading at
        buf - the destination buffer
        offset - offset into the destination buffer
        len - 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 at
        buf - the source buffer
        offset - the offset into the source buffer
        len - 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 at
        b - 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

        BlockedByteArrayOutputStream getOutputStream​(long pos)
        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