Created
May 31, 2017 19:21
-
-
Save lfgrando/1b3b417cf31e8ae9e1bc24c9fb443292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<byte[]> ReadChunks(string source) | |
{ | |
using (Stream stream = IsUrl(source) ? WebRequest.Create(source).GetResponse().GetResponseStream() : new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read)) | |
{ | |
const int sampleSize = 300; | |
var fileSize = GetFileSize(source); | |
if (fileSize > sampleSize) | |
fileSize = sampleSize; | |
var buffer = new byte[fileSize]; | |
if (stream == null) | |
throw new Exception($"Não foi possível ler o arquivo {Path.GetFileName(source)}"); | |
int totalRead = 0; | |
int bytesRead; | |
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
totalRead += bytesRead; | |
yield return buffer; | |
} | |
Console.WriteLine($"[{(IsUrl(source) ? "URL" : "FILE")}]\t Quantidade pela Stream.Read: {totalRead}."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment