Skip to main content

BrotliStream

Namespace: System.IO.Compression

Assembly: Wisej.Web.Ext.Brotli (4.1.0.0)

Represents a Brotli stream for compression or decompression.

public class BrotliStream : BrotliStream

This class uses the same name and constructors as the System.IO.Compression.BrotliStream class of .NET Core, so that code written for .NET Core also compiles on .NET Framework, which doesn't include Brotli support. The compression is performed by the native Brotli library (brolib_x86.dll or brolib_x64.dll), which is embedded in this assembly and extracted to the temporary folder the first time the class is used. The native library is a Windows DLL, so this class works only on Windows.

The extension also includes a Brotli decoder for the browser, which replaces the default GZip decoder used by Wisej.

Example:

The following example compresses a string and decompresses it again:

using System.IO;
using System.IO.Compression;
using System.Text;

var data = Encoding.UTF8.GetBytes("Hello, Brotli! Hello, Brotli! Hello, Brotli!");

// compress.
var compressed = new MemoryStream();
using (var brotli = new BrotliStream(compressed, CompressionLevel.Optimal, leaveOpen: true))
{
brotli.Write(data, 0, data.Length);
}

// decompress.
compressed.Position = 0;
using (var brotli = new BrotliStream(compressed, CompressionMode.Decompress))
using (var reader = new StreamReader(brotli, Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd()); // Hello, Brotli! Hello, Brotli! Hello, Brotli!
}

Constructors​

Instance member BrotliStream(stream, level, leaveOpen)​

Initializes a new instance of the BrotliStream class using the specified stream and compression level, and optionally leaves the stream open.

NameTypeDescription
streamStreamThe stream to compress or decompress.
levelCompressionLevelOne of CompressionLevel values that indicates the level of compression to adopt.
leaveOpenBooleantrue to leave the stream open after disposing the BrotliStream object; otherwise, false.

The stream is created in Compress mode. The compression level is mapped to the Brotli quality: NoCompression uses quality 0, Fastest uses quality 1 and Optimal uses quality 9. Use SetQuality to select a different quality between 0 and 11.

The compressed data is completed when the BrotliStream is disposed. Set leaveOpen to true to read the compressed data from stream after disposing the BrotliStream.

Throws:

Example:

The following example compresses a file and keeps the output stream open to read its length:

using (var input = File.OpenRead("report.json"))
using (var output = new MemoryStream())
{
using (var brotli = new BrotliStream(output, CompressionLevel.Fastest, leaveOpen: true))
{
input.CopyTo(brotli);
}

Console.WriteLine($"Compressed {input.Length} bytes to {output.Length} bytes.");
}

Instance member BrotliStream(stream, level)​

Initializes a new instance of the BrotliStream class using the specified stream and compression level.

NameTypeDescription
streamStreamThe stream to compress.
levelCompressionLevelOne of CompressionLevel values that indicates the level of compression to adopt.

The stream is created in Compress mode and stream is closed when the BrotliStream is disposed. See #ctor for how level is mapped to the Brotli quality.

Throws:

Example:

The following example saves a Brotli compressed copy of a file:

using (var input = File.OpenRead("styles.css"))
using (var output = File.Create("styles.css.br"))
using (var brotli = new BrotliStream(output, CompressionLevel.Optimal))
{
input.CopyTo(brotli);
}

Instance member BrotliStream(stream, mode, leaveOpen)​

Initializes a new instance of the BrotliStream class using the specified stream and compression mode, and optionally leaves the stream open.

NameTypeDescription
streamStreamThe stream to compress or decompress.
modeCompressionModeOne of the enumeration values that indicates whether to compress or decompress the stream.
leaveOpenBooleantrue to leave the stream open after disposing the BrotliStream object; otherwise, false.

In Compress mode, write the data to the BrotliStream and the compressed data is written to stream ; the default quality is 5. In Decompress mode, read from the BrotliStream to get the decompressed data from stream .

Throws:

  • ArgumentNullException stream is null.
  • BrotliException The native Brotli encoder or decoder can't be created.

Example:

The following example decompresses the Brotli data received in a request without closing the request stream:

private string ReadBrotliBody(Stream body)
{
using (var brotli = new BrotliStream(body, CompressionMode.Decompress, leaveOpen: true))
using (var reader = new StreamReader(brotli))
{
return reader.ReadToEnd();
}
}

Instance member BrotliStream(stream, mode)​

Initializes a new instance of the BrotliStream class using the specified stream and compression mode.

NameTypeDescription
streamStreamThe stream to compress or decompress.
modeCompressionModeOne of the enumeration values that indicates whether to compress or decompress the stream.

stream is closed when the BrotliStream is disposed.

Throws:

  • ArgumentNullException stream is null.
  • BrotliException The native Brotli encoder or decoder can't be created.

Example:

The following example restores a file compressed with Brotli:

using (var input = File.OpenRead("styles.css.br"))
using (var brotli = new BrotliStream(input, CompressionMode.Decompress))
using (var output = File.Create("styles.css"))
{
brotli.CopyTo(output);
}