0

我有一个大字节数组,我将它分成 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;
        }
    }
4

3 回答 3

2

您不需要创建新的字节数组;您可以使用ArraySegment<byte>枚举来“拆分”您的大数组,而不会产生新字节数组分配的损失。

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;
    }
}

要使用它,代码将类似于以下代码:

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++;
        }
    }
}
于 2012-11-26T22:35:03.413 回答
0

这可能适用于您需要的东西

        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;
        }
于 2012-11-26T22:03:19.643 回答
0

您可以将数组放在提供计数功能的列表中。

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;
    }
于 2012-11-26T22:49:14.997 回答