Skip to content

Commit 7fd198a

Browse files
committed
Add async decompressStream functions
1 parent 0223872 commit 7fd198a

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.7
22
import PackageDescription
33

44
let package = Package(
55
name: "compress-nio",
6+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13)],
67
products: [
78
.library(name: "CompressNIO", targets: ["CompressNIO"]),
89
],

Sources/CompressNIO/ByteBuffer+Compressor.swift

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension ByteBuffer {
8686
/// - Returns: `ByteBuffer` containing compressed data
8787
public mutating func decompressStream(
8888
with decompressor: NIODecompressor,
89-
process: (ByteBuffer)->()
89+
process: (ByteBuffer) throws -> ()
9090
) throws {
9191
guard var window = decompressor.window else {
9292
preconditionFailure("decompressString(with:flush:process requires your compressor has a window buffer")
@@ -95,14 +95,48 @@ extension ByteBuffer {
9595
do {
9696
try decompressStream(to: &window, with: decompressor)
9797
} catch let error as CompressNIOError where error == .bufferOverflow {
98-
process(window)
98+
try process(window)
9999
window.moveReaderIndex(to: 0)
100100
window.moveWriterIndex(to: 0)
101101
}
102102
}
103103

104104
if window.readableBytes > 0 {
105-
process(window)
105+
try process(window)
106+
}
107+
}
108+
109+
/// A version of decompressStream which you provide a fixed sized window buffer to and a process closure.
110+
///
111+
/// When the window buffer is full the process closure is called. If there is any unprocessed data left
112+
/// at the end of the compress the process closure is called with this.
113+
///
114+
/// Before calling this you need to provide a working window `ByteBuffer` to the decompressor by
115+
/// setting `NIODecompressor.window`.
116+
///
117+
/// - Parameters:
118+
/// - compressor: Algorithm to use when decompressing
119+
/// - process: Closure to be called when window buffer fills up or decompress has finished
120+
/// - Returns: `ByteBuffer` containing compressed data
121+
public mutating func decompressStream(
122+
with decompressor: NIODecompressor,
123+
process: (ByteBuffer) async throws -> ()
124+
) async throws {
125+
guard var window = decompressor.window else {
126+
preconditionFailure("decompressString(with:flush:process requires your compressor has a window buffer")
127+
}
128+
while self.readableBytes > 0 {
129+
do {
130+
try decompressStream(to: &window, with: decompressor)
131+
} catch let error as CompressNIOError where error == .bufferOverflow {
132+
try await process(window)
133+
window.moveReaderIndex(to: 0)
134+
window.moveWriterIndex(to: 0)
135+
}
136+
}
137+
138+
if window.readableBytes > 0 {
139+
try await process(window)
106140
}
107141
}
108142

@@ -215,14 +249,77 @@ extension ByteBuffer {
215249
public mutating func compressStream(
216250
with compressor: NIOCompressor,
217251
flush: CompressNIOFlush,
218-
process: (ByteBuffer)->()
252+
process: (ByteBuffer) throws -> ()
219253
) throws {
220254
guard var window = compressor.window else { preconditionFailure("compressString(with:flush:process requires your compressor has a window buffer") }
221255
while self.readableBytes > 0 {
222256
do {
223257
try compressStream(to: &window, with: compressor, flush: .no)
224258
} catch let error as CompressNIOError where error == .bufferOverflow {
225-
process(window)
259+
try process(window)
260+
window.moveReaderIndex(to: 0)
261+
window.moveWriterIndex(to: 0)
262+
}
263+
}
264+
265+
if flush == .sync {
266+
while true {
267+
do {
268+
try compressStream(to: &window, with: compressor, flush: .sync)
269+
break
270+
} catch let error as CompressNIOError where error == .bufferOverflow {
271+
try process(window)
272+
window.moveReaderIndex(to: 0)
273+
window.moveWriterIndex(to: 0)
274+
}
275+
}
276+
} else if flush == .finish {
277+
while true {
278+
do {
279+
try compressStream(to: &window, with: compressor, flush: .finish)
280+
break
281+
} catch let error as CompressNIOError where error == .bufferOverflow {
282+
try process(window)
283+
window.moveReaderIndex(to: 0)
284+
window.moveWriterIndex(to: 0)
285+
}
286+
}
287+
}
288+
289+
if flush == .finish {
290+
if window.readableBytes > 0 {
291+
try process(window)
292+
window.moveReaderIndex(to: 0)
293+
window.moveWriterIndex(to: 0)
294+
}
295+
}
296+
compressor.window = window
297+
}
298+
299+
/// A version of compressStream which you provide a fixed sized window buffer to and a process closure.
300+
///
301+
/// When the window buffer is full the process closure is called. If there is any unprocessed data left
302+
/// at the end of the compress the process closure is called with this.
303+
///
304+
/// Before calling this you need to provide a working window `ByteBuffer` to the compressor by setting
305+
/// `NIOCompressor.window`.
306+
///
307+
/// - Parameters:
308+
/// - compressor: Algorithm to use when compressing
309+
/// - flush: how compressor should flush output data.
310+
/// - process: Closure to be called when window buffer fills up or compress has finished
311+
/// - Returns: `ByteBuffer` containing compressed data
312+
public mutating func compressStream(
313+
with compressor: NIOCompressor,
314+
flush: CompressNIOFlush,
315+
process: (ByteBuffer) async throws -> ()
316+
) async throws {
317+
guard var window = compressor.window else { preconditionFailure("compressString(with:flush:process requires your compressor has a window buffer") }
318+
while self.readableBytes > 0 {
319+
do {
320+
try compressStream(to: &window, with: compressor, flush: .no)
321+
} catch let error as CompressNIOError where error == .bufferOverflow {
322+
try await process(window)
226323
window.moveReaderIndex(to: 0)
227324
window.moveWriterIndex(to: 0)
228325
}
@@ -234,7 +331,7 @@ extension ByteBuffer {
234331
try compressStream(to: &window, with: compressor, flush: .sync)
235332
break
236333
} catch let error as CompressNIOError where error == .bufferOverflow {
237-
process(window)
334+
try await process(window)
238335
window.moveReaderIndex(to: 0)
239336
window.moveWriterIndex(to: 0)
240337
}
@@ -245,7 +342,7 @@ extension ByteBuffer {
245342
try compressStream(to: &window, with: compressor, flush: .finish)
246343
break
247344
} catch let error as CompressNIOError where error == .bufferOverflow {
248-
process(window)
345+
try await process(window)
249346
window.moveReaderIndex(to: 0)
250347
window.moveWriterIndex(to: 0)
251348
}
@@ -254,7 +351,7 @@ extension ByteBuffer {
254351

255352
if flush == .finish {
256353
if window.readableBytes > 0 {
257-
process(window)
354+
try await process(window)
258355
window.moveReaderIndex(to: 0)
259356
window.moveWriterIndex(to: 0)
260357
}

0 commit comments

Comments
 (0)