StreamReader.ReadToEnd() returning an empty string

2019-04-05 15:45发布

I have a method

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;
}

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: 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.

标签: .net stream
3条回答
beautiful°
2楼-- · 2019-04-05 16:01

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.

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

查看更多
在下西门庆
3楼-- · 2019-04-05 16:11

"There must be something obvious I'm missing here." - maybe, and so am I ;-)
Let's start with a little self-contained example and see where it differs from your actual code.

class SOTest
{
  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;
  }

  private static byte[] foo(string data)
  {
    MemoryStream dest = new MemoryStream();
    using (GZipStream compressor = new GZipStream(dest, CompressionMode.Compress))
    {
      using (StreamWriter sw = new StreamWriter(compressor))
      {
        sw.Write(data);
      }
    }
    return dest.GetBuffer();
  }


  static void Main()
  {
    System.Console.WriteLine(
      DecompressAndDecode(foo("Mary had a little lamb."))
    );
    return;
  }
}

prints Mary had a little lamb.

查看更多
Animai°情兽
4楼-- · 2019-04-05 16:19

Create your own custom function. It'll take the path as a parameter:

    static string read(string path)
    {
        StreamReader sr = new StreamReader(@path); 
        string txt = "";
        while (!sr.EndOfStream) {
            txt += sr.ReadLine() + "\n";
        }
        sr.Close();
        return txt;
    }

Then call it instead of the call to ReadToEnd(). I tested it, and it worked.

查看更多
登录 后发表回答