Escrever StringBuilder para Corrente
-
19-09-2019 - |
Pergunta
O que é o melhor método de escrever um StringBuilder para uma System.IO.Stream?
Atualmente, estou fazendo:
StringBuilder message = new StringBuilder("All your base");
message.Append(" are belong to us");
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
stream.Write(encoder.GetBytes(message.ToString()), 0, message.Length);
Solução
Não use um StringBuilder, se você está escrevendo para um fluxo, fazer isso com um StreamWriter :
using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream ))
{
// Various for loops etc as necessary that will ultimately do this:
writer.Write(...);
}
Outras dicas
Este é o melhor método. Outra perda sábio o StringBuilder e usar algo como a seguir:
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(ms, Encoding.Unicode))
{
sw.WriteLine("dirty world.");
}
//do somthing with ms
}
Talvez seja útil.
var sb= new StringBuilder("All your money");
sb.Append(" are belong to us, dude.");
var myString = sb.ToString();
var myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
var ms = new MemoryStream(myByteArray);
// Do what you need with MemoryStream
Dependendo do seu caso de uso também pode fazer sentido para começar com um StringWriter:
StringBuilder sb = null;
// StringWriter - a TextWriter backed by a StringBuilder
using (var writer = new StringWriter())
{
writer.WriteLine("Blah");
. . .
sb = writer.GetStringBuilder(); // Get the backing StringBuilder out
}
// Do whatever you want with the StringBuilder
Se você quiser usar algo como um StringBuilder porque é mais limpo para passar ao redor e trabalhar com, então você pode usar algo como o seguinte StringBuilder alternativo que eu criei.
A coisa mais importante que faz diferente é que ele permite o acesso aos dados internos sem ter que montá-lo em uma String ou ByteArray em primeiro lugar. Isto significa que você não tem que dobrar os requisitos de memória e risco tentando alocar um pedaço contíguo de memória que se encaixa todo o seu objeto.
NOTA:. Estou certo de que há melhores opções, em seguida, usando um List<string>()
internamente, mas esta era simples e provou ser bom o suficiente para os meus propósitos
public class StringBuilderEx
{
List<string> data = new List<string>();
public void Append(string input)
{
data.Add(input);
}
public void AppendLine(string input)
{
data.Add(input + "\n");
}
public void AppendLine()
{
data.Add("\n");
}
/// <summary>
/// Copies all data to a String.
/// Warning: Will fail with an OutOfMemoryException if the data is too
/// large to fit into a single contiguous string.
/// </summary>
public override string ToString()
{
return String.Join("", data);
}
/// <summary>
/// Process Each section of the data in place. This avoids the
/// memory pressure of exporting everything to another contiguous
/// block of memory before processing.
/// </summary>
public void ForEach(Action<string> processData)
{
foreach (string item in data)
processData(item);
}
}
Agora você pode despejar todo o conteúdo para arquivo usando o seguinte código.
var stringData = new StringBuilderEx();
stringData.Append("Add lots of data");
using (StreamWriter file = new System.IO.StreamWriter(localFilename))
{
stringData.ForEach((data) =>
{
file.Write(data);
});
}