如何检查 C# Stream 的大小是否可调整?

发布于 2024-11-30 20:09:08 字数 559 浏览 0 评论 0原文

来自 http://msdn。 microsoft.com/en-us/library/system.io.memorystream%28v=VS.90%29.aspx

使用无符号字节数组创建的内存流提供了 不可调整大小的数据流。当使用字节数组时,您可以 既不追加也不缩小流,尽管您可能可以 根据传入的参数修改现有内容 构造函数。空内存流的大小是可以调整的,并且可以 写入和读取。

当提供对 MemoryStream (甚至只是一个 Stream)的引用时,如何检查它是否可调整大小?

使用 OpenXML SDK 时会出现这种情况,它要求传递给它的流可以调整大小。我可以通过深度复制到可调整大小的流来确保可调整大小,我只是想知道是否有特殊原因导致库在向其传递错误参数(即不可调整大小的流)时不抛出异常。

From http://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=VS.90%29.aspx:

Memory streams created with an unsigned byte array provide a
non-resizable stream of the data. When using a byte array, you can
neither append to nor shrink the stream, although you might be able to
modify the existing contents depending on the parameters passed into
the constructor. Empty memory streams are resizable, and can be
written to and read from.

When provided a reference to a MemoryStream (or even just a Stream), how can one check if it's resizable?

The situation arose when using the OpenXML SDK, which requires the streams passed to it be resizable. I can ensure resizability by deep copying to a resizable stream, I'm just wondering if there's a particular reason why the library doesn't throw an exception when a bad parameter is passed to it (i.e. an unresizable stream).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-12-07 20:09:08

这是一种在尝试调整大小时捕获 NotSupportedException 的丑陋方法。

public static bool IsResizable(this Stream stream)
    {
        bool result;
        long oldLength = stream.Length;

        try
        {
            stream.SetLength(oldLength + 1);
            result = true;
        }
        catch (NotSupportedException)
        {
            result = false;
        }

        if (result)
        {
            stream.SetLength(oldLength);
        }

        return result;
    }

Here's one kinda ugly way were you catch the NotSupportedException when attempting to resize.

public static bool IsResizable(this Stream stream)
    {
        bool result;
        long oldLength = stream.Length;

        try
        {
            stream.SetLength(oldLength + 1);
            result = true;
        }
        catch (NotSupportedException)
        {
            result = false;
        }

        if (result)
        {
            stream.SetLength(oldLength);
        }

        return result;
    }
失与倦" 2024-12-07 20:09:08

编辑:以下解决方案不适用于使用带有最后一个参数的 MemoryStream(byte[], int, int, bool, bool) 构造函数创建的 MemoryStreams < code>publiclyVisible 设置为 true


根据 MSDNMemoryStream。如果对象不是使用“公开可见的缓冲区”创建的,GetBuffer 将抛出 UnauthorizedAccessException。列出的具有公开可见缓冲区的构造函数可以方便地映射到可调整大小的相同构造函数。因此,您可以通过检查 GetBuffer 来检查它的大小是否可调整:

public static bool IsResizable(this MemoryStream stream)
{
    if (stream == null) throw new ArgumentNullException("stream");

    bool resizable;
    try
    {
        stream.GetBuffer();
        resizable = true;
    }
    catch (UnauthorizedAccessException) { resizable = false; }

    return resizable;
}

Edit: the below solution doesn't work for MemoryStreams that were created using the MemoryStream(byte[], int, int, bool, bool) constructor with the last parameter publiclyVisible set to true.


According to MSDN, MemoryStream.GetBuffer will throw an UnauthorizedAccessException if the object was not created with a "publicly visible buffer". The constructors listed with publicly visible buffers conveniently map to the sames ones that are resizable. So you could check that it's re sizable by checking GetBuffer:

public static bool IsResizable(this MemoryStream stream)
{
    if (stream == null) throw new ArgumentNullException("stream");

    bool resizable;
    try
    {
        stream.GetBuffer();
        resizable = true;
    }
    catch (UnauthorizedAccessException) { resizable = false; }

    return resizable;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文