Пример #1
0
        public async Task The_ReadAsync_Method_Throws_When_Length_Is_Negative()
        {
            var reader = new AsyncBinaryReader(new MemoryStream());

            await Assert.ThrowsAsync<ArgumentException>(async () =>
            {
                await reader.ReadAsync(-1);
            });
        }
Пример #2
0
        public async Task The_ReadAsync_Method_Works()
        {
            var expected = Encoding.UTF8.GetBytes("123456");
            var stream = new MemoryStream(expected);
            var reader = new AsyncBinaryReader(stream);

            var actual = await reader.ReadAsync(expected.Length);

            Assert.Equal(expected, actual);
        }
Пример #3
0
        /// <summary>
        /// Reads the <see cref="uint"/> using the specified <paramref name="reader"/>.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public async Task <uint> ReadValueAsync(AsyncBinaryReader reader)
        {
            Guard.NotNull(reader, nameof(reader));

            var data = await reader.ReadAsync(this.Length);

            Array.Reverse(data);
            Array.Resize(ref data, 4);

            return(BitConverter.ToUInt32(data, 0));
        }
Пример #4
0
        /// <summary>
        /// Reads the <see cref="string"/> using the specified <paramref name="reader"/>.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public async Task <string> ReadValueAsync(AsyncBinaryReader reader)
        {
            Guard.NotNull(reader, nameof(reader));

            var data = await reader.ReadAsync(this.Length);

            if (this.ZeroTerminated)
            {
                data = data
                       .TakeWhile(f => f != 0)
                       .ToArray();
            }

            return(this.Encoding.GetString(data));
        }
Пример #5
0
        /// <summary>
        /// Reads the <see cref="T:byte[]"/> using the specified <paramref name="reader"/>.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public Task <byte[]> ReadValueAsync(AsyncBinaryReader reader)
        {
            Guard.NotNull(reader, nameof(reader));

            return(reader.ReadAsync(this.Length));
        }