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 6363065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:59:19+00:00 2026-05-24T23:59:19+00:00

I have a method that converts a file to bytes so that I can

  • 0

I have a method that converts a file to bytes so that I can later send it over the internet. anyways because I plan to send large files I send chunks of files instead of sending the whole file. each chunk consist of an array of bytes (byte[]) . I am new to all this so I wanted to save each chunk in an List of chunks ( List ) before sending it . so my class looks like:

public class SomeClass
{

    public List<byte[]> binaryFileList;

    public void SendChunk(byte[] data, int index)
    {
        binaryFileList.Add(data);
        // later I will add code in here to do something with data
    }

    public void test(string path)
    {
        binaryFileList = new List<byte[]>();

        System.IO.FileStream stream = new System.IO.FileStream(path,
            System.IO.FileMode.Open, System.IO.FileAccess.Read);

        var MaxChunkSize = 10000;
        byte[] chunk = new byte[MaxChunkSize];
        while (true)
        {
            int index = 0;
            // There are various different ways of structuring this bit of code.
            // Fundamentally we're trying to keep reading in to our chunk until
            // either we reach the end of the stream, or we've read everything we need.
            while (index < chunk.Length)
            {
                int bytesRead = stream.Read(chunk, index, chunk.Length - index);

                if (bytesRead == 0)
                {
                    break;
                }
                index += bytesRead;
            }
            if (index != 0) // Our previous chunk may have been the last one
            {
                SendChunk(chunk, index); // index is the number of bytes in the chunk
            }
            if (index != chunk.Length) // We didn't read a full chunk: we're done
            {
                return;
            }
        }


    }
}

and when I execute:

SomeClass s = new SomeClass();
s.test(@"A:\Users\Tono\Desktop\t.iso");

binaryFileList List gets populated with chunks of the file: A:\Users\Tono\Desktop\t.iso

Now the problem came when I tied to create a file from that data. when debuging I noticed that the problem was because items in binaryFileList changed as I entered data. let me show you what I mean:

enter image description here

notice that in this debug it is the first time I add an item to binaryFileList. and also you can see each byte of that item in the array…

now I will let the method run more times adding more items to binaryFileList.

so now binaryFileList has 278 items instead of one like on the last picture:

enter image description here

so everything so far looks ok right? but did you guys recall that the first item of binaryFileList contained an array of bytes with almost all 0’s? take a look at the first item of binaryFileList:

enter image description here

and as I keep adding items to binaryFileList note how the first item changes:

enter image description here

In other words binaryFileList is a list of byte[]. and when I add a byte[] to binaryFileList other byte[] should not change. they do change! why!?

  • 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-24T23:59:20+00:00Added an answer on May 24, 2026 at 11:59 pm

    The following line has to go inside the loop:

    byte[] chunk = new byte[MaxChunkSize];
    

    You create the chunk only once and overwrite it each time with new data. What you store in you list, is just a reference to this chunk, not a copy of it.

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

Sidebar

Related Questions

I have and old(ish) C# method I wrote that takes a number and converts
i have a javascript method that takes a date: convert(new Date(02/20/2010); how can i
I have class method that returns a list of employees that I can iterate
I have a method that can return either a single object or a collection
I have this method that converts a signed or non-signed byte to int ,
I have a method that where I want to redirect the user back to
I have a method that takes an IQueryable. Is there a LINQ query that
I have a method that periodically (e.g. once in every 10 secs) try to
I have a method that's about ten lines of code. I want to create
I have a method that needs to accept an array of country names, and

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.