예제 #1
0
        public static async Task ReadWriteBuildWithRangeAsync(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: true);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            await writer.WriteAsync(bytes, 0, byte.MaxValue);

            await writer.WriteAsync(bytes.AsMemory(byte.MaxValue));

            Equal(bytes.Length, writer.Length);
            using var manager = await writer.GetWrittenContentAsync(0..255);

            Equal(bytes.AsMemory(0, 255).ToArray(), manager.Memory.ToArray());
        }
        public static async Task CompatWithReadOnlySequenceAsync(int threshold)
        {
            await using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: true);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            await writer.WriteAsync(bytes, 0, 450);

            await writer.WriteAsync(bytes.AsMemory(450));

            Equal(bytes.Length, writer.Length);
            using var source = await writer.GetWrittenContentAsync(10);

            Equal(bytes, source.Sequence.ToArray());
        }
        public static async Task DrainToBufferAsync(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: true);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            await writer.WriteAsync(bytes, 0, byte.MaxValue);

            await writer.WriteAsync(bytes.AsMemory(byte.MaxValue));

            Equal(bytes.Length, writer.Length);
            var ms = new ArrayBufferWriter <byte>();
            await writer.CopyToAsync(ms);

            Equal(bytes, ms.WrittenSpan.ToArray());
        }
예제 #4
0
 public static async Task WriteDuringReadAsync()
 {
     using var writer = new FileBufferingWriter();
     writer.Write(new byte[] { 1, 2, 3 });
     using var manager = writer.GetWrittenContent();
     Equal(new byte[] { 1, 2, 3 }, manager.Memory.ToArray());
     Throws <InvalidOperationException>(writer.Clear);
     Throws <InvalidOperationException>(() => writer.WriteByte(2));
     Throws <InvalidOperationException>(() => writer.GetWrittenContent());
     await ThrowsAsync <InvalidOperationException>(() => writer.WriteAsync(new byte[2], 0, 2));
     await ThrowsAsync <InvalidOperationException>(writer.GetWrittenContentAsync().AsTask);
 }
        public async Task BufferingWriterAsync()
        {
            using var writer = new FileBufferingWriter(memoryThreshold: MemoryThreshold, asyncIO: true);
            foreach (var chunk in GetChunks())
            {
                await writer.WriteAsync(chunk);
            }
            await writer.FlushAsync();

            using var ms = new MemoryStream(content.Length);
            await writer.CopyToAsync(ms);
        }