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;
5using Waher.Content;
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
68 public override Task<ulong> DecodeAsync(byte[] Data, int Offset, int NrRead)
69 {
70 return Task.FromResult(0x100000000UL); // TODO: Support Content-Encoding in POST, PUT and PATCH, etc.
71 }
72
79 public override async Task<bool> EncodeAsync(byte[] Data, int Offset, int NrBytes)
80 {
81 if (NrBytes > 0)
82 {
83 if (this.finished)
84 return false;
85
86 this.dataWritten = true;
87
88 if (this.bytesLeft.HasValue)
89 {
90 if (NrBytes > this.bytesLeft.Value)
91 NrBytes = (int)this.bytesLeft.Value;
92
93 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
94
95 this.bytesLeft -= NrBytes;
96 if (this.bytesLeft <= 0)
97 {
98 this.finished = true;
99 await this.FlushAsync();
100 }
101 }
102 else
103 await this.brEncoder!.WriteAsync(Data, Offset, NrBytes);
104 }
105
106 return true;
107 }
108
112 public override async Task<bool> FlushAsync()
113 {
114 if (this.dataWritten)
115 {
116 await this.brEncoder!.FlushAsync();
117 this.dataWritten = false;
118
119 byte[] Data = this.ms!.ToArray();
120 int c = Data.Length;
121 if (this.pos < c)
122 {
123 c -= this.pos;
124 if (!await this.uncompressedStream!.EncodeAsync(Data, this.pos, c))
125 return false;
126
127 if (!string.IsNullOrEmpty(this.compressedFileName))
128 {
129 if (this.pos == 0)
130 await Resources.WriteAllBytesAsync(this.compressedFileName, Data, this.pos, c);
131 else
132 await Resources.AppendAllBytesAsync(this.compressedFileName, Data, this.pos, c);
133 }
134
135 this.pos += c;
136 }
137
138 return await this.uncompressedStream!.FlushAsync();
139 }
140
141 return true;
142 }
143
147 public override async Task<bool> ContentSentAsync()
148 {
149 await this.FlushAsync();
150 return await this.uncompressedStream!.ContentSentAsync();
151 }
152
156 public override void Dispose()
157 {
158 this.uncompressedStream?.Dispose();
159 this.uncompressedStream = null;
160
161 this.brEncoder?.Dispose();
162 this.brEncoder = null;
163
164 this.brDecoder?.Dispose();
165 this.brDecoder = null;
166
167 this.ms?.Dispose();
168 this.ms = null;
169
170 base.Dispose();
171 }
172
176 public override bool InvalidEncoding => this.uncompressedStream!.InvalidEncoding;
177
181 public override bool TransferError => this.uncompressedStream!.TransferError;
182 }
183}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
Definition: Resources.cs:216
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
Definition: Resources.cs:241
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.
Definition: HttpResponse.cs:21
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.