将string转换为内存stream – 内存stream不可扩展?

我试图写一个string到内存stream,但失败的错误消息:

Memory stream is not expandable. 

产生这个问题的代码行:

 context.Response.Filter = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myPage)); 

任何人有一个解决方法/修复?

堆栈跟踪:

 [NotSupportedException: Memory stream is not expandable.] System.IO.MemoryStream.set_Capacity(Int32 value) +9385744 System.IO.MemoryStream.EnsureCapacity(Int32 value) +50 System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +265 System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +9155697 System.Web.HttpResponse.FilterOutput() +159 System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +52 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 

附加数据的自定义stream将更合适。

最小testing。 假设您希望在刷新stream时写入文本,然后只有一次。

 public class AppendTextFilter : Stream { private Stream Filter { get; set; } private string Text { get; set; } private bool TextWritten { get; set; } public AppendTextFilter( Stream filter, string text ) { this.Filter = filter; this.Text = text; } public override bool CanRead { get { return Filter.CanRead; } } public override bool CanSeek { get { return Filter.CanSeek; } } public override bool CanWrite { get { return Filter.CanWrite; } } public override void Flush() { if (!TextWritten) { var bytes = Encoding.UTF7.GetBytes( Text ); Filter.Write( bytes, 0, bytes.Length ); TextWritten = true; } Filter.Flush(); } public override long Length { get { return Filter.Length + Text.Length; } } public override long Position { get { return Filter.Position; } set { Filter.Position = value; } } public override int Read( byte[] buffer, int offset, int count ) { return Filter.Read( buffer, offset, count ); } public override long Seek( long offset, SeekOrigin origin ) { return Filter.Seek( offset, origin ); } public override void SetLength( long value ) { Filter.SetLength( value ); } public override void Write( byte[] buffer, int offset, int count ) { Filter.Write( buffer, offset, count ); } } 

下面的代码对我来说是正确的

 public class Foo { public static void Main() { var myPage = "test string"; var repo = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myPage)); } } 

看来正确的方法是使用默认的构造函数创buildMemoryStream

 var repo = new System.IO.MemoryStream(); 

然后写信给它

 var stringBytes = System.Text.Encoding.UTF8.GetBytes(myPage); repo.Write(stringBytes, 0, stringBytes.Length); 

如果您希望能够正常读取stream(例如,使用StreamReader),那么您还需要调用:

 repo.Seek(0, SeekOrigin.Begin); 

当你从一个字节数组创build一个MemoryStream ,你实际上是围绕着这个数组创build一个包装器。 这意味着stream的缓冲区一旦达到其容量就不能扩展。

但是, HttpResponse.Filter本质上是:一个filter 。 该文件指出:

当您创buildStream对象并将Filter属性设置为Stream对象时,Write发送的所有HTTP输出都将通过该filter。

所以数据最终被写入到MemoryStream 。 所以,这将有助于知道你想要达到什么,因为一个MemoryStream不会成为一个有用的filter…

  byte[] buffer = File.ReadAllBytes("test.xml"); XmlDocument doc = new XmlDocument(); using (MemoryStream output = new MemoryStream()) { using (MemoryStream ms = new MemoryStream(buffer )) { doc.Load(ms); } // Make changes to your memory stream here doc.Save(output);//Output stream has the changes. }