Provides a thin wrapper for a buffer that is emptied by calling the provided callback.

The fields in this class and its base are not encapsulated properly for performance reasons. See ReadBuffer for more information.

A frequent use case for this class is when a not previously known number of bytes need to be written to a stream one by one. In this case the following code tends to be much faster than calling Stream.WriteByte for each byte: void WriteToStream(Stream stream) { var writeBuffer = new WriteBuffer(stream.Write, 1024); bool done = false; while (!done && ((writeBuffer.Count < writeBuffer.Capacity) || writeBuffer.Flush())) { // .. byte theByte = 0x00; // Calculate the byte to append to the buffer writeBuffer[writeBuffer.Count++] = theByte; // Set done to true as soon as we're done... } }

Inheritance: Buffer
Ejemplo n.º 1
0
        private int WriteByte(byte[] buffer, int offset, WriteBuffer writeBuffer)
        {
            var currentByte = buffer[offset];

            if (this.previousWasEscapeByte)
            {
                this.previousWasEscapeByte = false;
                ++offset;
            }
            else
            {
                if (currentByte == Command.InterpretAsCommand)
                {
                    this.previousWasEscapeByte = true;
                }
                else
                {
                    ++offset;
                }
            }

            writeBuffer[writeBuffer.Count++] = currentByte;
            return(offset);
        }
Ejemplo n.º 2
0
 internal MyBufferStream(ReadBuffer readBuffer, WriteBuffer writeBuffer)
     : base(readBuffer, writeBuffer)
 {
 }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Initializes a new instance of the <see cref="BufferStream"/> class.</summary>
        /// <remarks>Pass <c>null</c> for one of the buffers to create a stream that only supports reading or writing.</remarks>
        protected BufferStream(ReadBuffer readBuffer, WriteBuffer writeBuffer)
        {
            this.ReadBuffer  = readBuffer;
            this.WriteBuffer = writeBuffer;
        }
Ejemplo n.º 4
0
        public void WriteAsyncTest()
        {
            AsyncPump.Run(
                async () =>
                {
                    var bytes = new byte[2];
                    this.Random.NextBytes(bytes);

                    // This covers the case where the written bytes are copied into the buffer in two chunks
                    using (var stream = new MemoryStream())
                    {
                        var writeBuffer = new WriteBuffer(stream.WriteAsync, 1);
                        await writeBuffer.WriteAsync(bytes, 0, bytes.Length, CancellationToken.None);
                        await writeBuffer.FlushAsync(CancellationToken.None);
                        CollectionAssert.AreEqual(bytes, stream.ToArray());
                    }
                });
        }
Ejemplo n.º 5
0
        public void ExceptionTest()
        {
            AsyncPump.Run(
                async () =>
                {
                    AssertThrow<ArgumentNullException>(
                        () => new ReadBuffer((ReadCallback)null, 1).Ignore(),
                        () => new ReadBuffer((ReadAsyncCallback)null, 1).Ignore(),
                        () => new WriteBuffer((WriteCallback)null, 1).Ignore(),
                        () => new WriteBuffer((WriteAsyncCallback)null, 1).Ignore(),
                        () => new WriteBuffer((b, o, c) => { }, 1).WriteAsUtf8(null, 0));
                    AssertThrow<ArgumentOutOfRangeException>(() => new ReadBuffer((b, o, c) => 0, 0).Ignore());

                    using (var stream = new MemoryStream())
                    {
                        var readBuffer = new ReadBuffer(stream.Read, 1);
                        AssertThrow<EndOfStreamException>(
                            () => readBuffer.Fill(new byte[1], 0, 1),
                            () => readBuffer.Fill(new byte[2], 0, 2));
                        await AssertThrowAsync<InvalidOperationException>(
                            () => readBuffer.ReadAsync(CancellationToken.None),
                            () => readBuffer.ReadAsync(new byte[1], 0, 1, CancellationToken.None),
                            () => readBuffer.FillAsync(1, CancellationToken.None),
                            () => readBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None));

                        var asyncReadBuffer = new ReadBuffer((ReadAsyncCallback)stream.ReadAsync, 1);
                        await AssertThrowAsync<EndOfStreamException>(
                            () => asyncReadBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None),
                            () => asyncReadBuffer.FillAsync(new byte[2], 0, 2, CancellationToken.None));
                        AssertThrow<InvalidOperationException>(
                            () => asyncReadBuffer.Read(),
                            () => asyncReadBuffer.Read(new byte[1], 0, 1),
                            () => asyncReadBuffer.Fill(1),
                            () => asyncReadBuffer.Fill(new byte[1], 0, 1),
                            () => asyncReadBuffer.ReadUtf8(1));

                        var writeBuffer = new WriteBuffer(stream.Write, 1);
                        await AssertThrowAsync<InvalidOperationException>(
                            () => writeBuffer.FlushAsync(CancellationToken.None),
                            () => writeBuffer.ReserveAsync(2, CancellationToken.None),
                            () => writeBuffer.WriteAsync(new byte[3], 0, 3, CancellationToken.None));

                        var asyncWriteBuffer = new WriteBuffer(stream.WriteAsync, 1);
                        asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
                        AssertThrow<InvalidOperationException>(() => asyncWriteBuffer.Flush());
                        asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
                        AssertThrow<InvalidOperationException>(
                            () => asyncWriteBuffer.Reserve(2), () => asyncWriteBuffer.Write(new byte[3], 0, 3));
                        asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
                        var str = "Hello";
                        AssertThrow<InvalidOperationException>(
                            () => asyncWriteBuffer.WriteAsUtf8(str, Encoding.UTF8.GetByteCount(str)));
                    }
                });
        }
Ejemplo n.º 6
0
        public void WriteTest()
        {
            var originalBytes = new byte[2];
            this.Random.NextBytes(originalBytes);

            // This covers the case where the written bytes are copied into the buffer in two chunks
            using (var stream = new MemoryStream())
            {
                var writeBuffer = new WriteBuffer(stream.Write, 1);
                writeBuffer.Write(originalBytes, 0, originalBytes.Length);
                writeBuffer.Flush();
                CollectionAssert.AreEqual(originalBytes, stream.ToArray());
            }
        }
 internal MyBufferStream(ReadBuffer readBuffer, WriteBuffer writeBuffer)
     : base(readBuffer, writeBuffer)
 {
 }
Ejemplo n.º 8
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing && !this.IsDisposed)
         {
             this.Flush();
         }
     }
     catch
     {
     }
     finally
     {
         this.ReadBuffer = null;
         this.WriteBuffer = null;
         base.Dispose(disposing);
     }
 }
Ejemplo n.º 9
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Initializes a new instance of the <see cref="BufferStream"/> class.</summary>
        /// <remarks>Pass <c>null</c> for one of the buffers to create a stream that only supports reading or writing.</remarks>
        protected BufferStream(ReadBuffer readBuffer, WriteBuffer writeBuffer)
        {
            this.ReadBuffer = readBuffer;
            this.WriteBuffer = writeBuffer;
        }
Ejemplo n.º 10
0
        private int WriteByte(byte[] buffer, int offset, WriteBuffer writeBuffer)
        {
            var currentByte = buffer[offset];

            if (this.previousWasEscapeByte)
            {
                this.previousWasEscapeByte = false;
                ++offset;
            }
            else
            {
                if (currentByte == Command.InterpretAsCommand)
                {
                    this.previousWasEscapeByte = true;
                }
                else
                {
                    ++offset;
                }
            }

            writeBuffer[writeBuffer.Count++] = currentByte;
            return offset;
        }