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);
}
精彩评论