/// <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)); }
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); }); }
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); }
/// <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); }
/// <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)); }