/// <summary> /// Reads the specified number of bytes from the current position. /// </summary> public override int Read(byte[] buffer, int offset, int count) { if (count <= 0) { return(0); } try { var rangeToRead = IndexRange.FromLength(this.position, count); int writtenCount = 0; if (fileDataRange.Intersection(rangeToRead) == null) { int readCountFromFooter; if (TryReadFromFooter(rangeToRead, buffer, offset, out readCountFromFooter)) { writtenCount += readCountFromFooter; } return(writtenCount); } rangeToRead = fileDataRange.Intersection(rangeToRead); var startingBlock = ByteToBlock(rangeToRead.StartIndex); var endingBlock = ByteToBlock(rangeToRead.EndIndex); for (var blockIndex = startingBlock; blockIndex <= endingBlock; blockIndex++) { var currentBlock = blockFactory.Create(blockIndex); var rangeToReadInBlock = currentBlock.LogicalRange.Intersection(rangeToRead); var copyStartIndex = rangeToReadInBlock.StartIndex % blockFactory.GetBlockSize(); Buffer.BlockCopy(currentBlock.Data, (int)copyStartIndex, buffer, offset + writtenCount, (int)rangeToReadInBlock.Length); writtenCount += (int)rangeToReadInBlock.Length; } this.position += writtenCount; return(writtenCount); } catch (Exception e) { throw new VhdParsingException("Invalid or Corrupted VHD file", e); } }