开发者

How to find out whether a memory stream is filled properly

The code below tries to fill the message byte array with some simple text until the buffer is filled.

byte[] message = new byte[1024];

using (MemoryStream memoryStream = new MemoryStream(message, true))
    {
        using (StreamWriter streamWriter = new StreamWrite开发者_如何学Cr(memoryStream, Encoding.ASCII))
        {
            while (???)
                streamWriter.WriteLine("Hello World!");
        }
    }

What should be in the while(???) statement?


byte[] message = new byte[1024];
using (MemoryStream memoryStream = new MemoryStream(message, true))
{
    using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
    {
        var lineToAdd = "Hello World!";
        while (memoryStream.Length - memoryStream.Position > lineToAdd.Length)
        {
            streamWriter.WriteLine(lineToAdd);
            streamWriter.Flush();
        }
    }
}

OR

using (MemoryStream memoryStream = new MemoryStream(message, true))
using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
{
    streamWriter.AutoFlush = true;
    var lineToAdd = "Hello World!";
    while (memoryStream.Length - memoryStream.Position > lineToAdd.Length)
        streamWriter.WriteLine(lineToAdd);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜