public byte[] ReadMap(out int bitmapSizeInBits) { byte[] firstBlockBytes = _disk.ReadAllBytesFromBlock(_freeSpaceMapStartingBlock); var stream = new MemoryStream(firstBlockBytes); var binaryReader = new BinaryReader(stream); int freeSpaceLengthInBits = binaryReader.ReadInt32(); int freeSpaceLengthInBytes = SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData( freeSpaceLengthInBits, Constants.NumberOfBitsInByte); ReadOnlyCollection <BucketDistribution> numberOfBytesToReadFromEachBlock = ItemDistributor.Distribute(freeSpaceLengthInBytes, _disk.BlockSizeInBytes - Constants.BlockReferenceSizeInBytes, _disk.BlockSizeInBytes); var freeSpaceMap = new byte[freeSpaceLengthInBytes]; int numberOfCurrentBlock = _freeSpaceMapStartingBlock; int initialArrayPosition = 0; foreach (BucketDistribution distribution in numberOfBytesToReadFromEachBlock) { byte[] newArray = _disk.ReadBytesFromBlock(numberOfCurrentBlock + distribution.BucketIndex, distribution.IndexOfFirstItemTheBucketGot, distribution.NumberOfItemsDistributed); Array.Copy(newArray, 0, freeSpaceMap, initialArrayPosition, distribution.NumberOfItemsDistributed); initialArrayPosition += distribution.NumberOfItemsDistributed; } bitmapSizeInBits = freeSpaceLengthInBits; return(freeSpaceMap); }
private void InitializeSingleIndirectionBlock(int indexOfBlockToCreateIndirectionBlockFrom, int currentCountOfIntegersInBlock, int currentPositionInBlock) { byte[] blockContents = _disk.ReadAllBytesFromBlock(indexOfBlockToCreateIndirectionBlockFrom); _singlyIndirectBlockReferences = new IntegerListConstrained(blockContents, currentCountOfIntegersInBlock, _addressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock); _singlyIndirectReferencesEnumerator = _singlyIndirectBlockReferences.GetAddressableEnumerator(); if (currentPositionInBlock != PositionBeforeFirstElement) { _singlyIndirectReferencesEnumerator.SetPosition(currentPositionInBlock); } }
/// <summary> /// /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="numberOfBlockToRead"></param> /// <exception cref="InconsistentDataDetectedException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> private TResult DeserializeObjectAtBlock <TResult>(int numberOfBlockToRead) { try { byte[] blockBytes = _virtualDisk.ReadAllBytesFromBlock(numberOfBlockToRead); int serializedStreamLength = GetSerializedStreamSize(blockBytes); return((TResult)ReadObject(blockBytes, 4, serializedStreamLength)); } catch (InvalidCastException exception) { throw ConstructGenericCannotReadNodeException(exception); } catch (SerializationException exception) { throw ConstructGenericCannotReadNodeException(exception); } }
public DataStreamStructureBuilderImmutable(IVirtualDisk disk, DataStreamDefinition dataStreamDefinition, AddressingSystemParameters addressingSystemParameters, IFileSystemNodeStorage fileSystemNodeStorage, Node governingNode) { if (disk == null) { throw new ArgumentNullException("disk"); } if (dataStreamDefinition == null) { throw new ArgumentNullException("dataStreamDefinition"); } if (addressingSystemParameters == null) { throw new ArgumentNullException("addressingSystemParameters"); } if (fileSystemNodeStorage == null) { throw new ArgumentNullException("fileSystemNodeStorage"); } if (governingNode == null) { throw new ArgumentNullException("governingNode"); } _disk = disk; _governingNode = governingNode; _fileSystemNodeStorage = fileSystemNodeStorage; _addressingSystemParameters = addressingSystemParameters; _dataStreamDefinition = dataStreamDefinition; int numberOfBlocks = SpaceRequirementsCalculator.GetNumberOfChunksNeededToStoreData( dataStreamDefinition.StreamLengthInBytes, addressingSystemParameters.BlockSize); _addressingBlockSizesCalculator = new AddressingBlockSizesCalculator(addressingSystemParameters.IndirectBlockReferencesCountInDoubleIndirectBlock, addressingSystemParameters.DataBlockReferencesCountInSingleIndirectBlock); _sizesOfLastAddressingBlocks = AddressingBlockSizesCalculator.GetSizesOfAddressingBlocksSufficientToStoreItems(numberOfBlocks); int doubleIndirectBlockSize = _sizesOfLastAddressingBlocks.DoubleIndirectBlockSize; _doubleIndirectBlocks = new IntegerListConstrained(disk.ReadAllBytesFromBlock(dataStreamDefinition.ContentsBlockIndex), doubleIndirectBlockSize, _addressingSystemParameters.IndirectBlockReferencesCountInDoubleIndirectBlock); CalculateAndSetMaximumSize(addressingSystemParameters); }