如何使用大字节数组?

时间:2012-11-26 21:45:01

标签: .net c#-4.0 bytearray ienumerable

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

3 个答案:

答案 0 :(得分: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++;
        }
    }
}

答案 1 :(得分: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;
        }

答案 2 :(得分:0)

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

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