You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Expose ZlibCompressor and make it non-Copyable
* Use latest swift image in api-breakage.yml
* Make ZlibAlgorithm sendable
* Update package.swift
* Revert ZlibCompressor/Decompressor back to a class
Also clean up interface so when they are allocated they are active already, instead of having non-initialized state
* Add finishWindowedStream back in and deprecate it
* Update README
Copy file name to clipboardExpand all lines: README.md
+11-12Lines changed: 11 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,12 @@ var uncompressedBuffer = buffer.decompress(with: .gzip)
11
11
These methods allocate a new `ByteBuffer` for you. The `decompress` method can allocate multiple `ByteBuffers` while it is uncompressing depending on how well compressed the original `ByteBuffer` is. It is preferable to know in advance the size of buffer you need and allocate it yourself just the once and use the following functions.
12
12
```swift
13
13
let uncompressedSize = buffer.readableBytes
14
-
let maxCompressedSize = CompressionAlgorithm.deflate.compressor.maxSize(from:buffer)
15
-
var compressedBuffer =ByteBufferAllocator().buffer(capacity: maxCompressedSize)
14
+
var compressedBuffer =ByteBufferAllocator().buffer(capacity: knownCompressedSize)
In the above example there is a call to a function `CompressionAlgorithm.deflate.compressor.maxSize(from:buffer)`. This returns the maximum size of buffer required to write out compressed data for the `deflate` compression algorithm.
19
+
This returns the maximum size of buffer required to write out compressed data for the `deflate` compression algorithm.
21
20
22
21
If you provide a buffer that is too small a `CompressNIO.bufferOverflow` error is thrown. You will need to provide a larger `ByteBuffer` to complete your operation.
23
22
@@ -31,29 +30,29 @@ There are three methods for doing stream compressing: window, allocating and raw
31
30
#### Window method
32
31
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.
33
32
```swift
34
-
let compressor = CompressionAlgorithm.gzip.compressor
var window =ByteBufferAllocator().buffer(capacity: 64*1024)
37
35
whilevar buffer =getData() {
38
-
try buffer.compressStream(with: compressor, flush: .finish) { buffer in
36
+
try buffer.compressStream(with: compressor, window: window, flush: .finish) { buffer in
39
37
// process your compressed data
40
38
}
41
39
}
42
-
try compressor.finishStream()
40
+
try compressor.reset()
43
41
```
44
42
#### Allocation method
45
43
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`
46
44
```swift
47
-
let compressor = CompressionAlgorithm.gzip.compressor
48
-
try compressor.startStream()
45
+
let compressor =ZlibCompressor(algorithm: .gzip)
49
46
whilevar buffer =getData() {
50
47
let flush: CompressNIOFlush = isThisTheFinalBlock ? .finish: .sync
51
48
let compressedBuffer =try buffer.compressStream(with: compressor, flush: flush, allocator: ByteBufferAllocator())
52
49
}
53
-
try compressor.finishStream()
50
+
try compressor.reset()
54
51
```
55
-
If you don't know when you are receiving your last 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.
52
+
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.
53
+
56
54
#### Raw method
55
+
57
56
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`.
0 commit comments