2012-11-26 72 views
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; 
     } 
    } 
+1

如果您需要知道您的索引,請使用for循環,而不是foreach。 – cadrell0

+0

通過字節數組的數量,你是指原始數組的第i塊?您可以使用字典來識別塊。 – MGZero

+0

你能提供循環或字典的例子嗎? – updev

回答

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

編輯:修復索引在答案nubering –

2

你並不需要創建新的字節數組工作;您可以使用枚舉來「拆分」您的大數組,而不會受到新字節數組分配的影響。

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++; 
     } 
    } 
} 
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; 
    } 
相關問題