C
C#9mo ago
Relevant

StreamReader is always EndOfStream=true even before reading

//var response = httpClient.GetAsync(url);

using (var fs = new FileStream("000000_0.deflate", FileMode.Open))
{
//await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
//var response = httpClient.GetAsync(url);

using (var fs = new FileStream("000000_0.deflate", FileMode.Open))
{
//await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
So this code works when I run locally with a file name hard coded in. When I instead download the the file and load it into a FileStream, it's always at EndOfStream. Am I doing something wrong? Here's what the code would look like in the way it's NOT working:
var url = $"http://url.to.download/{fileName}";
var response = httpClient.GetAsync(url);

using (var fs = new FileStream(_fileDir + fileName, FileMode.Create))
{
await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
var url = $"http://url.to.download/{fileName}";
var response = httpClient.GetAsync(url);

using (var fs = new FileStream(_fileDir + fileName, FileMode.Create))
{
await response.Result.Content.CopyToAsync(fs);
using (var deflate = new ZLibStream(fs, CompressionMode.Decompress, true))
using (var reader = new StreamReader(deflate, Encoding.ASCII))
{
messages.Add($"EOS: {reader.EndOfStream}");
8 Replies
Relevant
RelevantOP9mo ago
is the CopyToAsync forcing it to the EOS? Looks like that might have been it. I closed the FileStream after copying, then opened a new FileStream to read the newly created file Seems a bit redundant, but I guess necessary?
Jimmacle
Jimmacle9mo ago
copying implies reading the whole stream, so it makes sense it would be at the end afterwards you can probably seek it back to the beginning instead of closing and reopening the file
Relevant
RelevantOP9mo ago
Yeah make sense. Thanks
leowest
leowest9mo ago
also since you're using await, use it on the getasync as well.
var response = await httpClient.GetAsync(url);
var response = await httpClient.GetAsync(url);
and
await response.Content.CopyToAsync(fs);
await response.Content.CopyToAsync(fs);
Relevant
RelevantOP9mo ago
why? I don't need to wait for that to complete until the copy
leowest
leowest9mo ago
what if your request failed? among other issues that can happen in between
Relevant
RelevantOP9mo ago
ok
leowest
leowest9mo ago
there is 2 separate awaits there one for the request header the other for the content
Want results from more Discord servers?
Add your server