Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lfgrando/1b3b417cf31e8ae9e1bc24c9fb443292 to your computer and use it in GitHub Desktop.
Save lfgrando/1b3b417cf31e8ae9e1bc24c9fb443292 to your computer and use it in GitHub Desktop.
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