Question: I know this question has been asked before on Stackoverflow, but could not find an explanation.

When I try to read a string from a compressed byte array I get an empty string on the first attempt, on the second I succed and get the string.

Code example:

```
public static string Decompress(byte[] gzBuffer)
{
    if (gzBuffer == null)
        return null;
    using (var ms = new MemoryStream(gzBuffer))
    {
        using (var decompress = new GZipStream(ms, CompressionMode.Decompress))
        {
            using (var sr = new StreamReader(decompress, Encoding.UTF8))
            {
                string ret = sr.ReadToEnd();
                // this is the extra check that is needed !?
                if (ret == "")
                    ret = sr.ReadToEnd();
                return ret;
            }
        }
    }
}

```

All suggestions are appreciated. 
- Victor Cassel

Answer: 
try adding `ms.Position = 0` before `string ret = sr.ReadToEnd();`
[The MSDN Page on the function](http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend.aspx) mentions the following:

> 
> If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. 
> 
> 
> 

Perhaps try calling `DiscardBufferedData()` before your `ReadToEnd()` and see what it does (I know you aren't getting the exception, but it's all I can think of...)?