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

  • Home
  • SEARCH
  • 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 69203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:32:12+00:00 2026-05-10T19:32:12+00:00

What is the prefered method for creating a byte array from an input stream?

  • 0

What is the prefered method for creating a byte array from an input stream?

Here is my current solution with .NET 3.5.

Stream s; byte[] b;  using (BinaryReader br = new BinaryReader(s)) {     b = br.ReadBytes((int)s.Length); } 

Is it still a better idea to read and write chunks of the stream?

  • 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. 2026-05-10T19:32:12+00:00Added an answer on May 10, 2026 at 7:32 pm

    It really depends on whether or not you can trust s.Length. For many streams, you just don’t know how much data there will be. In such cases – and before .NET 4 – I’d use code like this:

    public static byte[] ReadFully(Stream input) {     byte[] buffer = new byte[16*1024];     using (MemoryStream ms = new MemoryStream())     {         int read;         while ((read = input.Read(buffer, 0, buffer.Length)) > 0)         {             ms.Write(buffer, 0, read);         }         return ms.ToArray();     } } 

    With .NET 4 and above, I’d use Stream.CopyTo, which is basically equivalent to the loop in my code – create the MemoryStream, call stream.CopyTo(ms) and then return ms.ToArray(). Job done.

    I should perhaps explain why my answer is longer than the others. Stream.Read doesn’t guarantee that it will read everything it’s asked for. If you’re reading from a network stream, for example, it may read one packet’s worth and then return, even if there will be more data soon. BinaryReader.Read will keep going until the end of the stream or your specified size, but you still have to know the size to start with.

    The above method will keep reading (and copying into a MemoryStream) until it runs out of data. It then asks the MemoryStream to return a copy of the data in an array. If you know the size to start with – or think you know the size, without being sure – you can construct the MemoryStream to be that size to start with. Likewise you can put a check at the end, and if the length of the stream is the same size as the buffer (returned by MemoryStream.GetBuffer) then you can just return the buffer. So the above code isn’t quite optimised, but will at least be correct. It doesn’t assume any responsibility for closing the stream – the caller should do that.

    See this article for more info (and an alternative implementation).

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

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Monotouch uses Ahead-Of-Time compilation to produce a single statically compiled… May 12, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer I assume that you want to have a screen with… May 12, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer Well, actually localizing a form is not that hard. You… May 12, 2026 at 1:58 pm

Related Questions

Ok! I'm ready to embark on some .NET development for the first time. I've
Does anyone have some good information on the usage of the .SaveChanges() method? I
I am creating several XmlElements used in a larger XML Document. XmlElement thisElement =
I'm having some trouble returning multiple values in this program that calculates min, max,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.