/// <summary> /// Reads characters from the byte buffer into the character buffer, consuming data from the byte buffer. /// </summary> /// <param name="sbuffer">The byte buffer to read from.</param> /// <param name="cbuffer">The character buffer to write to.</param> /// <param name="offset">The offset to write to in <paramref name="cbuffer"/>.</param> /// <param name="count">The number of characters to read into <paramref name="cbuffer"/>.</param> /// <returns>The number of bytes read into <paramref name="cbuffer"/>.</returns> /// <remarks> /// Data is read from the read byte buffer kept in <paramref name="sbuffer"/>, converted using the decoder into /// <paramref name="cbuffer"/>. As data is read from sbuffer, it is consumed. /// </remarks> public int Read(SerialBuffer sbuffer, char[] cbuffer, int offset, int count) { int chars = 0; if (IsOverflowed) { Reset(true); } if (IsCached) { chars = m_ReadCache.CopyTo(cbuffer, offset, count); SerialTrace.TraceRT.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "Read: Got {0} chars, need {1} count - From Cache", chars, count); ReadToConsume(sbuffer, chars); if (chars == count) { return(chars); } } chars += sbuffer.Stream.Read(cbuffer, offset + chars, count - chars, Decoder); SerialTrace.TraceRT.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "Read: Got {0} chars, need {1} count", chars, count); return(chars); }
public static bool DeEncryption(CircularBuffer <byte> data, NetPackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } byte[] commandBytes = new byte[2]; data.CopyTo(4, commandBytes, 0, 2); mPackage.command = BitConverter.ToUInt16(commandBytes, 0); byte[] bodyLengthBytes = new byte[2]; data.CopyTo(6, bodyLengthBytes, 0, 2); UInt16 nBodyLength1 = BitConverter.ToUInt16(bodyLengthBytes, 0); if (nBodyLength1 < 0 || nBodyLength1 + 8 > data.Length) { return(false); } mPackage.buffer = new byte[nBodyLength1]; data.CopyTo(8, mPackage.buffer, 0, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); return(true); }
public static void TestingICollectionImplementation() { var circularBuffer = new CircularBuffer <byte>(3, false); circularBuffer.Add(3); circularBuffer.Add(34); circularBuffer.Add(24); //Testing contains Assert.True(circularBuffer.Contains(3)); //Testing CopyTo var array = new byte[3]; circularBuffer.CopyTo(array, 0); Assert.Equal(3, array[0]); Assert.Equal(34, array[1]); Assert.Equal(24, array[2]); //Testing Count Assert.Equal(3, circularBuffer.Count); //Testing clear circularBuffer.Clear(); Assert.Equal(0, circularBuffer.Pop()); Assert.Equal(0, circularBuffer.Pop()); Assert.Equal(0, circularBuffer.Pop()); Assert.Empty(circularBuffer); }
public void CopyToTest() { // arrange CircularBuffer <string> target; int expectedHead; string[] expected; string[] actual; target = new CircularBuffer <string>(10); expected = new[] { "Alpha", "Beta", "Gamma" }; expectedHead = 0; actual = new string[3]; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(actual); // assert CollectionAssert.AreEqual(expected, actual); Assert.IsTrue(target.Contains("Alpha")); Assert.IsTrue(target.Contains("Beta")); Assert.IsTrue(target.Contains("Gamma")); Assert.AreEqual(expectedHead, target.Head); }
/// <summary> /// 将服务器发的的bytes进行解析 /// </summary> private void AnalyticalNetworkMsg() { while (ringBuf.Size >= sizeof(uint)) { byte[] lengthByte = new byte[sizeof(uint)]; ringBuf.CopyTo(0, lengthByte, 0, lengthByte.Length); uint msgLength = BitConverter.ToUInt32(lengthByte, 0); msgLength = NetworkMsg.NetworkToHostOrder(msgLength); if (ringBuf.Size >= msgLength) { byte[] msgdata = new byte[msgLength]; ringBuf.Get(msgdata); if (msgLength == 0) { ringBuf.Clear(); return; } using (MemoryStream stream = new MemoryStream(msgdata)) { BinaryReader br = new BinaryReader(stream); NetworkMsg Nmsg = new NetworkMsg(); try { Nmsg.length = NetworkMsg.NetworkToHostOrder(br.ReadInt32()); Nmsg.msgId = NetworkMsg.NetworkToHostOrder(br.ReadInt32()); Nmsg.sequence = (short)NetworkMsg.NetworkToHostOrder(br.ReadInt16()); } catch (Exception) { throw; } // 10 is header length length:4 msgId:4 sequence:2 byte[] data = br.ReadBytes((int)msgLength - 10); Nmsg.data = data; lock (objThread) { mMsgEvents.Enqueue(Nmsg); } } } else { break; } } }
public void CopyToArrayWithStartingIndexOffsetAndCountTest() { // arrange CircularBuffer <string> target; int expectedHead; string[] expected; string[] actual; int offset; int count; int index; target = new CircularBuffer <string>(10); expected = new[] { "Zeta", "Beta", "Gamma", "Eta" }; actual = new[] { "Zeta", null, null, "Eta" }; expectedHead = 0; index = 1; offset = 1; count = 2; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(index, actual, offset, count); // assert actual.Should(). Equal(expected); target.Contains("Alpha"). Should(). BeTrue(); target.Contains("Beta"). Should(). BeTrue(); target.Contains("Gamma"). Should(). BeTrue(); target.Head.Should(). Be(expectedHead); }
public void NewNumbersShouldOverrideOld() { var buffer = new CircularBuffer <int>(BUFFER_SIZE); Add6Numbers(buffer); var underlyingArray = new int[BUFFER_SIZE]; buffer.CopyTo(underlyingArray, 0); Assert.Equal(new [] { 4, 5, 6 }, underlyingArray); }
public void CircularBuffer_CopyTo() { var data = new[] { 12, 4, 9, 43, 0 }; var buffer = new CircularBuffer<int>(10); buffer.EnqueueRange(data, 0, data.Length); var dataCopy = new int[5]; buffer.CopyTo(dataCopy, 0, 5); CollectionAssert.AreEqual(new[] { 12, 4, 9, 43, 0 }, dataCopy); }
public void ShouldSaveWithOverflowArray() { var buffer = new CircularBuffer<int>(4); for(var i = 0; i < 6; i++) { buffer.Add(i); } var result = new [] { 3, 4, 5 }; var copy = new int[3]; buffer.CopyTo(copy, 0); Assert.AreEqual(result, copy); }
public void CopyToArrayWithOffsetTest() { // arrange CircularBuffer <string> target; int expectedHead; string[] expected; string[] actual; int offset; target = new CircularBuffer <string>(10); expected = new[] { "Zeta", "Alpha", "Beta", "Gamma" }; actual = new[] { "Zeta", null, null, null }; expectedHead = 0; offset = 1; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(actual, offset); // assert actual.Should(). Equal(expected); target.Contains("Alpha"). Should(). BeTrue(); target.Contains("Beta"). Should(). BeTrue(); target.Contains("Gamma"). Should(). BeTrue(); target.Head.Should(). Be(expectedHead); }
public void ShouldSaveWithoutOverflowArray() { var buffer = new CircularBuffer<int>(5); var array = new [] { 1, 2, 3, -1, 0 }; for(var i = 0; i < 3; i++) { buffer.Add(array[i]); } var copy = new int[5]; copy[3] = -1; buffer.CopyTo(copy, 0); CollectionAssert.AreEqual(array, copy); }
public void ShouldSaveWithOverflowArray() { var buffer = new CircularBuffer <int>(4); for (var i = 0; i < 6; i++) { buffer.Add(i); } var result = new [] { 3, 4, 5 }; var copy = new int[3]; buffer.CopyTo(copy, 0); Assert.AreEqual(result, copy); }
public void ShouldSaveWithWrapArray() { var buffer = new CircularBuffer <int>(4); for (var i = 0; i < 6; i++) { buffer.Enqueue(i); } var result = new [] { 2, 3, 4, 5 }; var copy = new int[4]; buffer.CopyTo(copy, 0); Assert.AreEqual(result, copy); }
public void CopyTo_WithinCapacity_OnlyCopiesAddedItems() { var buffer = new CircularBuffer <int>(3); buffer.Add(1); buffer.Add(2); var result = new int[2]; buffer.CopyTo(result, 0); Assert.AreEqual(1, result[0]); Assert.AreEqual(2, result[1]); }
public void ShouldSaveWithoutOverflowArray() { var buffer = new CircularBuffer <int>(5); var array = new [] { 1, 2, 3, -1, 0 }; for (var i = 0; i < 3; i++) { buffer.Add(array[i]); } var copy = new int[5]; copy[3] = -1; buffer.CopyTo(copy, 0); CollectionAssert.AreEqual(array, copy); }
public void CircularBufferTestCopyToArray() { var buffer = new CircularBuffer <long>(3); for (int i = 0; i < 5; i++) { buffer.PushFront(i); } var testArray = buffer.ToArray(); Assert.AreEqual(2, testArray[0]); Assert.AreEqual(3, testArray[1]); Assert.AreEqual(4, testArray[2]); testArray = new long[3]; buffer.CopyTo(testArray, 0); Assert.AreEqual(2, testArray[0]); Assert.AreEqual(3, testArray[1]); Assert.AreEqual(4, testArray[2]); testArray = new long[5]; buffer.CopyTo(testArray, 2); Assert.AreEqual(2, testArray[2]); }
public static bool DeEncryption(CircularBuffer <byte> data, NetReceivePackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } byte[] commandBytes = new byte[2]; data.CopyTo(4, commandBytes, 0, 2); mPackage.nUniqueId = BitConverter.ToUInt16(commandBytes, 0); byte[] bodyLengthBytes = new byte[2]; data.CopyTo(6, bodyLengthBytes, 0, 2); UInt16 nBodyLength1 = BitConverter.ToUInt16(bodyLengthBytes, 0); if (nBodyLength1 < 0 || nBodyLength1 + 8 > data.Length) { return(false); } data.CopyTo(8, mPackage.buffer.Array, mPackage.buffer.Offset, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); ArraySegment <byte> mChe = new ArraySegment <byte> (mPackage.buffer.Array, mPackage.buffer.Offset, nBodyLength1); mPackage.buffer = mChe; return(true); }
public void CopyToArrayWithOffsetAndCountTest() { // arrange CircularBuffer <string> target; int expectedHead; string[] expected; string[] actual; int offset; int count; int index; target = new CircularBuffer <string>(10); expected = new[] { "Zeta", "Alpha", "Beta", "Eta" }; actual = new[] { "Zeta", null, null, "Eta" }; expectedHead = 0; index = 0; offset = 1; count = 2; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(index, actual, offset, count); // assert CollectionAssert.AreEqual(expected, actual); Assert.IsTrue(target.Contains("Alpha")); Assert.IsTrue(target.Contains("Beta")); Assert.IsTrue(target.Contains("Gamma")); Assert.AreEqual(expectedHead, target.Head); }
public static bool DeEncryption(CircularBuffer <byte> data, NetPackage mPackage) { if (data.Length <= 8) { return(false); } for (int i = 0; i < 4; i++) { if (data [i] != mCheck [i]) { return(false); } } int nBodyLength1 = data [4] | data [5] << 8 | data [6] << 16 | data [7] << 24; if (nBodyLength1 <= 0 || nBodyLength1 + 8 > data.Length) { return(false); } if (nBodyLength1 > mReceiveBuffer.Length) { mReceiveBuffer = new byte[nBodyLength1]; } data.CopyTo(8, mReceiveBuffer, 0, nBodyLength1); data.ClearBuffer(nBodyLength1 + 8); byte[] msg = mAES.Decryption(mReceiveBuffer, 0, nBodyLength1); if (msg == null) { DebugSystem.LogBitStream("解包失败: ", mReceiveBuffer); return(false); } int command = msg [0] | msg [1] << 8 | msg [2] << 16 | msg [3] << 24; int nBodyLength2 = msg.Length - 4; byte[] buffer = new byte[nBodyLength2]; Array.Copy(msg, 4, buffer, 0, nBodyLength2); mPackage.command = command; mPackage.buffer = buffer; return(true); }
public void CopyTo_OverCapacity_RetainsOrder() { var buffer = new CircularBuffer <int>(3); buffer.Add(1); buffer.Add(2); buffer.Add(3); buffer.Add(4); var result = new int[3]; buffer.CopyTo(result, 0); Assert.AreEqual(2, result[0]); Assert.AreEqual(3, result[1]); Assert.AreEqual(4, result[2]); }
public void CopyToTest() { // arrange CircularBuffer <string> target; int expectedHead; string[] expected; string[] actual; target = new CircularBuffer <string>(10); expected = new[] { "Alpha", "Beta", "Gamma" }; expectedHead = 0; actual = new string[3]; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(actual); // assert actual.Should(). Equal(expected); target.Contains("Alpha"). Should(). BeTrue(); target.Contains("Beta"). Should(). BeTrue(); target.Contains("Gamma"). Should(). BeTrue(); target.Head.Should(). Be(expectedHead); }
public void PopWorksInReversedOrder() { var buffer = new CircularBuffer <int>(BUFFER_SIZE); Add6Numbers(buffer); var list = new List <int>(); for (var i = 0; i < BUFFER_SIZE; i++) { list.Add(buffer.Pop()); } var underlyingArray = new int[BUFFER_SIZE]; buffer.CopyTo(underlyingArray, 0); Array.Reverse(underlyingArray); Assert.Equal(list.ToArray(), underlyingArray); }
public void CopyToExceptionTest() { // arrange CircularBuffer <string> target; string[] actual; int offset; int count; int index; target = new CircularBuffer <string>(10); actual = new string[target.Capacity]; index = 0; offset = 0; count = 4; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act & assert Assert.That(() => target.CopyTo(index, actual, offset, count), Throws.TypeOf <ArgumentOutOfRangeException>()); }
public void CopyToExceptionTest() { // arrange CircularBuffer <string> target; string[] actual; int offset; int count; int index; target = new CircularBuffer <string>(10); actual = new string[target.Capacity]; index = 0; offset = 0; count = 4; target.Put("Alpha"); target.Put("Beta"); target.Put("Gamma"); // act target.CopyTo(index, actual, offset, count); }
public void TestCopyTo() { var buffer = new CircularBuffer <int>(5); Assert.That(() => buffer.CopyTo(null, 0), Throws.ArgumentNullException); Assert.That(() => buffer[0], Throws.TypeOf <IndexOutOfRangeException>()); var array = new int[] { 1, 2, 3, 4, 5 }; buffer.AddMany(array, 1, array.Length); Assert.AreEqual(buffer[0], 2); Assert.AreEqual(buffer[1], 3); Assert.AreEqual(buffer[2], 4); Assert.AreEqual(buffer[3], 5); Assert.That(() => buffer[4], Throws.TypeOf <IndexOutOfRangeException>()); int[] array2 = new int[buffer.Count - 1]; Assert.That(() => buffer.CopyTo(array2, -1), Throws.TypeOf <ArgumentOutOfRangeException>()); Assert.That(() => buffer.CopyTo(array2, 0), Throws.ArgumentException); array2 = new int[buffer.Count]; buffer.CopyTo(array2, 0); Assert.IsTrue(array2.EqualTo(new int[] { 2, 3, 4, 5 })); buffer.Clear(); buffer.CopyTo(array2, 0); buffer.AddMany(array, 0, array.Length); buffer.Remove(); buffer.Remove(); buffer.Remove(); buffer.Add(6); buffer.Add(7); buffer.CopyTo(array2, 0); Assert.IsTrue(array2.EqualTo(new int[] { 4, 5, 6, 7 })); }
public void CopyTo_WithinCapacity_OnlyCopiesAddedItems() { var buffer = new CircularBuffer<int>(3); buffer.Add(1); buffer.Add(2); var result = new int[2]; buffer.CopyTo(result, 0); Assert.AreEqual(1, result[0]); Assert.AreEqual(2, result[1]); }
void ICollection <object> .CopyTo(object[] array, int arrayIndex) { _buffer.CopyTo(array, arrayIndex); }
public void CopyToWithIndex_OverCapacity_RetainsOrder() { var buffer = new CircularBuffer<int>(3); buffer.Add(1); buffer.Add(2); buffer.Add(3); buffer.Add(4); var result = new int[5]; buffer.CopyTo(result, 2); Assert.AreEqual(0, result[0]); Assert.AreEqual(0, result[1]); Assert.AreEqual(2, result[2]); Assert.AreEqual(3, result[3]); Assert.AreEqual(4, result[4]); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g; g = e.Graphics; g.Clear(Color.FromArgb(255, 255, 220)); g.SmoothingMode = SmoothingMode.AntiAlias; if (_snakeBody.Size > 1) { // don't do this! //g.DrawLines(_bodyPen, _snakeBody.ToArray()); if (!this.HasSplitResults()) { // this isn't great either really, but as Graphics.DrawLines // isn't enlightened enough to take a start and length, // we copy the buffer out of the CircularBuffer into an // existing byte array so in theory we aren't allocating // an array over and over again. In this demo though, we // are if the sizes are different this.EnsureSize(ref _buffer, _snakeBody.Size); _snakeBody.CopyTo(_buffer); g.DrawLines(_bodyPen, _buffer); } else { int start; Point previous; Point current; // if we've wrapped the playing field, I can't just // call DrawLines with the entire buffer as we'll get // lines drawn across the entire playing field, so // instead I need to break it down into smaller buffers. // In this scenario I'd be better off with a pool, but // I haven't implemented one for this simple demo start = 0; previous = _snakeBody.PeekAt(0); for (int i = 1; i < _snakeBody.Size; i++) { current = _snakeBody.PeekAt(i); if (this.GetDistance(previous, current) > _gridSize) { // here we have a split, so let us grab a subset of // the buffer and draw our lines this.DrawSection(g, start, i - start); start = i; } previous = current; } if (start < _snakeBody.Size) { this.DrawSection(g, start, _snakeBody.Size - start); } } } g.DrawEllipse(_headPen, new Rectangle(_snakeHead.X - _headSize, _snakeHead.Y - _headSize, _gridSize, _gridSize)); }