StreamReader.ReadToEnd() 返回一个空字符串

StreamReader.ReadToEnd() returning an empty string(StreamReader.ReadToEnd() 返回一个空字符串)
本文介绍了StreamReader.ReadToEnd() 返回一个空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有办法

private static String DecompressAndDecode(byte[] data)
{
   GZipStream decompressor = new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
   StreamReader decompressed = new StreamReader(decompressor, Encoding.UTF8);
   String result = decompressed.ReadToEnd();
   return result;
}

我有一些 GZipped 文本作为输入,结果应该是该文本的字符串表示.问题是该方法返回一个空字符串.令我困惑的是,当我在调试模式下逐步执行该方法并到达 return 语句时,结果变量是一个空字符串,但是如果我为 decompressed.ReadToEnd() 表达式创建一个监视,它会返回文本.在这一点上,我期望的是包含文本的结果变量和计算为空字符串的 decompressed.ReadToEnd() 表达式.(重新评估 decompressed.ReadToEnd() 表达式按预期返回空字符串).

I have some GZipped text as input and the result is supposed to be a String representation of this text. The problem is that the method returns an empty string. What is puzzling me is that when I step trough the method in debug mode and reach the return statement the result variable is an empty string but if I create a watch for the decompressed.ReadToEnd() expression it returns me the text. What I would expect at this point is the result variable to contain the text and the decompressed.ReadToEnd() expression evaluating to an empty string. (Reevaluating the decompressed.ReadToEnd() expression returns an empty string as expected).

@Edit:我发现在我的例子中 ReadToEnd() 在第二次调用时返回文本在第一次调用时返回空字符串调用并在第二个调用之后.

@ I have found that in my case ReadToEnd() returns the text on the second call returning empty strings on the first call and after the second call.

这里肯定有一些明显的东西我遗漏了.

There must be something obvious I'm missing here.

推荐答案

我认为你的问题是指针在 Steam 中的位置.每次执行 ReadToEnd 后,指针都会设置到末尾,这就是您可以第一次观看的原因.

I think your problem is the position of the pointer in the steam. Each time after you perform the ReadToEnd, the pointer is set to the end that is why you can watch it first time.

ReadToEnd 之前运行以下代码,将指针设置为开头.someStream.Seek(0, SeekOrigin.Begin)

Run the following code before ReadToEnd to set the pointer to the beginning. someStream.Seek(0, SeekOrigin.Begin)

这篇关于StreamReader.ReadToEnd() 返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Why do I get quot;error: ... must be a reference typequot; in my C# generic method?(为什么我会收到“错误:...必须是引用类型?在我的 C# 泛型方法中?)