Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:24:47+00:00 2026-05-13T08:24:47+00:00

A useful convenience introduced in .NET 4 is Stream.CopyTo(Stream[, Int32]) which reads the content

  • 0

A useful convenience introduced in .NET 4 is Stream.CopyTo(Stream[, Int32]) which reads the content from the current stream and writes it to another stream.

This obviates the need for slightly tedious code such as this:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    while (true)
    {
        int read = input.Read (buffer, 0, buffer.Length);
        if (read <= 0)
            return;
        output.Write (buffer, 0, read);
    }
}

Since I don’t have .NET 4 installed on this machine, I was wondering if someone who has .NET 4 installed could open up Reflector and show us how the Framework Class Library team implemented this method for .NET 4.

Compare and contrast their implementation with code snippet above. In particular, I’m interested to know what default buffer size was chosen.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:24:48+00:00Added an answer on May 13, 2026 at 8:24 am

    In .NET 4.5.1, it uses a fixed buffer size of 81920 bytes. (Earlier versions of .NET used a fixed buffer size of 4096 bytes, and it will no doubt continue to change over time.) There is also an overload where you can pass your own buffer size.

    The implementation is very similar to yours, modulo some shuffling around and some error checking. Reflector renders the heart of it as follows:

    private void InternalCopyTo(Stream destination, int bufferSize)
    {
      int num;
      byte[] buffer = new byte[bufferSize];
      while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
      {
        destination.Write(buffer, 0, num);
      }
    }
    

    (You can now see the actual source at http://referencesource.microsoft.com/#mscorlib/system/io/stream.cs#98ac7cf3acb04bb1.)

    The error checking is basically around whether input.CanRead and output.CanWrite are both true, or either is disposed. So in answer to Benny’s question this should be perfectly happy copying from a NetworkStream (or to a writable NetworkStream).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It would be useful to also know Disk Reads/Writes, Memory, CPU, etc. statistics for
Which useful (for performance or otherwise) constructions are valid bytecode, but not expressable in
I found this post useful and important to share, so if anybody has problems
I've used this useful JQuery function that serializes nested elements. the problem is how
I found user controls to be incredibly useful when working with ASP.NET webforms. By
I'm trying to implement cycling iterator, which is quite useful in my problem. According
Suppose I need to inherit from two classes in C# . This is not
I read from < Essential ASP.NET with Examples in C# > the following statement:
Objective-C categories are extremely useful, but there are some problems with this power. These
I am learning Ruby & Perl has this very convenient module called Data::Dumper, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.