2 // © Copyright Henrik Ravn 2004
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 using System.Diagnostics;
15 /// This class implements a circular buffer
17 internal class CircularBuffer
20 private int _capacity;
24 private byte[] _buffer;
27 public CircularBuffer(int capacity)
29 Debug.Assert( capacity > 0 );
30 _buffer = new byte[capacity];
37 public int Size { get { return _size; } }
39 public int Put(byte[] source, int offset, int count)
41 Debug.Assert( count > 0 );
42 int trueCount = Math.Min(count, _capacity - Size);
43 for (int i = 0; i < trueCount; ++i)
44 _buffer[(_tail+i) % _capacity] = source[offset+i];
51 public bool Put(byte b)
53 if (Size == _capacity) // no room
61 public int Get(byte[] destination, int offset, int count)
63 int trueCount = Math.Min(count,Size);
64 for (int i = 0; i < trueCount; ++i)
65 destination[offset + i] = _buffer[(_head+i) % _capacity];
77 int result = (int)_buffer[_head++ % _capacity];