问题描述
我有一种将文件转换为字节的方法,以便以后可以通过Internet发送.无论如何,因为我打算发送大文件,所以我发送文件块而不是发送整个文件.每个块都由字节数组(byte [])组成.我对这一切还是陌生的,所以我想在发送之前将每个块保存在块列表(List)中.所以我的课看起来像这样:
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;
}
}
}
}
当我执行时:
SomeClass s = new SomeClass();
s.test(@"A:\Users\Tono\Desktop\t.iso");
binaryFileList列表填充了以下文件块:A:\ Users \ Tono \ Desktop \ t.iso
binaryFileList List gets populated with chunks of the file: A:\Users\Tono\Desktop\t.iso
现在,当我试图从该数据创建文件时,问题就来了.调试时,我注意到问题是因为在我输入数据时,binaryFileList中的项目发生了变化.让我告诉你我的意思:
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:
请注意,在此调试中,这是我第一次将项目添加到binaryFileList.而且您还可以在数组中看到该项目的每个字节...
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...
现在,我将让该方法运行更多次,将更多项目添加到binaryFileList.
now I will let the method run more times adding more items to binaryFileList.
因此,binaryFileList现在有278个项目,而不是最后一张图片中的一个:
so now binaryFileList has 278 items instead of one like on the last picture:
那么到目前为止一切看起来还好吧?但是你们是否还记得binaryFileList的第一项包含几乎全为0的字节数组?看看binaryFileList的第一项:
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:
随着我不断将项目添加到binaryFileList,请注意第一项如何更改:
and as I keep adding items to binaryFileList note how the first item changes:
换句话说,binaryFileList是字节[]的列表.当我向binaryFileList添加一个byte []时,其他byte []不应更改.他们确实改变了!为什么!?
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!?
推荐答案
下面的代码行必须放在循环内:
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.
这篇关于当我添加新数据时,列表项也会发生变化...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!