/** * Frees all blocks in the given file. */ public void free(RegularFile file) { free(file, file.blockCount()); }
/** * Frees the last {@code count} blocks from the given file. */ public synchronized void free(RegularFile file, int count) { int remainingCacheSpace = maxCachedBlockCount - blockCache.blockCount(); if (remainingCacheSpace > 0) { file.copyBlocksTo(blockCache, Math.min(count, remainingCacheSpace)); } file.truncateBlocks(file.blockCount() - count); allocatedBlockCount -= count; } }
/** * Allocates the given number of blocks and adds them to the given file. */ public synchronized void allocate(RegularFile file, int count) throws IOException { int newAllocatedBlockCount = allocatedBlockCount + count; if (newAllocatedBlockCount > maxBlockCount) { throw new IOException("out of disk space"); } int newBlocksNeeded = Math.max(count - blockCache.blockCount(), 0); for (int i = 0; i < newBlocksNeeded; i++) { file.addBlock(new byte[blockSize]); } if (newBlocksNeeded != count) { blockCache.transferBlocksTo(file, count - newBlocksNeeded); } allocatedBlockCount = newAllocatedBlockCount; }
/** * Frees all blocks in the given file. */ public void free(RegularFile file) { free(file, file.blockCount()); }
/** * Frees the last {@code count} blocks from the given file. */ public synchronized void free(RegularFile file, int count) { int remainingCacheSpace = maxCachedBlockCount - blockCache.blockCount(); if (remainingCacheSpace > 0) { file.copyBlocksTo(blockCache, Math.min(count, remainingCacheSpace)); } file.truncateBlocks(file.blockCount() - count); allocatedBlockCount -= count; } }
/** * Allocates the given number of blocks and adds them to the given file. */ public synchronized void allocate(RegularFile file, int count) throws IOException { int newAllocatedBlockCount = allocatedBlockCount + count; if (newAllocatedBlockCount > maxBlockCount) { throw new IOException("out of disk space"); } int newBlocksNeeded = Math.max(count - blockCache.blockCount(), 0); for (int i = 0; i < newBlocksNeeded; i++) { file.addBlock(new byte[blockSize]); } if (newBlocksNeeded != count) { blockCache.transferBlocksTo(file, count - newBlocksNeeded); } allocatedBlockCount = newAllocatedBlockCount; }