Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "fork" a Stream in .NET?

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
like image 810
I. J. Kennedy Avatar asked Oct 27 '09 21:10

I. J. Kennedy


People also ask

How do I set the upstream repository for a fork?

To do this, you'll need to use Git on the command line. You can practice setting the upstream repository using the same octocat/Spoon-Knife repository you just forked. On GitHub.com, navigate to the octocat/Spoon-Knife repository. In the top-right corner of the page, click Fork . To learn more about GitHub CLI, see " About GitHub CLI ."

How do I check if my fork is synced with upstream?

To verify the new upstream repository you've specified for your fork, type git remote -v again. You should see the URL for your fork as origin, and the URL for the original repository as upstream. Now, you can keep your fork synced with the upstream repository with a few Git commands.

How do I create a fork on GitHub?

On GitHub.com, navigate to the octocat/Spoon-Knife repository. In the top-right corner of the page, click Fork . To learn more about GitHub CLI, see " About GitHub CLI ." To create a fork of a repository, use the gh repo fork subcommand. To create the fork in an organization, use the --org flag.

How do I fork spoon-knife on GitHub?

On GitHub.com, navigate to the octocat/Spoon-Knife repository. In the top-right corner of the page, click Fork . To learn more about GitHub CLI, see " About GitHub CLI ."


3 Answers

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.

like image 92
Ricardo Amores Avatar answered Oct 21 '22 13:10

Ricardo Amores


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
}    
like image 36
Marc Gravell Avatar answered Oct 21 '22 14:10

Marc Gravell


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
like image 34
Chris Avatar answered Oct 21 '22 14:10

Chris