A fixed size buffer of bytes. Both reading and writing are supported. Reading from an empty buffer will wait until data is written. Writing to a full buffer will wait until data is read.
Exemplo n.º 1
0
        public void Basic()
        {
            const int Size = 64;

            buffer_ = new ReadWriteRingBuffer(Size);

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsTrue(buffer_.IsEmpty);

            buffer_.WriteByte(1);

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsFalse(buffer_.IsEmpty);
            Assert.AreEqual(1, buffer_.Count);

            Assert.AreEqual(1, buffer_.ReadByte());

            Assert.IsFalse(buffer_.IsFull);
            Assert.IsTrue(buffer_.IsEmpty);

            for (int i = 0; i < buffer_.Length; ++i)
            {
                buffer_.WriteByte(unchecked ((byte)(i & 0xff)));
            }

            Assert.IsTrue(buffer_.IsFull);
            Assert.IsFalse(buffer_.IsEmpty);

            buffer_.Close();

            Assert.IsTrue(buffer_.IsClosed);

            bool caught = false;

            try
            {
                buffer_.WriteByte(1);
            }
            catch
            {
                caught = true;
            }

            Assert.IsTrue(caught);

            int count    = Size;
            int expected = 0;

            while (count != 0)
            {
                Assert.AreEqual(count, buffer_.Count);
                Assert.AreEqual(expected, buffer_.ReadByte());
                count--;
                expected = (expected + 1) & 0xff;
            }

            Assert.IsTrue(buffer_.IsEmpty);
            Assert.AreEqual(-1, buffer_.ReadByte());
        }
Exemplo n.º 2
0
        public void Threaded()
        {
            buffer_     = new ReadWriteRingBuffer(8);
            readTarget_ = writeTarget_ = 16384;

            Thread reader = new Thread(Reader);

            reader.Start();

            Thread writer = new Thread(Writer);

            writer.Start();

            writer.Join();
            reader.Join();
        }
Exemplo n.º 3
0
        public void Buffered()
        {
            const int Size = 64;

            buffer_ = new ReadWriteRingBuffer(Size);

            byte[] writeBuffer = new byte[16];
            for (int i = 0; i < 16; ++i)
            {
                writeBuffer[i] = (byte)i;
            }

            buffer_.Write(writeBuffer, 0, 3);
            Assert.AreEqual(3, buffer_.Count);

            byte[] readBuffer = new byte[16];
            Assert.AreEqual(3, buffer_.Read(readBuffer, 0, 3));
            for (int i = 0; i < 3; ++i)
            {
                Assert.AreEqual(i, readBuffer[i]);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowedStream"/> class.
 /// </summary>
 /// <param name="size">The size.</param>
 public WindowedStream(int size)
 {
     ringBuffer_ = new ReadWriteRingBuffer(size);
 }
Exemplo n.º 5
0
		/// <summary>
		/// Initializes a new instance of the <see cref="WindowedStream"/> class.
		/// </summary>
		/// <param name="size">The size.</param>
		public WindowedStream(int size)
		{
			ringBuffer_ = new ReadWriteRingBuffer(size);
		}
Exemplo n.º 6
0
		public void Threaded()
		{
			buffer_ = new ReadWriteRingBuffer(8);
			readTarget_ = writeTarget_ = 16384;

			Thread reader = new Thread(Reader);
			reader.Start();

			Thread writer = new Thread(Writer);
			writer.Start();

			writer.Join();
			reader.Join();
		}
Exemplo n.º 7
0
		public void Buffered()
		{

			const int Size = 64;

			buffer_ = new ReadWriteRingBuffer(Size);

			byte[] writeBuffer = new byte[16];
			for (int i = 0; i < 16; ++i)
			{
				writeBuffer[i] = (byte)i;
			}

			buffer_.Write(writeBuffer, 0, 3);
			Assert.AreEqual(3, buffer_.Count);

			byte[] readBuffer = new byte[16];
			Assert.AreEqual(3, buffer_.Read(readBuffer, 0, 3));
			for (int i = 0; i < 3; ++i)
			{
				Assert.AreEqual(i, readBuffer[i]);
			}

		}
Exemplo n.º 8
0
		public void Basic()
		{
			const int Size = 64;

			buffer_ = new ReadWriteRingBuffer(Size);

			Assert.IsFalse(buffer_.IsFull);
			Assert.IsTrue(buffer_.IsEmpty);

			buffer_.WriteByte(1);

			Assert.IsFalse(buffer_.IsFull);
			Assert.IsFalse(buffer_.IsEmpty);
			Assert.AreEqual(1, buffer_.Count);

			Assert.AreEqual(1, buffer_.ReadByte());

			Assert.IsFalse(buffer_.IsFull);
			Assert.IsTrue(buffer_.IsEmpty);

			for (int i = 0; i < buffer_.Length; ++i)
			{
				buffer_.WriteByte(unchecked((byte)(i & 0xff)));
			}

			Assert.IsTrue(buffer_.IsFull);
			Assert.IsFalse(buffer_.IsEmpty);

			buffer_.Close();

			Assert.IsTrue(buffer_.IsClosed);

			bool caught = false;
			try
			{
				buffer_.WriteByte(1);
			}
			catch
			{
				caught = true;
			}

			Assert.IsTrue(caught);

			int count = Size;
			int expected = 0;

			while (count != 0)
			{
				Assert.AreEqual(count, buffer_.Count);
				Assert.AreEqual(expected, buffer_.ReadByte());
				count--;
				expected = (expected + 1) & 0xff;
			}

			Assert.IsTrue(buffer_.IsEmpty);
			Assert.AreEqual(-1, buffer_.ReadByte());
		}
Exemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowedStream"/> class.
 /// </summary>
 /// <param name="size">The size.</param>
 public WindowedStream(int size, CancellationToken?token = null)
 {
     ringBuffer = new ReadWriteRingBuffer(size, token);
 }