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

60 lines
1.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.IO;
2026-01-20 12:22:38 +00:00
using System.Threading.Tasks;
2022-12-20 15:06:44 +00:00
namespace SharpCompress.Test.Mocks;
// This is a simplified version of CryptoStream that always flushes the inner stream on Dispose to trigger an error in EntryStream
// CryptoStream doesn't always trigger the Flush, so this class is used instead
// See https://referencesource.microsoft.com/#mscorlib/system/security/cryptography/cryptostream.cs,141
2024-03-14 08:57:16 +00:00
public class FlushOnDisposeStream(Stream innerStream) : Stream
{
2024-03-14 08:57:16 +00:00
public override bool CanRead => innerStream.CanRead;
2022-12-20 15:06:44 +00:00
public override bool CanSeek => false;
2022-12-20 15:06:44 +00:00
public override bool CanWrite => false;
2024-03-14 08:57:16 +00:00
public override long Length => innerStream.Length;
2022-12-20 15:06:44 +00:00
public override long Position
{
2024-03-14 08:57:16 +00:00
get => innerStream.Position;
set => innerStream.Position = value;
2022-12-20 15:06:44 +00:00
}
public override void Flush() { }
2022-12-20 15:20:49 +00:00
public override int Read(byte[] buffer, int offset, int count) =>
2024-03-14 08:57:16 +00:00
innerStream.Read(buffer, offset, count);
2022-12-20 15:20:49 +00:00
public override long Seek(long offset, SeekOrigin origin) =>
2022-12-20 15:06:44 +00:00
throw new NotImplementedException();
2022-12-20 15:20:49 +00:00
public override void SetLength(long value) => throw new NotImplementedException();
2022-12-20 15:20:49 +00:00
public override void Write(byte[] buffer, int offset, int count) =>
2022-12-20 15:06:44 +00:00
throw new NotImplementedException();
2022-12-20 15:06:44 +00:00
protected override void Dispose(bool disposing)
{
if (disposing)
2021-01-09 13:33:34 +00:00
{
2024-03-14 08:57:16 +00:00
innerStream.Flush();
innerStream.Close();
2018-10-04 13:13:14 +02:00
}
2022-12-20 15:06:44 +00:00
base.Dispose(disposing);
2018-10-04 13:13:14 +02:00
}
2026-01-20 12:22:38 +00:00
2026-01-22 15:12:18 +00:00
#if !LEGACY_DOTNET
2026-01-20 12:22:38 +00:00
public override async ValueTask DisposeAsync()
{
await innerStream.FlushAsync();
innerStream.Close();
await base.DisposeAsync();
}
#endif
}