简体   繁体   English

试图了解字节[]数组C#中的数据传输

[英]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. 我的问题是我有一个很好的“复制粘贴”代码,可以使用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. 就像我说的那样,这非常有效,并且可以下载zip文件,没有任何问题。 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. 如果有人愿意解释缓冲区中实际发生的情况,为什么我们将其设置为2048以及while循环实际上在做什么,将不胜感激。 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: 从流中以2 kB块读取数据并将其写入文件:

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 . 该代码将responseStream的内容读入buffer数组并将其写出到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. while循环是必需的,因为当您在流上调用Read()时,无法保证它将读取足够的字节来填充缓冲区。 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. 因此,您必须继续调用Read()直到它返回0,这意味着您已经到达流的末尾。 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) . 您可以使用长度为1的缓冲区,但是使用大约2-4kb的内容可能会带来性能上的好处(由于网络数据包或磁盘扇区的典型大小)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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