Decompress the block into a buffer you size, then read only the number of bytes the decoder returns.
dotnet add package RecursiveAI.OneCompressRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneCompress.Lz4; using System.Text; byte[] block = [0x50, 72, 101, 108, 108, 111]; byte[] output = new byte[5]; int count = Lz4Block.Decompress(block, output); Console.WriteLine(Encoding.UTF8.GetString( output.AsSpan(0, count)));
Unzip the example, then dotnet run.
Prints the five decoded bytes as text:
HelloThe example’s entire input is inline: 0x50 is the token for five literal bytes, followed by the bytes of Hello. The UTF-8 conversion is only for display. If the destination is too small, Decompress throws CompressionLimitException; a truncated or invalid block throws CompressionFormatException.
Variations
Allocate the result with a size limit. Use this after defining block as above if you don’t have a buffer but do have an application limit:
byte[] decoded = Lz4Block.Decompress(block, new Lz4BlockOptions { MaxOutputSize = 1024 }); Console.WriteLine(decoded.Length);
5Check if the block fits a buffer. A four-byte buffer can’t hold this example. TryDecompress returns false when the output doesn’t fit; invalid input can still throw.
Span<byte> buffer = stackalloc byte[4]; if (Lz4Block.TryDecompress(block, buffer, out int written)) Console.WriteLine(Encoding.UTF8.GetString(buffer[..written])); else Console.WriteLine("Output buffer too small.");
Output buffer too small.Related recipes
The short answer
Use Lz4Block.Decompress(block, destination) from RecursiveAI.OneCompress. Give it a span containing exactly one raw block and a destination large enough for the decoded bytes. Read only the number of bytes it returns. For a complete LZ4 frame instead of a raw block, use Lz4.Decompress.
What is the difference between an LZ4 block and an LZ4 frame?
A block holds the compressed sequences. It doesn’t have its own header that tells the reader where the block ends. If an outer format stores blocks inside a packet or a file, that format has to tell you which bytes to pass in. Including the packet header or the start of the next block changes the input to the decoder.
A frame wraps data blocks with a header, block lengths and an end marker. Flags in the header say whether fields such as a content size or checksums are present. A regular LZ4 frame starts with the magic value 0x184D2204, written as 04 22 4D 18 in the file. See the block and frame layouts for the distinction. Choose the decoder based on the producer’s format, not the file extension or the mere absence of those four bytes: an outer format can add its own header to a raw block.
How big should the LZ4 decompression buffer be?
If your protocol provides the original length, check that length against a limit you’re prepared to allocate, then use it to size the destination. If exact agreement with that length matters, compare it with the returned byte count. A reusable buffer can be larger than the output; its remaining bytes aren’t part of the result. This is why the example slices the buffer before converting it to text.
The block doesn’t declare its decoded size up front. If you know the compressed byte range but not the decoded length, the allocating overload in the first variation measures the decoded length before allocating and checks it against MaxOutputSize. It defaults to 64 MiB; use a lower limit when your format calls for smaller messages. With the caller-supplied buffer overload, the buffer length itself sets the limit.