将字符串转换为内存流 - 内存流是不可扩展的?

时间:2011-11-11 16:42:27

标签: c# memorystream

我试图将字符串写入内存流, 但失败并显示错误消息:

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

4 个答案:

答案 0 :(得分:79)

以下代码适用于我

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

执行此操作的正确方法似乎是使用默认构造函数

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

然后写信给它

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

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

repo.Seek(0, SeekOrigin.Begin);

答案 1 :(得分:4)

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

经过最低限度的测试。假设您希望在刷新流时写入文本,然后只需要一次。

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

答案 2 :(得分:3)

从字节数组创建MemoryStream时,实质上是围绕所述数组创建一个包装器。这意味着流的缓冲区一旦达到其容量就无法扩展。

但是,HttpResponse.Filter基本上是:过滤器。文档说明:

  

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

因此数据最终被写入MemoryStream。因此,确切地知道您要尝试实现的目标是有帮助的,因为MemoryStream不会成为有用的过滤器......

答案 3 :(得分:2)

            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.
            }