Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions PowerKit.Tests/MemoryWriteStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit.Tests.Utils;
Expand Down Expand Up @@ -29,4 +30,46 @@ public void MemoryWriteStream_Test()
seekable.CanSeek.Should().BeTrue();
destination.ToArray().Should().Equal(data);
}

[Fact]
public void MemoryWriteStream_FlushTwice_Throws()
{
// Arrange
using var destination = new MemoryStream();
using var seekable = new MemoryWriteStream(destination);

seekable.Write([1, 2, 3]);
seekable.Flush();

// Act & Assert
seekable.Invoking(s => s.Flush()).Should().Throw<InvalidOperationException>();
}

[Fact]
public void MemoryWriteStream_WriteAfterFlush_Throws()
{
// Arrange
using var destination = new MemoryStream();
using var seekable = new MemoryWriteStream(destination);

seekable.Write([1, 2, 3]);
seekable.Flush();

// Act & Assert
seekable.Invoking(s => s.Write([4, 5, 6])).Should().Throw<InvalidOperationException>();
}

[Fact]
public void MemoryWriteStream_FlushThenDispose_DoesNotThrow()
{
// Arrange
using var destination = new MemoryStream();
var seekable = new MemoryWriteStream(destination);

seekable.Write([1, 2, 3]);
seekable.Flush();

// Act & Assert
seekable.Invoking(s => s.Dispose()).Should().NotThrow();
}
}
Comment thread
Tyrrrz marked this conversation as resolved.
42 changes: 37 additions & 5 deletions PowerKit/MemoryWriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ namespace PowerKit;

/// <summary>
/// A <see cref="Stream" /> wrapper that buffers all writes in memory and flushes them to the
/// underlying stream on <see cref="Flush" />.
/// underlying stream on <see cref="Flush()" />.
/// </summary>
/// <remarks>
/// Writes go to an in-memory buffer and do not touch the underlying stream until
/// <see cref="Flush" /> is called. This makes the wrapper always seekable and allows writes to
/// <see cref="Flush()" /> is called. This makes the wrapper always seekable and allows writes to
/// be reordered freely before the final flush.
/// </remarks>
public class MemoryWriteStream(Stream source) : Stream
{
private readonly MemoryStream _buffer = new();
private bool _isFlushed;

/// <inheritdoc />
public override bool CanRead => false;
Expand All @@ -35,12 +36,34 @@ public override long Position
set => _buffer.Position = value;
}

/// <inheritdoc />
public override void Flush()
private void Flush(bool throwIfAlreadyFlushed)
{
if (_isFlushed)
{
if (throwIfAlreadyFlushed)
{
throw new InvalidOperationException(
"This stream has already been flushed and cannot be flushed again."
);
}

return;
}

_buffer.Position = 0;
_buffer.CopyTo(source);
source.Flush();
_isFlushed = true;
}

/// <inheritdoc />
public override void Flush() => Flush(true);

/// <inheritdoc />
protected override void Dispose(bool disposing)
{
Flush(false);
base.Dispose(disposing);
}
Comment thread
Tyrrrz marked this conversation as resolved.

/// <inheritdoc />
Expand All @@ -54,6 +77,15 @@ public override int Read(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();

/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count) =>
public override void Write(byte[] buffer, int offset, int count)
{
if (_isFlushed)
{
throw new InvalidOperationException(
"This stream has already been flushed and cannot be flushed again."
);
}

_buffer.Write(buffer, offset, count);
}
}