private void ensureCapacity() throws IOException { if (offset >= BLOCK_SIZE) { // get the next block if (blockCount >= usedBlocks.length) { int[] blocks = new int[blockCount * 2]; System.arraycopy(usedBlocks, 0, blocks, 0, usedBlocks.length); Arrays.fill(blocks, usedBlocks.length, blocks.length, -1); usedBlocks = blocks; } int nextBlockId = usedBlocks[blockCount]; if (nextBlockId == -1) { nextBlockId = file.allocBlock(); usedBlocks[blockCount] = nextBlockId; } // flush the current block into the disk if (blockId != -1) { BTreeUtils.integerToBytes(nextBlockId, bytes); file.writeBlock(blockId, bytes); } blockId = nextBlockId; blockCount++; Arrays.fill(bytes, (byte) 0); offset = 4; } }
public NodeOutputStream(NodeFile file) throws IOException { this(file, new int[] {file.allocBlock()}); }