Use streamable compression (needs file cache clear)
This commit is contained in:
@@ -1,60 +1,12 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public class CountedStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
public ulong BytesRead { get; private set; }
|
||||
public ulong BytesWritten { get; private set; }
|
||||
|
||||
public CountedStream(Stream underlyingStream)
|
||||
{
|
||||
_stream = underlyingStream;
|
||||
}
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
|
||||
public override bool CanSeek => _stream.CanSeek;
|
||||
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => _stream.Position = value; }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int n = _stream.Read(buffer, offset, count);
|
||||
BytesRead += (ulong)n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return _stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
BytesWritten += (ulong)count;
|
||||
_stream.Write(buffer, offset, count);
|
||||
}
|
||||
}
|
||||
|
||||
// Concatenates the content of multiple readable streams
|
||||
public class ConcatenatedStreamReader : Stream
|
||||
{
|
||||
private IEnumerable<Stream> _streams;
|
||||
private IEnumerator<Stream> _iter;
|
||||
private bool _finished;
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public ConcatenatedStreamReader(IEnumerable<Stream> streams)
|
||||
{
|
||||
@@ -63,12 +15,17 @@ public class ConcatenatedStreamReader : Stream
|
||||
_finished = !_iter.MoveNext();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
foreach (var stream in _streams)
|
||||
stream.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public override long Length => throw new NotSupportedException();
|
||||
|
||||
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
|
||||
@@ -92,6 +49,24 @@ public class ConcatenatedStreamReader : Stream
|
||||
return n;
|
||||
}
|
||||
|
||||
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
while (n == 0 && !_finished)
|
||||
{
|
||||
n = await _iter.Current.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
if (n == 0)
|
||||
_finished = !_iter.MoveNext();
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
@@ -0,0 +1,73 @@
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Counts the number of bytes read/written to an underlying stream
|
||||
public class CountedStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
public long BytesRead { get; private set; }
|
||||
public long BytesWritten { get; private set; }
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream UnderlyingStream { get => _stream; }
|
||||
|
||||
public CountedStream(Stream underlyingStream)
|
||||
{
|
||||
_stream = underlyingStream;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
public override bool CanSeek => _stream.CanSeek;
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => _stream.Position = value; }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int n = _stream.Read(buffer, offset, count);
|
||||
BytesRead += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int n = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
BytesRead += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return _stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_stream.Write(buffer, offset, count);
|
||||
BytesWritten += count;
|
||||
}
|
||||
|
||||
public async override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
await _stream.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
BytesWritten += count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Calculates the hash of content read or written to a stream
|
||||
public class HashingStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
private readonly HashAlgorithm _hashAlgo;
|
||||
private bool _finished = false;
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream UnderlyingStream { get => _stream; }
|
||||
|
||||
public HashingStream(Stream underlyingStream, HashAlgorithm hashAlgo)
|
||||
{
|
||||
_stream = underlyingStream;
|
||||
_hashAlgo = hashAlgo;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
if (!_finished)
|
||||
_stream.Dispose();
|
||||
_hashAlgo.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => throw new NotSupportedException(); }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
int n = _stream.Read(buffer, offset, count);
|
||||
if (n > 0)
|
||||
_hashAlgo.TransformBlock(buffer, offset, n, buffer, offset);
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
_stream.Write(buffer, offset, count);
|
||||
string x = new(System.Text.Encoding.ASCII.GetChars(buffer.AsSpan().Slice(offset, count).ToArray()));
|
||||
_hashAlgo.TransformBlock(buffer, offset, count, buffer, offset);
|
||||
}
|
||||
|
||||
public byte[] Finish()
|
||||
{
|
||||
if (_finished)
|
||||
return _hashAlgo.Hash;
|
||||
_hashAlgo.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
|
||||
if (DisposeUnderlying)
|
||||
_stream.Dispose();
|
||||
return _hashAlgo.Hash;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Writes data read from one stream out to a second stream
|
||||
public class TeeStream : Stream
|
||||
{
|
||||
private readonly Stream _inStream;
|
||||
private readonly Stream _outStream;
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream InStream { get => _inStream; }
|
||||
public Stream OutStream { get => _outStream; }
|
||||
|
||||
public TeeStream(Stream inStream, Stream outStream)
|
||||
{
|
||||
_inStream = inStream;
|
||||
_outStream = outStream;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
_inStream.Dispose();
|
||||
_outStream.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _inStream.CanRead;
|
||||
public override bool CanSeek => _inStream.CanSeek;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => _inStream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _inStream.Position;
|
||||
set => _inStream.Position = value;
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_inStream.Flush();
|
||||
_outStream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int n = _inStream.Read(buffer, offset, count);
|
||||
if (n > 0)
|
||||
_outStream.Write(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int n = await _inStream.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
if (n > 0)
|
||||
await _outStream.WriteAsync(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return _inStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
_inStream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user