public ChainLoopDetector(long rawSize, BlockStore blockStore) { this.blockStore = blockStore; int numBlocks = (int)Math.Ceiling(1.0 * (rawSize / blockStore.GetBlockStoreBlockSize())); used_blocks = new bool[numBlocks]; }
public ChainLoopDetector(long rawSize, BlockStore blockStore) { this.blockStore = blockStore; int blkSize = blockStore.GetBlockStoreBlockSize(); int numBlocks = (int)(rawSize / blkSize); if ((rawSize % blkSize) != 0) { numBlocks++; } used_blocks = new bool[numBlocks]; }
/** * Updates the contents of the stream to the new * Set of bytes. * Note - if this is property based, you'll still * need to update the size in the property yourself */ public void UpdateContents(byte[] contents) { // How many blocks are we going to need? int blockSize = blockStore.GetBlockStoreBlockSize(); int blocks = (int)Math.Ceiling(((double)contents.Length) / blockSize); // Make sure we don't encounter a loop whilst overwriting // the existing blocks ChainLoopDetector loopDetector = blockStore.GetChainLoopDetector(); // Start writing int prevBlock = POIFSConstants.END_OF_CHAIN; int nextBlock = startBlock; for (int i = 0; i < blocks; i++) { int thisBlock = nextBlock; // Allocate a block if needed, otherwise figure // out what the next block will be if (thisBlock == POIFSConstants.END_OF_CHAIN) { thisBlock = blockStore.GetFreeBlock(); loopDetector.Claim(thisBlock); // We're on the end of the chain nextBlock = POIFSConstants.END_OF_CHAIN; // Mark the previous block as carrying on to us if needed if (prevBlock != POIFSConstants.END_OF_CHAIN) { blockStore.SetNextBlock(prevBlock, thisBlock); } blockStore.SetNextBlock(thisBlock, POIFSConstants.END_OF_CHAIN); // If we've just written the first block on a // new stream, save the start block offset if (this.startBlock == POIFSConstants.END_OF_CHAIN) { this.startBlock = thisBlock; } } else { loopDetector.Claim(thisBlock); nextBlock = blockStore.GetNextBlock(thisBlock); } // Write it //byte[] buffer = blockStore.CreateBlockIfNeeded(thisBlock); ByteBuffer buffer = blockStore.CreateBlockIfNeeded(thisBlock); int startAt = i * blockSize; int endAt = Math.Min(contents.Length - startAt, blockSize); buffer.Write(contents, startAt, endAt); //for (int index = startAt, j = 0; index < endAt; index++, j++) // buffer[j] = contents[index]; // Update pointers prevBlock = thisBlock; } int lastBlock = prevBlock; // If we're overwriting, free any remaining blocks NPOIFSStream toFree = new NPOIFSStream(blockStore, nextBlock); toFree.free(loopDetector); // Mark the end of the stream blockStore.SetNextBlock(lastBlock, POIFSConstants.END_OF_CHAIN); }