我的根本问题是,当using调用Disposea时StreamWriter,它也会处理BaseStream(同样的问题Close).
我有一个解决方法,但正如你所看到的,它涉及复制流.有没有办法这样做而不复制流?
这样做的目的是将字符串(最初从数据库中读取)的内容获取到流中,因此可以由第三方组件读取该流.
注意:我无法更改第三方组件.
public System.IO.Stream CreateStream(string value)
{
var baseStream = new System.IO.MemoryStream();
var baseCopy = new System.IO.MemoryStream();
using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
{
writer.Write(value);
writer.Flush();
baseStream.WriteTo(baseCopy);
}
baseCopy.Seek(0, System.IO.SeekOrigin.Begin);
return baseCopy;
}
Run Code Online (Sandbox Code Playgroud)
用作
public void Noddy()
{
System.IO.Stream myStream = CreateStream("The contents of this string are unimportant");
My3rdPartyComponent.ReadFromStream(myStream);
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我正在寻找一个名为的假想方法BreakAssociationWithBaseStream,例如
public System.IO.Stream CreateStream_Alternate(string value)
{
var baseStream = new System.IO.MemoryStream();
using (var writer …Run Code Online (Sandbox Code Playgroud) 假设我正在写几个文件到磁盘,介于2MB和5GB之间.什么是FileStream的合理缓冲区值?
使用几兆字节的缓冲区是否合理,还是应该坚持使用千字节缓冲区?
概观
我有一个客户端服务器,我streamWriter.WriteLine(messageToClient);在服务器上发送大量消息.该streamReader.ReadLine();客户端上阅读2000年长度的消息罚款.
这里的问题是streamWriter.Flush()对较大的消息没有影响,因为我的目标是服务器能够在缓冲区之间直接切换(具有流畅的服务器消息输出)而不是(服务器响应将混杂在一起)直到它达到缓冲区限制冲洗).
问题:我认为缓冲区是自动刷新的,因为它们已满?那么streamWriters的最大缓冲区大小是多少?
我已经测试使用更小messageToClient的流,并且流的不冲洗按预期工作.Aka服务器流一次向客户端输出一组小消息,大概是因为缓冲区已满.
c# ×3
buffer ×1
dispose ×1
filestream ×1
flush ×1
networking ×1
size ×1
stream ×1
streamwriter ×1
terminology ×1