3using System.IO.Compression;
4using System.Threading.Tasks;
14 private readonly
string? compressedFileName;
16 private MemoryStream? ms =
null;
17 private BrotliStream? brEncoder =
null;
18 private BrotliStream? brDecoder =
null;
19 private long? bytesLeft;
21 private bool dataWritten =
false;
22 private bool finished =
false;
31 : base(null, UncompressedStream)
33 this.compressedFileName = CompressedFileName;
34 this.uncompressedStream = UncompressedStream;
35 this.bytesLeft = ExpectedContentLength;
43 this.ms =
new MemoryStream();
44 this.brEncoder =
new BrotliStream(this.ms, CompressionMode.Compress,
true);
70 public override Task<ulong>
DecodeAsync(
bool ConstantBuffer,
byte[] Data,
int Offset,
int NrRead)
72 return Task.FromResult(0x100000000UL);
84 public override async Task<bool>
EncodeAsync(
bool ConstantBuffer,
byte[] Data,
int Offset,
int NrBytes,
bool LastData)
91 this.dataWritten =
true;
93 if (this.bytesLeft.HasValue)
95 if (NrBytes > this.bytesLeft.Value)
96 NrBytes = (int)this.bytesLeft.Value;
98 await
this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
100 this.bytesLeft -= NrBytes;
101 if (this.bytesLeft <= 0)
103 this.finished =
true;
108 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
120 if (this.dataWritten)
122 await this.brEncoder!.FlushAsync();
123 this.dataWritten =
false;
125 byte[] Data = this.ms!.ToArray();
130 if (!await this.uncompressedStream!.
EncodeAsync(
true, Data, this.pos, c, EndOfData))
133 if (!
string.IsNullOrEmpty(this.compressedFileName))
144 return await this.uncompressedStream!.
FlushAsync(EndOfData);
164 this.uncompressedStream?.
Dispose();
165 this.uncompressedStream =
null;
167 this.brEncoder?.Dispose();
168 this.brEncoder =
null;
170 this.brDecoder?.Dispose();
171 this.brDecoder =
null;
Class performing br encoding and decoding.
override async Task< bool > FlushAsync(bool EndOfData)
Sends any remaining data to the client.
override bool TransferError
If the transfer failed.
override async Task< bool > EncodeAsync(bool ConstantBuffer, byte[] Data, int Offset, int NrBytes, bool LastData)
Is called when new binary data is to be sent and needs to be encoded.
BrotliEncoder(string? CompressedFileName, TransferEncoding UncompressedStream, long? ExpectedContentLength)
Class performing br encoding and decoding.
override async Task< bool > ContentSentAsync()
Is called when the content has all been sent to the encoder. The method sends any cached data to the ...
override void Dispose()
IDisposable.Dispose
override Task BeforeContentAsync(HttpResponse Response, bool ExpectContent)
Is called when the header is complete, and before content is being transferred.
override Task< ulong > DecodeAsync(bool ConstantBuffer, byte[] Data, int Offset, int NrRead)
Is called when new binary data has been received that needs to be decoded.
override bool InvalidEncoding
If encoding of data was invalid.
void PrepareForCompression()
Prepares the encoder for compression
Represets a response of an HTTP client request.
Base class for all transfer encodings.
virtual void Dispose()
IDisposable.Dispose
virtual bool InvalidEncoding
If encoding of data was invalid.
abstract Task< bool > FlushAsync(bool EndOfData)
Sends any remaining data to the client.
abstract Task< bool > ContentSentAsync()
Is called when the content has all been sent to the encoder. The method sends any cached data to the ...
virtual bool TransferError
If the transfer failed.
virtual Task BeforeContentAsync(HttpResponse Response, bool ExpectContent)
Is called when the header is complete, and before content is being transferred.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.