public static void ReadWriteBuildWithRange(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using var manager = writer.GetWrittenContent(0..255);
            Equal(bytes.AsMemory(0, 255).ToArray(), manager.Memory.ToArray());
        }
        public static void CompatWithReadOnlySequence(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, 450);
            writer.Write(bytes.AsSpan(450));
            Equal(bytes.Length, writer.Length);
            using var source = writer.GetWrittenContent(10);
            Equal(bytes, source.Sequence.ToArray());
        }
        public static void DrainToSpan(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            var buffer = new byte[100];

            Equal(buffer.Length, writer.CopyTo(buffer));
            Equal(bytes[0..100], buffer);
        public static void DrainToStream(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using var ms = new MemoryStream(500);
            writer.CopyTo(ms);
            Equal(bytes, ms.ToArray());
        }
        public static void ReadWriteWithInitialCapacity(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, initialCapacity: 5, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using var manager = writer.GetWrittenContent();
            Equal(bytes, manager.Memory.ToArray());
            if (writer.TryGetWrittenContent(out var content))
            {
                Equal(bytes, content.ToArray());
            }
        }
        public static void StressTest3()
        {
            var buffer = new byte[1024 * 1024 * 10];    // 10 MB

            new Random().NextBytes(buffer);
            using var writer = new FileBufferingWriter(asyncIO: false);
            writer.Write(buffer);
            False(writer.TryGetWrittenContent(out _));
            using var content = writer.GetWrittenContent();
            True(buffer.AsSpan().SequenceEqual(content.Memory.Span));
        }
 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 void BufferingWriter()
        {
            using var writer = new FileBufferingWriter(memoryThreshold: MemoryThreshold, asyncIO: false);
            foreach (var chunk in GetChunks())
            {
                writer.Write(chunk.Span);
            }
            writer.Flush();

            using var ms = new MemoryStream(content.Length);
            writer.CopyTo(ms);
        }
        public static void ReuseAfterCleanup(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

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

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using (var manager = writer.GetWrittenContent())
                Equal(bytes, manager.Memory.ToArray());

            writer.Clear();
            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using (var manager = writer.GetWrittenContent())
                Equal(bytes, manager.Memory.ToArray());
        }
Example #10
0
        public static void PermanentFile()
        {
            var    expected = RandomBytes(500);
            string fileName;

            using (var writer = new FileBufferingWriter(new FileBufferingWriter.Options {
                MemoryThreshold = 100, AsyncIO = false, FileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
            }))
            {
                writer.Write(expected);
                False(writer.TryGetWrittenContent(out _, out fileName));
            }

            using var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            var actual = new byte[expected.Length];

            fs.ReadBlock(actual);
            Equal(expected, actual);
        }