Class Zstandard
- Namespace
- Egami.Compression
- Assembly
- Egami.dll
Zstandard (RFC 8878) compression in managed code: the codec TIFF's ZSTD compression uses, for format plugins and apps that need it too, such as KTX 2's supercompressed mip levels. Both directions work on spans the caller sizes, and each thread keeps its tables, so a warm call allocates nothing.
public static class Zstandard
- Inheritance
-
Zstandard
- Inherited Members
Examples
// Compress into a buffer sized for the worst case, then keep the bytes written.
byte[] packed = new byte[Zstandard.MaxCompressedLength(data.Length)];
int written = Zstandard.Compress(data, packed, level: 9);
// Decompress into a buffer of the size you know (KTX 2 stores each level's): it returns the bytes written.
byte[] unpacked = new byte[data.Length];
int length = Zstandard.Decompress(packed.AsSpan(0, written), unpacked);
Fields
DefaultLevel
The level Compress(ReadOnlySpan<byte>, Span<byte>, int) uses when none is given, as the zstd tool's default.
public const int DefaultLevel = 3
Field Value
MaxLevel
The highest compression level, the smallest output.
public const int MaxLevel = 22
Field Value
MinLevel
The lowest compression level, the fastest.
public const int MinLevel = 1
Field Value
Methods
Compress(ReadOnlySpan<byte>, Span<byte>, int)
Compresses source into one Zstandard frame that records its content size and has no checksum.
public static int Compress(ReadOnlySpan<byte> source, Span<byte> destination, int level = 3)
Parameters
sourceReadOnlySpan<byte>The bytes to compress.
destinationSpan<byte>Where the frame goes; it needs room for MaxCompressedLength(int) bytes.
levelint
Returns
- int
The bytes written.
Exceptions
- ArgumentOutOfRangeException
levelis out of range.- ArgumentException
destinationis shorter than MaxCompressedLength(int).
Decompress(ReadOnlySpan<byte>, Span<byte>)
Decompresses the Zstandard frames of source, one after another, into destination.
Skippable frames are skipped and a frame's checksum is checked when it has one.
public static int Decompress(ReadOnlySpan<byte> source, Span<byte> destination)
Parameters
sourceReadOnlySpan<byte>The compressed frames.
destinationSpan<byte>Where the bytes go; the data must fit in it.
Returns
- int
The bytes written. Bytes after that count may have been written to.
Exceptions
- CorruptDataException
The data breaks the format, or needs more room than
destinationhas.- UnsupportedFormatException
A frame needs a dictionary.
MaxCompressedLength(int)
Gets the most bytes Compress(ReadOnlySpan<byte>, Span<byte>, int) can write for length bytes of input.
public static int MaxCompressedLength(int length)
Parameters
lengthintThe number of bytes to compress.
Returns
- int
The room the output needs.
Exceptions
- ArgumentOutOfRangeException
lengthis negative, or the result does not fit an int.