public static async Task ReadWriteApm2(int threshold, bool asyncIO) { using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: asyncIO); var bytes = new byte[500]; for (byte i = 0; i < byte.MaxValue; i++) { bytes[i] = i; } var checker = new CallbackChecker(); var ar = writer.BeginWrite(bytes, 0, byte.MaxValue, checker.DoCallback, "state1"); Equal("state1", ar.AsyncState); True(await checker.Task); writer.EndWrite(ar); checker = new CallbackChecker(); ar = writer.BeginWrite(bytes, byte.MaxValue, bytes.Length - byte.MaxValue, checker.DoCallback, "state2"); Equal("state2", ar.AsyncState); True(await checker.Task); writer.EndWrite(ar); Equal(bytes.Length, writer.Length); using var manager = await writer.GetWrittenContentAsync(); Equal(bytes, manager.Memory.ToArray()); }
public static async Task ReuseAfterBuild(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()) Equal(bytes, manager.Memory.ToArray()); await writer.WriteAsync(new byte[] { 3, 4, 5 }.AsMemory()); writer.WriteByte(6); using (var manager = await writer.GetWrittenContentAsync(500..)) { Equal(new byte[] { 3, 4, 5, 6 }, manager.Memory.ToArray()); } }
public static void ReadWriteApm(int threshold, bool asyncIO) { using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: asyncIO); var bytes = new byte[500]; for (byte i = 0; i < byte.MaxValue; i++) { bytes[i] = i; } var ar = writer.BeginWrite(bytes, 0, byte.MaxValue, null, "state1"); Equal("state1", ar.AsyncState); True(ar.AsyncWaitHandle.WaitOne(DefaultTimeout)); writer.EndWrite(ar); ar = writer.BeginWrite(bytes, byte.MaxValue, bytes.Length - byte.MaxValue, null, "state2"); Equal("state2", ar.AsyncState); True(ar.AsyncWaitHandle.WaitOne(DefaultTimeout)); writer.EndWrite(ar); Equal(bytes.Length, writer.Length); using var manager = writer.GetWrittenContent(); Equal(bytes, manager.Memory.ToArray()); }
internal ReadOnlySequenceSource(FileBufferingWriter writer, int segmentLength) { var buffer = writer.buffer; tail = buffer.Memory.Slice(0, writer.position); accessor = writer.fileBackend is null ? null : new ReadOnlySequenceAccessor(writer.fileBackend, segmentLength); session = writer.EnterReadMode(this); }
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)); }
internal MemoryMappedFileManager(FileBufferingWriter writer, long offset, long length) { Debug.Assert(length <= int.MaxValue); Debug.Assert(writer.fileBackend != null); mappedFile = CreateMemoryMappedFile(writer.fileBackend); accessor = mappedFile.CreateViewAccessor(offset, length, MemoryMappedFileAccess.ReadWrite); accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); Debug.Assert(ptr != default); session = writer.EnterReadMode(this); Debug.Assert(writer.IsReading); }
internal ReadOnlySequenceSource(FileBufferingWriter writer, int segmentLength) { var buffer = writer.buffer; tail = buffer.Memory.Slice(0, writer.position); accessor = writer.fileBackend is null ? null : new ReadOnlySequenceAccessor(writer.fileBackend, segmentLength); this.writer = writer; version = ++writer.readVersion; }
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); }
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 NotEnoughMemory() { using var writer = new FileBufferingWriter(memoryThreshold: 10, asyncIO: false); var bytes = new byte[500]; for (byte i = 0; i < byte.MaxValue; i++) { bytes[i] = i; } IBufferWriter <byte> buffer = writer; Throws <InsufficientMemoryException>(() => buffer.Write(bytes)); }
public static void StressTest(int threshold) { var dict = new Dictionary <string, string> { { "Key1", "Value1" }, { "Key2", "Value2" } }; var formatter = new BinaryFormatter(); using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false); formatter.Serialize(writer, dict); using var manager = writer.GetWrittenContent(); using var source = StreamSource.AsStream(manager.Memory); Equal(dict, formatter.Deserialize(source)); }
public static void StressTest2(int threshold) { var dict = new Dictionary <string, string> { { "Key1", "Value1" }, { "Key2", "Value2" } }; var formatter = new BinaryFormatter(); using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false); formatter.Serialize(writer, dict); using var source = new MemoryStream(1024); writer.CopyTo(source); source.Position = 0L; Equal(dict, formatter.Deserialize(source)); }
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 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 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 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 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 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); }
public static async Task ReadWriteAsync(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(); Equal(bytes, manager.Memory.ToArray()); }
public static async Task UnsupportedMethods() { await using var writer = new FileBufferingWriter(); True(writer.CanWrite); False(writer.CanRead); False(writer.CanSeek); False(writer.CanTimeout); Throws <NotSupportedException>(() => writer.ReadByte()); Throws <NotSupportedException>(() => writer.Position); Throws <NotSupportedException>(() => writer.Position = 42L); Throws <NotSupportedException>(() => writer.SetLength(42L)); Throws <NotSupportedException>(() => writer.Seek(0L, default)); Throws <NotSupportedException>(() => writer.Read(new Span <byte>())); Throws <NotSupportedException>(() => writer.Read(new byte[10], 0, 10)); Throws <NotSupportedException>(() => writer.BeginRead(new byte[10], 0, 10, null, null)); Throws <InvalidOperationException>(() => writer.EndRead(Task.CompletedTask)); await ThrowsAsync <NotSupportedException>(() => writer.ReadAsync(new byte[10], 0, 10)); await ThrowsAsync <NotSupportedException>(writer.ReadAsync(new byte[10], CancellationToken.None).AsTask); }
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 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()); }
public static void BufferedReadWrite(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; } IBufferWriter <byte> buffer = writer; buffer.Write(new ReadOnlySpan <byte>(bytes, 0, byte.MaxValue)); buffer.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 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()); }
internal ReaderStream(FileBufferingWriter writer) { writer.GetWrittenContentAsStream(out source); session = writer.EnterReadMode(this); }
public static void EmptyContent() { using var writer = new FileBufferingWriter(); True(writer.TryGetWrittenContent(out var content)); True(content.IsEmpty); }
internal ReaderStream(FileBufferingWriter writer, bool useAsyncIO) { writer.GetWrittenContentAsStream(out source, useAsyncIO); session = writer.EnterReadMode(this); }
internal MemoryManager(FileBufferingWriter writer, in Range range)