Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BrotliEncoder.cs
1using System;
2using System.IO;
3using System.IO.Compression;
4using System.Threading.Tasks;
6
8{
13 {
14 private readonly string? compressedFileName;
15 private TransferEncoding? uncompressedStream;
16 private MemoryStream? ms = null;
17 private BrotliStream? brEncoder = null;
18 private BrotliStream? brDecoder = null;
19 private long? bytesLeft;
20 private int pos = 0;
21 private bool dataWritten = false;
22 private bool finished = false;
23
30 public BrotliEncoder(string? CompressedFileName, TransferEncoding UncompressedStream, long? ExpectedContentLength)
31 : base(null, UncompressedStream)
32 {
33 this.compressedFileName = CompressedFileName;
34 this.uncompressedStream = UncompressedStream;
35 this.bytesLeft = ExpectedContentLength;
36 }
37
42 {
43 this.ms = new MemoryStream();
44 this.brEncoder = new BrotliStream(this.ms, CompressionMode.Compress, true);
45 }
46
52 public override Task BeforeContentAsync(HttpResponse Response, bool ExpectContent)
53 {
54 return this.uncompressedStream!.BeforeContentAsync(Response, ExpectContent);
55 }
56
70 public override Task<ulong> DecodeAsync(bool ConstantBuffer, byte[] Data, int Offset, int NrRead)
71 {
72 return Task.FromResult(0x100000000UL); // TODO: Support Content-Encoding in POST, PUT and PATCH, etc.
73 }
74
84 public override async Task<bool> EncodeAsync(bool ConstantBuffer, byte[] Data, int Offset, int NrBytes, bool LastData)
85 {
86 if (NrBytes > 0)
87 {
88 if (this.finished)
89 return false;
90
91 this.dataWritten = true;
92
93 if (this.bytesLeft.HasValue)
94 {
95 if (NrBytes > this.bytesLeft.Value)
96 NrBytes = (int)this.bytesLeft.Value;
97
98 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
99
100 this.bytesLeft -= NrBytes;
101 if (this.bytesLeft <= 0)
102 {
103 this.finished = true;
104 await this.FlushAsync(LastData);
105 }
106 }
107 else
108 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
109 }
110
111 return true;
112 }
113
118 public override async Task<bool> FlushAsync(bool EndOfData)
119 {
120 if (this.dataWritten)
121 {
122 await this.brEncoder!.FlushAsync();
123 this.dataWritten = false;
124
125 byte[] Data = this.ms!.ToArray();
126 int c = Data.Length;
127 if (this.pos < c)
128 {
129 c -= this.pos;
130 if (!await this.uncompressedStream!.EncodeAsync(true, Data, this.pos, c, EndOfData))
131 return false;
132
133 if (!string.IsNullOrEmpty(this.compressedFileName))
134 {
135 if (this.pos == 0)
136 await Files.WriteAllBytesAsync(this.compressedFileName, Data, this.pos, c);
137 else
138 await Files.AppendAllBytesAsync(this.compressedFileName, Data, this.pos, c);
139 }
140
141 this.pos += c;
142 }
143
144 return await this.uncompressedStream!.FlushAsync(EndOfData);
145 }
146
147 return true;
148 }
149
153 public override async Task<bool> ContentSentAsync()
154 {
155 await this.FlushAsync(true);
156 return await this.uncompressedStream!.ContentSentAsync();
157 }
158
162 public override void Dispose()
163 {
164 this.uncompressedStream?.Dispose();
165 this.uncompressedStream = null;
166
167 this.brEncoder?.Dispose();
168 this.brEncoder = null;
169
170 this.brDecoder?.Dispose();
171 this.brDecoder = null;
172
173 this.ms?.Dispose();
174 this.ms = null;
175
176 base.Dispose();
177 }
178
182 public override bool InvalidEncoding => this.uncompressedStream!.InvalidEncoding;
183
187 public override bool TransferError => this.uncompressedStream!.TransferError;
188 }
189}
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.
Definition: HttpResponse.cs:23
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.
Contains static methods
Definition: Files.cs:14
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
Definition: Files.cs:33
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
Definition: Files.cs:58