1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
   | public class RingBufferManager {     public byte[] Buffer { get; set; }      public int DataCount { get; set; }      public int DataStart { get; set; }      public int DataEnd { get; set; }        public RingBufferManager(int bufferSize)     {         DataCount = 0; DataStart = 0; DataEnd = 0;         Buffer = new byte[bufferSize];     }       public byte this[int index]     {         get         {             if (index >= DataCount) throw new Exception("环形缓冲区异常,索引溢出");             if (DataStart + index < Buffer.Length)             {                 return Buffer[DataStart + index];             }             else              {                 return Buffer[(DataStart + index) - Buffer.Length];             }         }     }       public int GetDataCount()      {         return DataCount;     }       public int GetReserveCount()      {         return Buffer.Length - DataCount;     }       public void Clear()     {         DataCount = 0;     }       public void Clear(int count)      {         if (count >= DataCount)          {             DataCount = 0;             DataStart = 0;             DataEnd = 0;         }         else         {             if (DataStart + count >= Buffer.Length)             {                 DataStart = (DataStart + count) - Buffer.Length;             }             else              {                 DataStart += count;             }             DataCount -= count;         }     }       public void WriteBuffer(byte[] buffer, int offset, int count)     {         Int32 reserveCount = Buffer.Length - DataCount;         if (reserveCount >= count)                                   {             if (DataEnd + count < Buffer.Length)                         {                 Array.Copy(buffer, offset, Buffer, DataEnd, count);                 DataEnd += count;                 DataCount += count;             }             else                        {                 System.Diagnostics.Debug.WriteLine("缓存从新开始....");                 Int32 overflowIndexLength = (DataEnd + count) - Buffer.Length;                       Int32 endPushIndexLength = count - overflowIndexLength;                              Array.Copy(buffer, offset, Buffer, DataEnd, endPushIndexLength);                 DataEnd = 0;                 offset += endPushIndexLength;                 DataCount += endPushIndexLength;                 if (overflowIndexLength != 0)                 {                     Array.Copy(buffer, offset, Buffer, DataEnd, overflowIndexLength);                 }                 DataEnd += overflowIndexLength;                                                      DataCount += overflowIndexLength;                                                }         }         else          {                      }     }       public void ReadBuffer(byte[] targetBytes,Int32 offset, Int32 count)      {         if (count > DataCount) throw new Exception("环形缓冲区异常,读取长度大于数据长度");         Int32 tempDataStart = DataStart;         if (DataStart + count < Buffer.Length)         {             Array.Copy(Buffer, DataStart, targetBytes, offset, count);         }         else          {             Int32 overflowIndexLength = (DataStart + count) - Buffer.Length;                 Int32 endPushIndexLength = count - overflowIndexLength;                          Array.Copy(Buffer, DataStart, targetBytes, offset, endPushIndexLength);                          offset += endPushIndexLength;                          if (overflowIndexLength != 0)             {                 Array.Copy(Buffer, 0, targetBytes, offset, overflowIndexLength);             }         }     }         public void WriteBuffer(byte[] buffer)     {         WriteBuffer(buffer, 0, buffer.Length);     }   }
   |