/** * Allocates a ByteBuffer to be used for writing to the specified block. The * contents of the ByteBuffer are undefined. The method {@link #writeBlock} * is called to write the buffer to the block. * * @param objectPool the object pool to attempt to get objects from and * release objects to. * @param blockId The ID of the block that this buffer will be written to. * @return a ByteBuffer to be used for writing to the specified block. */ public Block allocateBlock(ObjectPool objectPool, long blockId) { assert(blockId >= 0) && (blockId < nrBlocks); return Block.newInstance(objectPool, this, blockSize, blockId, byteOrder); }
/** * Create a new block, not attached to any file. * * @param objectPool The object pool to get the block from. * @param blockSize The size of the block to create. * @return A new block, with no file data. */ public static Block newInstance(ObjectPool objectPool, int blockSize) { return newInstance(objectPool, null, blockSize, 0, ByteOrder.nativeOrder()); }
/** * Allocates a ByteBuffer which is filled with the contents of the specified * block. If the buffer is modified then the method {@link #writeBlock} * should be called to write the buffer back to the file. * * @param objectPool the object pool to attempt to get objects from and * release objects to. * @param blockId the block to read into the ByteBuffer. * @return The block that was read. */ public Block readBlock(ObjectPool objectPool, long blockId) { if ((blockId < 0) || (blockId >= nrBlocks)) { throw new IllegalArgumentException( "blockId: " + blockId + " of " + nrBlocks ); } long fileOffset = blockId * blockSize; int regionNr = (int) (fileOffset / stride); int offset = (int) (fileOffset % stride); assert mappedByteBuffers != null; assert srcByteBuffers != null; assert intBuffers != null; assert longBuffers != null; return Block.newInstance( objectPool, this, blockSize, blockId, offset, mappedByteBuffers[regionNr], srcByteBuffers[regionNr], intBuffers[regionNr], longBuffers[regionNr] ); }
/** * Factory method to create a new block, allocating a new data buffer. Uses the objectPool * if possible, or else creates a new block that will be returned to the object pool. * * @param objectPool The pool of blocks to get the block from. * @param blockFile The file to get the block from. * @param blockSize The size of the data in the block. * @param blockId The ID of the block from the file. * @param byteOrder Represents little endian or big endian byte ordering. * @return The block from the pool, or a new block which will be returned to the pool. */ public static Block newInstance( ObjectPool objectPool, BlockFile blockFile, int blockSize, long blockId, ByteOrder byteOrder ) { Block block = (Block) objectPool.get(ObjectPool.TYPE_S_BLOCK, blockSize); if (block != null) { block.init(objectPool, blockFile, blockId); } else { block = Block.newInstance( objectPool, blockFile, blockSize, blockId, 0, ByteBuffer.allocateDirect(blockSize).order(byteOrder), null, null, null ); block.ownsBuffer = true; } return block; }