Beispiel #1
0
        public static async Task Test_Buffered_Peeking_Can_Peek_Counted_Bytes_With_ReadAllAsync()
        {
            //arrange
            DefaultStreamReaderStrategyAsync reader = new DefaultStreamReaderStrategyAsync(new byte[] { 5, 6, 7, 5 });

            //act
            IWireStreamReaderStrategyAsync peekedBufferReader = reader.PeekWithBufferingAsync();

            byte[] peekedBytes = await peekedBufferReader.PeekBytesAsync(4);

            byte[] peekedBytesAgain = await peekedBufferReader.PeekBytesAsync(2);

            byte b = peekedBufferReader.ReadByte();

            byte[] readBytes = await peekedBufferReader.ReadAllBytesAsync();

            //assert
            Assert.AreEqual(3, readBytes.Length);
            for (int i = 0; i < readBytes.Length; i++)
            {
                Assert.AreEqual(peekedBytes[i + 1], readBytes[i]);
            }

            for (int i = 0; i < peekedBytesAgain.Length; i++)
            {
                Assert.AreEqual(peekedBytes[i], peekedBytesAgain[i]);
            }

            Assert.Throws <InvalidOperationException>(() => peekedBufferReader.ReadByte());
        }
Beispiel #2
0
        public static async Task Test_Buffered_Peeking_Can_Peek_Bytes_Async()
        {
            //arrange
            DefaultStreamReaderStrategyAsync reader = new DefaultStreamReaderStrategyAsync(new byte[] { 5, 6, 7, 5 });

            //act
            IWireStreamReaderStrategyAsync peekedBufferReader = reader.PeekWithBufferingAsync();

            //assert
            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(5, await peekedBufferReader.PeekByteAsync());
            }

            Assert.AreEqual(5, await peekedBufferReader.ReadByteAsync());
            Assert.AreEqual(6, await peekedBufferReader.ReadByteAsync());

            byte[] readBytes = await peekedBufferReader.ReadAllBytesAsync();

            Assert.AreEqual(7, readBytes[0]);
            Assert.AreEqual(5, readBytes[1]);

            Assert.Throws <AggregateException>(() =>
            {
                byte b = peekedBufferReader.ReadByteAsync().Result;
            });
        }
Beispiel #3
0
        public static async Task Test_That_Prepended_Reader_Contains_Bytes_All_At_Once_Async(byte[] bytes)
        {
            //arrange
            DefaultStreamReaderStrategyAsync reader = new DefaultStreamReaderStrategyAsync(new byte[0]);

            //act
            IWireStreamReaderStrategyAsync bufferedReader = reader.PreprendWithBytesAsync(bytes);

            byte[] readBytes = await bufferedReader.ReadAllBytesAsync();

            //assert
            for (int i = 0; i < bytes.Length; i++)
            {
                Assert.AreEqual(readBytes[i], bytes[i]);
            }
        }
Beispiel #4
0
        /// <inheritdoc />
        public override async Task <TObjectType[]> ReadAsync(IWireStreamReaderStrategyAsync source)
        {
            byte[] bytes = await source.ReadAllBytesAsync()
                           .ConfigureAwait(false);

            FixedBufferWireReaderStrategy fixedStrategy = new FixedBufferWireReaderStrategy(bytes, 0, bytes.Length);

            QuickList <TObjectType> objects = new QuickList <TObjectType>(4);

            //Read until the fixed buffer is empty. This iwll give us all objects without read exceptions when end is reached.
            while (!fixedStrategy.isFinished)
            {
                objects.Add(ElementSerializer.Read(fixedStrategy));
            }

            //We can avoid some copies like this
            return(objects.Count == objects._items.Length ? objects._items : objects.ToArray());
        }
Beispiel #5
0
 public override Task <byte[]> ReadAsync(IWireStreamReaderStrategyAsync source)
 {
     return(source.ReadAllBytesAsync());
 }