我如何“分叉” .NET 中的流?

发布于 2024-08-08 16:24:19 字数 812 浏览 6 评论 0原文

正如之前讨论过< /a>,当 BinaryReader 或 BinaryWriter 关闭时,其底层 Stream 也会关闭(aargh)。考虑这种情况:例程 R 被传递给 MemoryStream,例如 M;我想向 M 写入一些内容,然后将其传递给另一个例程以进行更多处理(不一定是写入)。为了方便起见,我想将 M 包装在 BinaryWriter 中来进行写入。编写完成后,我已经完成了 BinaryWriter,但还没有完成 M

void R(MemoryStream M)
{
    using (B = new BinaryWriter(M))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}

但是,我无法在不关闭 M 的情况下处理 BinaryStream。

问:有没有办法执行以下任一操作?

  • 从 MemoryStream 中提取底层 byte[],
  • 克隆 Stream
  • 在关闭后重新打开 Stream

As discussed before, when a BinaryReader or BinaryWriter gets closed, its underlying Stream get closed as well (aargh). Consider this situation: a routine R is passed a MemoryStream, say M; I would like to write some stuff to M and then pass it to another routine for more processing (not necessarily writing). For convenience, I'd like to wrap M in a BinaryWriter to do my writing. After writing, I'm done with the BinaryWriter but not with M.

void R(MemoryStream M)
{
    using (B = new BinaryWriter(M))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}

But, I can't dispose of the BinaryStream without closing M.

Q: Is there a way to do any of the following?

  • extract the underlying byte[] from a MemoryStream,
  • clone a Stream
  • reopen a Stream after it's been closed

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

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

发布评论

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

评论(7

遇见了你 2024-08-15 16:24:19

您最好使用 获取底层 byte[] 缓冲区

byte[] buffer = ms.GetBuffer();

,然后使用 Array.Copy() 方法复制字节数据。
您可以自由地用它创建一个新的流。

You should better get the underlying byte[] buffer using

byte[] buffer = ms.GetBuffer();

And then copy the byte data using the Array.Copy() method.
You are free to create a new stream with it.

愚人国度 2024-08-15 16:24:19

您可以在 MiscUtil< 中使用诸如 MiscUtil.IO.NonClosingStreamWrapper 之类的东西/a>,它包装 Stream 并简单地忽略 Close/Dispose 请求。就是为了这个目的。

void R(MemoryStream M)
{
    using (B = new BinaryWriter(new NonClosingStreamWrapper(M)))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}    

You can use things like the MiscUtil.IO.NonClosingStreamWrapper in MiscUtil, which wraps a Stream and simply ignores Close/Dispose requests. For just this purpose.

void R(MemoryStream M)
{
    using (B = new BinaryWriter(new NonClosingStreamWrapper(M)))
    {
        // write some stuff using B
    }

    S(M);  // now pass M to another routine for further processing
}    
玩世 2024-08-15 16:24:19

您可以:

  • 调用 M.ToArray() 以字节数组形式获取流。
  • 子类 BinaryWriter 并重写 Dispose 方法以防止关闭子流

You can:

  • Call M.ToArray() to get the stream as an array of bytes.
  • Subclass BinaryWriter and override the Dispose method to prevent closing of the child stream
白首有我共你 2024-08-15 16:24:19

感谢几个建议 ToArray 的人,我得到了正确的答案,即“M.GetBuffer”。 ToArray 还不错,但它

  • 使副本
  • 仅获取缓冲区的一部分

GetBuffer 只是获取对底层 byte[] 的引用,这就是我所追求的。

Thanks to several who suggested ToArray, I was led to right answer, which is `M.GetBuffer'. ToArray is not too bad, but it

  • makes a copy
  • gets only part of the buffer

GetBuffer just grabs a reference to the underlying byte[], which is what I'm after.

夏夜暖风 2024-08-15 16:24:19

只是将其添加到此处,一个非常简单的解决方案是 编写器 Dispose() 。

void R(MemoryStream M)
{
    B = new BinaryWriter(M);

    // write some stuff using B        
    B.Flush();
    S(M);  // now pass M to another routine for further processing
}

现在您只需担心将 B 保留在范围内,即在 R() 期间。

这可能不是最好的解决方案,但值得注意的是,读者和作者不需要自行处理。

Just to add it in here, a very simple solution would be not to Dispose() the writer.

void R(MemoryStream M)
{
    B = new BinaryWriter(M);

    // write some stuff using B        
    B.Flush();
    S(M);  // now pass M to another routine for further processing
}

Now you only have to worry about keeping B in scope, which it will be during R().

This may not be the best solution here, but it is worth noting that the Readers and Writers don't need Disposing themselves.

寂寞花火° 2024-08-15 16:24:19

一种有点幼稚的方法是使用

byte buf[] = MemoryStream.ToArray();

将流内容复制到字节数组。您可以将其转回流

MemoryStream ms = new MemoryStream(buf);

A somewhat naive approach is to use

byte buf[] = MemoryStream.ToArray();

To copy the stream contents to a byte array. You can turn it back into a stream with

MemoryStream ms = new MemoryStream(buf);
单调的奢华 2024-08-15 16:24:19

根据这个 M.Clone();应该有效。但我可能错了...

Accoring to this M.Clone(); should work. But i may be wrong...

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