简体   繁体   中英

Trying to understand Data transfer in byte[] Array C#

First I would like to say that I tried to do research on how to understand what I'm about to ask but just couldn't come up with what I was looking for. This being said I thought I would ask some of you crazy smart people to explain this in lamens terms for me as best as possible.

My issue is that I have a perfectly good "Copy Pasted" code to download a file using ftpWebRequest. I will paste the code below:

        public static void DownloadFiles()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://****/test.zip");
        request.Credentials = new NetworkCredential("****", "*****");
        request.UseBinary = true; 
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        FileStream writer = new FileStream("***/test.zip", FileMode.Create);

        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];

        readCount = responseStream.Read(buffer, 0, bufferSize);

        while (readCount > 0)
        {
            writer.Write(buffer, 0, readCount);                
            readCount = responseStream.Read(buffer, 0, bufferSize);                
        }

        responseStream.Close();
        response.Close();
        writer.Close();
    }

Like I said this works perfectly and downloads the zip file without any issues. What I'm trying to understand because I think "Copy Paste" code is a horrible idea is how this is actually functinning. The only part that I do not understand is what is actually happening between these points:

            int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];

        readCount = responseStream.Read(buffer, 0, bufferSize);

        while (readCount > 0)
        {
            writer.Write(buffer, 0, readCount);                
            readCount = responseStream.Read(buffer, 0, bufferSize);                
        }

If someone would be so kind to please explain what is actually happening in the buffer, why we set it to 2048 and what the while loop is actually doing would be very appreciated. Just as a side not I have gone through the code and put message boxes and other debug's in to try and understand whats going on but without success. Thank you in advance and I apologize if this seems very elementary.

data is read from the stream in 2 kB chunks and written to file:

int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[2048]; // a buffer is created

// bytes are read from stream into buffer. readCount contains
// number of bytes actually read. it can be less then 2048 for the last chunk.
// e.g if there are 3000 bytes of data, first Read will read 2048 bytes
// and second will read 952 bytes
readCount = responseStream.Read(buffer, 0, bufferSize); 

while (readCount > 0) // as long as we have read some data
{
  writer.Write(buffer, 0, readCount); // write that many bytes from buffer to file               
  readCount = responseStream.Read(buffer, 0, bufferSize); // then read next chunk               
}

The code reads the contents of the responseStream into the buffer array and writes it out to the writer . The while loop is necessary because when you call Read() on a stream, there's no guarantee it will read enough bytes to fill the buffer. The stream only guarantees that, if there's anything to read (in other words, you haven't reached the end of the stream) it will read at least one byte. Whatever it does, it will return the actual number of bytes read. So, you have to keep calling Read() until it returns 0, meaning that you've reached the end of the stream. The size of the buffer is arbitrary. You could use a buffer of length 1 but there might be performance benefits to using something around 2-4kb (due to typical sizes of network packets or disk sectors) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM