.net 如何使用大字节数组?

.net 如何使用大字节数组?,.net,c#-4.0,bytearray,ienumerable,.net,C# 4.0,Bytearray,Ienumerable,我有一个大字节数组,我把它分成多字节数组,按1000字节进行处理。我使用的是IEnumerable,我可以使用foreach循环,但我想知道我使用的是IEnumerable中的哪个数组数。我可以得到总计数,但不知道我正在处理的字节数组的数量,所以有人能帮我找出如何实现这一点吗?如果你需要更多的细节,请告诉我 public static IEnumerable<byte[]> SplitSourceBytes(byte[] SrcBytes, int size) {

我有一个大字节数组,我把它分成多字节数组,按1000字节进行处理。我使用的是IEnumerable,我可以使用foreach循环,但我想知道我使用的是IEnumerable中的哪个数组数。我可以得到总计数,但不知道我正在处理的字节数组的数量,所以有人能帮我找出如何实现这一点吗?如果你需要更多的细节,请告诉我

    public static IEnumerable<byte[]> SplitSourceBytes(byte[] SrcBytes, int size)
    {
        int srcLenght = SrcBytes.Length;
        byte[] source = null;

        int i = 0;
        for (; srcLenght > (i + 1) * size; i++)
        {
            source = new byte[size];
            Array.Copy(SrcBytes, i * size, source, 0, size);
            yield return source;
        }

        int sourceLeft = srcLenght - i * size;
        if (sourceLeft > 0)
        {
            source = new byte[sourceLeft];
            Array.Copy(SrcBytes, i * size, source, 0, sourceLeft);
            yield return source;
        }
    }
公共静态IEnumerable SplitSourceBytes(字节[]SrcBytes,int size)
{
int srcLenght=SrcBytes.Length;
字节[]源=空;
int i=0;
对于(;srcLenght>(i+1)*大小;i++)
{
source=新字节[大小];
复制(SrcBytes,i*size,source,0,size);
收益回报源;
}
int sourceLeft=srcLenght-i*size;
如果(sourceLeft>0)
{
source=新字节[sourceLeft];
Copy(SrcBytes,i*size,source,0,sourceLeft);
收益回报源;
}
}

这可能满足您的需要

        private Dictionary<int,byte[]> SplitByteArray(byte[] SrcBytes, int size)
        {
            var result = new Dictionary<int, byte[]>();
            if (SrcBytes.Length > size)
            {
                int position = SrcBytes.Length;
                int index = 0;
                for (int i = 0; i < SrcBytes.Length; i += size)
                {
                    int length = Math.Min(size, position);
                    byte[] buffer = new byte[length];
                    Buffer.BlockCopy(SrcBytes, i, buffer, 0, length);
                    result.Add(index, buffer);
                    position -= size;
                    index++;
                }
            }
            else
            {
                result.Add(0, SrcBytes);
            }
            return result;
        }
private Dictionary SplitByteArray(byte[]SrcBytes,int size)
{
var result=newdictionary();
如果(SrcBytes.Length>size)
{
int position=SrcBytes.Length;
int指数=0;
for(int i=0;i
您不需要创建新的字节数组;您可以使用
ArraySegment
枚举来“拆分”大数组,而不会受到新字节数组分配的惩罚

public static IEnumerable<ArraySegment<byte>> SplitSourceBytes(byte[] SrcBytes, int size)
{
    int srcLength = SrcBytes.Length;
    int alreadyReturned = 0;
    while (alreadyReturned < srcLength)
    {
        int count = Math.Min(srcLength - alreadyReturned, size);
        yield return new ArraySegment<byte>(SrcBytes, alreadyReturned, count);
        alreadyReturned += count;
    }
}
公共静态IEnumerable SplitSourceBytes(字节[]SrcBytes,int size)
{
int srcLength=SrcBytes.Length;
int-alreadyReturned=0;
while(alreadyReturned
要使用它,代码将类似于下面的代码:

foreach (var segment in SplitSourceBytes(bytes, 1000)) {
    int maxAttempts = 3;
    int attempts = 0;
    bool dataSent = false;
    while (!dataSent && attempts < maxAttempts)
    {
        try
        {
            serverStream.Write(segment.Array, segment.Offset, segment.Count);
            dataSent = true;
        }
        catch (Exception ex)
        {
            // Error, try to retransmit
            attempts++;
        }
    }
}
foreach(以SplitSourceBytes(字节,1000)为单位的var段){
int=3;
int=0;
bool-dataSent=false;
而(!dataSent&&truments
您可以将数组放在提供计数功能的列表中

public static IEnumerable<byte[]> SplitSourceBytes(byte[] SrcBytes, int size)
    {
        List<byte[]> Resultset = new List<byte[]>();

        int srcLenght = SrcBytes.Length;
        byte[] source = null;

        int i = 0;
        for (; srcLenght > (i + 1) * size; i++)
        {
            Debug.WriteLine(string.Format("Working on byte array {0}.", Resultset.Count + 1); 
            source = new byte[size];
            Array.Copy(SrcBytes, i * size, source, 0, size);
            Resultset.Add(source);
        }

        int sourceLeft = srcLenght - i * size;
        if (sourceLeft > 0)
        {
            source = new byte[sourceLeft];
            Array.Copy(SrcBytes, i * size, source, 0, sourceLeft);
            Resultset.Add(source);
        }
        return Resultset;
    }
公共静态IEnumerable SplitSourceBytes(字节[]SrcBytes,int size)
{
列表结果集=新列表();
int srcLenght=SrcBytes.Length;
字节[]源=空;
int i=0;
对于(;srcLenght>(i+1)*大小;i++)
{
WriteLine(string.Format(“处理字节数组{0}.”,Resultset.Count+1);
source=新字节[大小];
复制(SrcBytes,i*size,source,0,size);
结果集添加(源);
}
int sourceLeft=srcLenght-i*size;
如果(sourceLeft>0)
{
source=新字节[sourceLeft];
Copy(SrcBytes,i*size,source,0,sourceLeft);
结果集添加(源);
}
返回结果集;
}

如果您需要知道自己使用的是什么索引,请使用for循环,而不是foreach。通过字节数组的数量,您是指原始数组的第i个块吗?您可以使用字典来识别这些块。您可以提供for循环或字典的示例吗?将大字节数组拆分为较小的块会获得什么?的内存分配大数组已经完成,现在你正在分配更多的内存(对于小的块)。如果你想用小的块处理大的字节数组,你可以考虑返回一个枚举的<代码> ARARY段< /C> >,在那里你有一个偏移量,它开始在更大的数组上(从中你甚至可以推断出“索引”)。你能提供你如何使用这个方法的代码吗?编辑:答案中的固定索引编号