字符串转换为内存流 - 内存流不可扩展? [英] convert string to memory stream - Memory stream is not expandable?

查看:1938
本文介绍了字符串转换为内存流 - 内存流不可扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个字符串到内存流,
,但与错误消息失败:

 内存流不可扩展。 



代码,产生这个问题的行:

  context.Response.Filter =新System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(我的空间)); 



任何人有一种变通方法/修复是什么?



堆栈跟踪:

  [NotSupportedException异常:内存流不可扩展] 
System.IO.MemoryStream.set_Capacity (的Int32值)9385744
System.IO.MemoryStream.EnsureCapacity(的Int32值)+50
System.IO.MemoryStream.Write(字节[]缓冲区,偏移的Int32,的Int32计数)+265
System.Web.HttpWriter.FilterIntegrated(布尔finalFiltering,IIS7WorkerRequest WR)9155697
System.Web.HttpResponse.FilterOutput()+159
System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep .Execute()+52
System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器; completedSynchronously)+75


< DIV CLASS =h2_lin>解决方案

这追加的数据会比较合适的自定义数据流。



微创测试。 。假设你想要的文字写入时流冲洗,然后只有一次

 公共类AppendTextFilter:流
{
私人流过滤器{搞定;组; }
私人字符串文本{搞定;组; }
私人布尔TextWritten {搞定;组; }

公共AppendTextFilter(流过滤器,字符串文本)
{
this.Filter =过滤器;
this.Text =文本;
}

公众覆盖布尔{的CanRead获得{返回Filter.CanRead; }}

公众覆盖布尔CanSeek {{返回Filter.CanSeek; }}

公众覆盖布尔CanWrite {{返回Filter.CanWrite; }}

公共覆盖无效的flush()
{
如果(!TextWritten)
{
VAR字节= Encoding.UTF7.GetBytes(文本) ;
Filter.Write(字节,0,bytes.Length);
TextWritten = TRUE;
}
Filter.Flush();
}

公众覆盖长度长{{返回Filter.Length + Text.Length; }}

公众覆盖多头头寸
{
得到
{
返回Filter.Position;
}

{
Filter.Position =价值;
}
}

公共覆盖INT读(字节[]缓冲区,诠释抵消,诠释计数)
{
返回Filter.Read(缓冲,偏移,计数);
}

公众覆盖寻求长(长偏移,SeekOrigin原点)
{
返回Filter.Seek(偏移,产地来源);
}

公共覆盖无效SetLength函数(长值)
{
Filter.SetLength(值);
}

公共覆盖无效写入(字节[]缓冲区,诠释抵消,诠释计数)
{
Filter.Write(缓冲区,偏移数);
}
}


i was trying to write a string to a memory stream, but failed with the error message:

Memory stream is not expandable.

the line of code that produces this problem:

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

anyone have a workaround/fix for that?

stacktrace:

[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

解决方案

A custom stream that appends the data would be more appropriate.

Minimally tested. Assumes that you want the text written when the stream is flushed, and then only once.

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

这篇关于字符串转换为内存流 - 内存流不可扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆