PETR
programming c# 4.0.pdf

In relied on loading all the files into memory

Info icon This preview shows pages 441–444. Sign up to view the full content.

in Example 11-39 , relied on loading all the files into memory upfront. The modified version in Example 11-44 uses streams. Example 11-44. Stream-based CompareBytes private static void CompareBytes( List<FileContents> files, Dictionary<FileContents, List<FileContents>> potentiallyMatched) { // Remember, this only ever gets called with files of equal length. long bytesToRead = files[0].Content.Length; // We work through all the files at once, so allocate a buffer for each. Dictionary<FileContents, byte[]> fileBuffers = files.ToDictionary(x => x, x => new byte[1024]); var sourceFilesWithNoMatches = new List<FileContents>(); while (bytesToRead > 0) { // Read up to 1k from all the files. int bytesRead = 0; foreach (var bufferEntry in fileBuffers) { FileContents file = bufferEntry.Key; byte[] buffer = bufferEntry.Value; Streams | 417
Image of page 441

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

int bytesReadFromThisFile = 0; while (bytesReadFromThisFile < buffer.Length) { int bytesThisRead = file.Content.Read( buffer, bytesReadFromThisFile, buffer.Length - bytesReadFromThisFile); if (bytesThisRead == 0) { break; } bytesReadFromThisFile += bytesThisRead; } if (bytesReadFromThisFile < buffer.Length && bytesReadFromThisFile < bytesToRead) { throw new InvalidOperationException( "Unexpected end of file - did a file change?"); } bytesRead = bytesReadFromThisFile; // Will be same for all files } bytesToRead -= bytesRead; foreach (var sourceFileEntry in potentiallyMatched) { byte[] sourceFileContent = fileBuffers[sourceFileEntry.Key]; for (int otherIndex = 0; otherIndex < sourceFileEntry.Value.Count; ++otherIndex) { byte[] otherFileContent = fileBuffers[sourceFileEntry.Value[otherIndex]]; for (int i = 0; i < bytesRead; ++i) { if (sourceFileContent[i] != otherFileContent[i]) { sourceFileEntry.Value.RemoveAt(otherIndex); otherIndex -= 1; if (sourceFileEntry.Value.Count == 0) { sourceFilesWithNoMatches.Add(sourceFileEntry.Key); } break; } } } } foreach (FileContents fileWithNoMatches in sourceFilesWithNoMatches) { potentiallyMatched.Remove(fileWithNoMatches); } // Don't bother with the rest of the file if there are // not further potential matches if (potentiallyMatched.Count == 0) { break; } sourceFilesWithNoMatches.Clear(); 418 | Chapter 11: Files and Streams
Image of page 442
} } Rather than reading entire files at once, we allocate small buffers, and read in 1 KB at a time. As with the previous version, this new one works through all the files of a particular name and size simultaneously, so we allocate a buffer for each file. We then loop round, reading in a buffer’s worth from each file, and perform compar- isons against just that buffer (weeding out any nonmatches). We keep going round until we either determine that none of the files match or reach the end of the files. Notice how each stream remembers its position for us, with each Read starting where the previous one left off. And since we ensure that we read exactly the same quantity from all the files for each chunk (either 1 KB, or however much is left when we get to the end of the file), all the streams advance in unison.
Image of page 443

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 444
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern