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);
68 public override Task<ulong>
DecodeAsync(
byte[] Data,
int Offset,
int NrRead)
70 return Task.FromResult(0x100000000UL);
79 public override async Task<bool>
EncodeAsync(
byte[] Data,
int Offset,
int NrBytes)
86 this.dataWritten =
true;
88 if (this.bytesLeft.HasValue)
90 if (NrBytes > this.bytesLeft.Value)
91 NrBytes = (int)this.bytesLeft.Value;
93 await
this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
95 this.bytesLeft -= NrBytes;
96 if (this.bytesLeft <= 0)
103 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
114 if (this.dataWritten)
116 await this.brEncoder!.FlushAsync();
117 this.dataWritten =
false;
119 byte[] Data = this.ms!.ToArray();
124 if (!await this.uncompressedStream!.
EncodeAsync(Data, this.pos, c))
127 if (!
string.IsNullOrEmpty(this.compressedFileName))
138 return await this.uncompressedStream!.
FlushAsync();
158 this.uncompressedStream?.
Dispose();
159 this.uncompressedStream =
null;
161 this.brEncoder?.Dispose();
162 this.brEncoder =
null;
164 this.brDecoder?.Dispose();
165 this.brDecoder =
null;
Static class managing loading of resources stored as embedded resources or in content files.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
Class performing br encoding and decoding.
override async Task< bool > FlushAsync()
Sends any remaining data to the client.
override bool TransferError
If the transfer failed.
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 Task< ulong > DecodeAsync(byte[] Data, int Offset, int NrRead)
Is called when new binary data has been received that needs to be decoded.
override void Dispose()
IDisposable.Dispose
override async Task< bool > EncodeAsync(byte[] Data, int Offset, int NrBytes)
Is called when new binary data is to be sent and needs to be encoded.
override Task BeforeContentAsync(HttpResponse Response, bool ExpectContent)
Is called when the header is complete, and before content is being transferred.
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 > 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.
abstract Task< bool > FlushAsync()
Sends any remaining data to the client.