示例#1
0
        /// <summary>
        /// Writes the specified <paramref name="value"/> using the specified <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="value">The value.</param>
        public Task WriteValueAsync(AsyncBinaryWriter writer, byte[] value)
        {
            Guard.NotNull(writer, nameof(writer));
            Guard.NotNull(value, nameof(value));

            Array.Resize(ref value, this.Length);

            return(writer.WriteAsync(value));
        }
示例#2
0
        public async Task The_WriteAsync_Method_Throws_When_Data_Is_Null()
        {
            var writer = new AsyncBinaryWriter(new MemoryStream());

            await Assert.ThrowsAsync<ArgumentNullException>(async () =>
            {
                await writer.WriteAsync(null);
            });
        }
示例#3
0
        public async Task The_WriteAsync_Method_Works()
        {
            var stream = new MemoryStream();
            var writer = new AsyncBinaryWriter(stream);
            var expected = Encoding.UTF8.GetBytes("123456");

            await writer.WriteAsync(expected);
            var actual = stream.ToArray();

            Assert.Equal(expected, actual);
        }
示例#4
0
        /// <summary>
        /// Writes the specified <paramref name="value"/> using the specified <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="value">The value.</param>
        public async Task WriteValueAsync(AsyncBinaryWriter writer, uint value)
        {
            Guard.NotNull(writer, nameof(writer));

            var data = BitConverter.GetBytes(value);

            Array.Resize(ref data, this.Length);
            Array.Reverse(data);

            await writer.WriteAsync(data);
        }
示例#5
0
        /// <summary>
        /// Writes the specified <paramref name="value"/> using the specified <paramref name="writer"/>.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="value">The value.</param>
        public Task WriteValueAsync(AsyncBinaryWriter writer, string value)
        {
            Guard.NotNull(writer, nameof(writer));
            Guard.NotNull(value, nameof(value));

            var data = this.Encoding.GetBytes(value);

            //Ensure data has right length
            Array.Resize(ref data, this.Length);

            if (this.ZeroTerminated)
            {
                //Ensure last byte is 0
                data[this.Length - 1] = 0;
            }

            return(writer.WriteAsync(data));
        }