开发者

How to find out whether a memory stream is filled properly

开发者 https://www.devze.com 2023-03-09 08:20 出处:网络
The code below tries to fill the message byte array with some simple text until the buffer is filled.

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

精彩评论

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