Contents

adam-fowler/compress-nio

Compression/Decompression support for Swift NIO ByteBuffer

Compressing

There are three methods for doing stream compressing: window, allocating and raw. All of them start with calling compressor.startStream and end with calling compressor.finishStream.

Window method

For the window method you provide a working buffer for the compressor to use. When you call compressStream it compresses into this buffer and when the buffer is full it will call a process closure you have provided.

let compressor = ZlibCompressor(algorithm: .gzip)
var window = ByteBufferAllocator().buffer(capacity: 64*1024)
while var buffer = getData() {
    try buffer.compressStream(with: compressor, window: window, flush: .finish) { buffer in
        // process your compressed data
    }
}
try compressor.reset()
Allocation method

With the allocating method you leave the compressor to allocate the ByteBuffers for output data. It will calculate the maximum possible size the compressed data could be and allocates that amount of space for each compressed data block. The last compressed block needs to have the flush parameter set to .finish

let compressor = ZlibCompressor(algorithm: .gzip)
while var buffer = getData() {
    let flush: CompressNIOFlush = isThisTheFinalBlock ? .finish : .sync
    let compressedBuffer = try buffer.compressStream(with: compressor, flush: flush, allocator: ByteBufferAllocator())
}
try compressor.reset()

If you don't know what is your final data block you can always compress an empty ByteBuffer with the flush set to .finish to get your final block. Also note that the flush parameter is set to .sync in the loop. This is required otherwise the next compressStream cannot successfully estimate its buffer size as there might be buffered data still waiting to be output.

Raw method

With this mehod you call the lowest level function and deal with .bufferOverflow errors thrown whenever you run out of space in your output buffer. You will need a loop for receiving data and then you will need an inner loop for compressing that data. You call the compress until you have no more data to compress. Everytime you receive a .bufferOverflow error you have to provide a new output data. Once you have read all the input data you do the same again but with the flush parameter set to .finish.

Decompressing

The same three methods window, allocation, raw are available for decompressing streamed data but you don't need to set a flush parameter to .finish while decompressing which makes everything a little easier.

Package Metadata

Repository: adam-fowler/compress-nio

Stars: 27

Forks: 5

Open issues: 0

Default branch: main

Primary language: swift

License: Apache-2.0

Topics: bytebuffer, compression, decompression, lz4, swift, swift-nio, zlib

README: README.md