Files
sharpcompress/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs

81 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-12-31 14:43:15 +00:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.Test.Mocks;
2026-04-29 11:50:51 +01:00
public class AsyncOnlyStream(Stream stream, bool disposeStream = true) : Stream
2025-12-31 14:43:15 +00:00
{
2026-04-28 09:14:31 +01:00
private readonly Stream _stream = stream ?? throw new ArgumentNullException(nameof(stream));
2025-12-31 14:43:15 +00:00
2026-01-31 15:29:34 +00:00
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;
2025-12-31 14:43:15 +00:00
public override long Position
{
2026-01-31 15:29:34 +00:00
get => _stream.Position;
set => _stream.Position = value;
2025-12-31 14:43:15 +00:00
}
2026-04-29 11:45:45 +01:00
public override Task FlushAsync(CancellationToken cancellationToken) =>
_stream.FlushAsync(cancellationToken);
public override void Flush() =>
throw new NotSupportedException("Synchronous Flush is not supported");
public override int ReadByte() =>
throw new NotSupportedException("Synchronous ReadByte is not supported");
2025-12-31 14:43:15 +00:00
2026-01-13 14:29:10 +00:00
public override int Read(byte[] buffer, int offset, int count) =>
throw new NotSupportedException("Synchronous Read is not supported");
2025-12-31 14:43:15 +00:00
public override Task<int> ReadAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
2026-01-31 15:29:34 +00:00
) => _stream.ReadAsync(buffer, offset, count, cancellationToken);
2025-12-31 14:43:15 +00:00
2026-01-13 14:06:14 +00:00
#if NET8_0_OR_GREATER
public override ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default
2026-01-31 15:29:34 +00:00
) => _stream.ReadAsync(buffer, cancellationToken);
2025-12-31 14:43:15 +00:00
#endif
2026-01-31 15:29:34 +00:00
public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin);
2026-01-31 15:29:34 +00:00
public override void SetLength(long value) => _stream.SetLength(value);
2026-01-13 14:06:14 +00:00
public override Task WriteAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
2026-01-31 15:29:34 +00:00
) => _stream.WriteAsync(buffer, offset, count, cancellationToken);
2026-01-13 14:06:14 +00:00
#if NET8_0_OR_GREATER
public override ValueTask WriteAsync(
ReadOnlyMemory<byte> buffer,
CancellationToken cancellationToken = default
2026-01-31 15:29:34 +00:00
) => _stream.WriteAsync(buffer, cancellationToken);
2026-01-13 14:06:14 +00:00
#endif
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException("Synchronous Write is not supported");
public override void WriteByte(byte value) =>
throw new NotSupportedException("Synchronous WriteByte is not supported");
2026-01-31 15:29:34 +00:00
protected override void Dispose(bool disposing)
{
2026-04-29 11:50:51 +01:00
if (disposing && disposeStream)
2026-01-31 15:29:34 +00:00
{
_stream.Dispose();
}
base.Dispose(disposing);
}
2025-12-31 14:43:15 +00:00
}