Read() публичный Метод

Reads bytes into the buffer by calling the callback specified during construction exactly once.

After each call to this method, Count contains the number of bytes in the buffer and Index equals 0.

This function is usually called when Index equals Count, that is, when the client has processed all bytes in the buffer (the whole buffer is filled in this case). If it is called earlier (when Index < Count) then the remaining bytes at the end of the buffer are first copied to the start of the buffer and the now empty part of the buffer is filled by calling the callback.

The object was created by calling /// .
public Read ( ) : bool
Результат bool
Пример #1
0
        public void ReadTest()
        {
            var originalbytes = new byte[3];

            this.Random.NextBytes(originalbytes);

            // This covers the case where the read bytes are copied into the buffer in two chunks
            using (var originalStream = new MemoryStream(originalbytes))
            {
                var readBuffer = new ReadBuffer(originalStream.Read, 2);
                var buffer     = new byte[1];
                int read;

                using (var readStream = new MemoryStream())
                {
                    while ((read = readBuffer.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        readStream.Write(buffer, 0, read);
                    }

                    Assert.AreEqual(0, readBuffer.Read(new byte[readBuffer.Capacity + 1], 0, readBuffer.Capacity + 1));
                    CollectionAssert.AreEqual(originalbytes, readStream.ToArray());
                }
            }
        }
Пример #2
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)));
                }
            });
        }
Пример #3
0
        public void ReadTest()
        {
            var originalbytes = new byte[3];
            this.Random.NextBytes(originalbytes);

            // This covers the case where the read bytes are copied into the buffer in two chunks
            using (var originalStream = new MemoryStream(originalbytes))
            {
                var readBuffer = new ReadBuffer(originalStream.Read, 2);
                var buffer = new byte[1];
                int read;

                using (var readStream = new MemoryStream())
                {
                    while ((read = readBuffer.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        readStream.Write(buffer, 0, read);
                    }

                    Assert.AreEqual(0, readBuffer.Read(new byte[readBuffer.Capacity + 1], 0, readBuffer.Capacity + 1));
                    CollectionAssert.AreEqual(originalbytes, readStream.ToArray());
                }
            }
        }
Пример #4
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)));
                    }
                });
        }