如何查明内存流是否已正确填充

本文关键字:填充 是否 何查明 内存 | 更新日期: 2023-09-27 17:57:47

下面的代码试图用一些简单的文本填充message字节数组,直到缓冲区被填满。

byte[] message = new byte[1024];
using (MemoryStream memoryStream = new MemoryStream(message, true))
    {
        using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
        {
            while (???)
                streamWriter.WriteLine("Hello World!");
        }
    }

while(???)语句中应该包含什么?

如何查明内存流是否已正确填充

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

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