@@ -44,27 +44,24 @@ public enum Algorithm {
44
44
/**
45
45
Compression errors
46
46
*/
47
- public enum FilterError : Error {
48
-
47
+ public enum FilterError : Error {
49
48
/// Filter failed to initialize
50
- case FilterInitError
49
+ case filterInitError
51
50
52
51
/// Invalid data in a call to compression_stream_process
53
- case FilterProcessError
52
+ case filterProcessError
54
53
55
54
/// Non-empty write after an output filter has been finalized
56
- case WriteToFinalizedFilter
55
+ case writeToFinalizedFilter
57
56
58
57
/// Invalid count argument in a read() call
59
- case ReadCountInvalid
60
-
58
+ case readCountInvalid
61
59
}
62
60
63
61
/**
64
62
Compression filter direction of operation, compress/decompress
65
63
*/
66
64
public enum FilterOperation {
67
-
68
65
/// Compress raw data to a compressed payload
69
66
case compress
70
67
@@ -77,7 +74,6 @@ public enum FilterOperation {
77
74
case . decompress: return COMPRESSION_STREAM_DECODE
78
75
}
79
76
}
80
-
81
77
}
82
78
83
79
@available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
@@ -89,29 +85,27 @@ extension compression_stream {
89
85
- Parameter operation: direction of operation
90
86
- Parameter algorithm: compression algorithm
91
87
92
- - Throws: `FilterError.FilterInitError ` if `algorithm` is not supported by the Compression stream API
88
+ - Throws: `FilterError.filterInitError ` if `algorithm` is not supported by the Compression stream API
93
89
*/
94
- init ( operation: FilterOperation , algorithm: Algorithm ) throws
95
- {
90
+ init ( operation: FilterOperation , algorithm: Algorithm ) throws {
96
91
self . init ( dst_ptr: UnsafeMutablePointer< UInt8> . allocate( capacity: 0 ) ,
97
92
dst_size: 0 ,
98
93
src_ptr: UnsafeMutablePointer< UInt8> . allocate( capacity: 0 ) ,
99
94
src_size: 0 ,
100
95
state: nil )
101
96
let status = compression_stream_init ( & self , operation. rawValue, algorithm. rawValue)
102
- guard status == COMPRESSION_STATUS_OK else { throw FilterError . FilterInitError }
97
+ guard status == COMPRESSION_STATUS_OK else { throw FilterError . filterInitError }
103
98
}
104
99
105
100
}
106
101
107
102
@available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
108
103
public class OutputFilter {
109
-
110
- private var _stream : compression_stream
111
- private var _buf : UnsafeMutablePointer < UInt8 >
112
- private var _bufCapacity : Int
113
- private var _writeFunc : ( Data ? ) throws -> ( )
114
- private var _finalized : Bool = false
104
+ private var _stream : compression_stream
105
+ private var _buf : UnsafeMutablePointer < UInt8 >
106
+ private let _bufCapacity : Int
107
+ private let _writeFunc : ( Data ? ) throws -> ( )
108
+ private var _finalized : Bool = false
115
109
116
110
/**
117
111
Initialize an output filter
@@ -124,11 +118,12 @@ public class OutputFilter {
124
118
125
119
- Throws: `FilterError.StreamInitError` if stream initialization failed
126
120
*/
127
- public init ( _ operation: FilterOperation ,
128
- using algorithm : Algorithm ,
129
- bufferCapacity : Int = 65536 ,
130
- writingTo writeFunc: @escaping ( Data ? ) throws -> ( ) ) throws
131
- {
121
+ public init (
122
+ _ operation: FilterOperation ,
123
+ using algorithm: Algorithm ,
124
+ bufferCapacity: Int = 65536 ,
125
+ writingTo writeFunc: @escaping ( Data ? ) throws -> ( )
126
+ ) throws {
132
127
_stream = try compression_stream ( operation: operation, algorithm: algorithm)
133
128
_buf = UnsafeMutablePointer< UInt8> . allocate( capacity: bufferCapacity)
134
129
_bufCapacity = bufferCapacity
@@ -145,16 +140,15 @@ public class OutputFilter {
145
140
- Parameter data: data to process
146
141
147
142
- Throws:
148
- `FilterError.FilterProcessError ` if an error occurs during processing
149
- `FilterError.WriteToFinalizedFilter ` if `data` is not empty/nil, and the filter is the finalized state
143
+ `FilterError.filterProcessError ` if an error occurs during processing
144
+ `FilterError.writeToFinalizedFilter ` if `data` is not empty/nil, and the filter is the finalized state
150
145
*/
151
- public func write( _ data: Data ? ) throws
152
- {
146
+ public func write( _ data: Data ? ) throws {
153
147
// Finalize if data is empty/nil
154
148
if data == nil || data!. isEmpty { try finalize ( ) ; return }
155
149
156
150
// Fail if already finalized
157
- if _finalized { throw FilterError . WriteToFinalizedFilter }
151
+ if _finalized { throw FilterError . writeToFinalizedFilter }
158
152
159
153
// Process all incoming data
160
154
try data!. withUnsafeBytes { ( src_ptr: UnsafePointer < UInt8 > ) in
@@ -173,8 +167,7 @@ public class OutputFilter {
173
167
174
168
- Throws: `FilterError.StreamProcessError` if an error occurs during processing
175
169
*/
176
- public func finalize( ) throws
177
- {
170
+ public func finalize( ) throws {
178
171
// Do nothing if already finalized
179
172
if _finalized { return }
180
173
@@ -191,8 +184,7 @@ public class OutputFilter {
191
184
}
192
185
193
186
// Cleanup resources. The filter is finalized now if it was not finalized yet.
194
- deinit
195
- {
187
+ deinit {
196
188
// Finalize now if not done earlier
197
189
try ? finalize ( )
198
190
@@ -203,21 +195,19 @@ public class OutputFilter {
203
195
204
196
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
205
197
// Return status
206
- private func process( _ finalize: Bool ) throws -> compression_status
207
- {
198
+ private func process( _ finalize: Bool ) throws -> compression_status {
208
199
// Process current input, and write to buf
209
200
_stream. dst_ptr = _buf
210
201
_stream. dst_size = _bufCapacity
211
202
212
203
let status = compression_stream_process ( & _stream, ( finalize ? Int32 ( COMPRESSION_STREAM_FINALIZE . rawValue) : 0 ) )
213
- guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . FilterProcessError }
204
+ guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . filterProcessError }
214
205
215
206
// Number of bytes written to buf
216
207
let writtenBytes = _bufCapacity - _stream. dst_size
217
208
218
209
// Write output
219
- if writtenBytes > 0
220
- {
210
+ if writtenBytes > 0 {
221
211
let outData = Data ( bytesNoCopy: _buf, count: writtenBytes, deallocator: . none)
222
212
try _writeFunc ( outData)
223
213
}
@@ -229,13 +219,12 @@ public class OutputFilter {
229
219
230
220
@available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
231
221
public class InputFilter {
232
-
233
- private var _stream : compression_stream
234
- private var _buf : Data ? = nil // current input data
235
- private var _bufCapacity : Int // size to read when refilling _buf
236
- private var _readFunc : ( Int ) throws -> Data ?
237
- private var _eofReached : Bool = false // did we read end-of-file from the input?
238
- private var _endReached : Bool = false // did we reach end-of-file from the decoder stream?
222
+ private var _stream : compression_stream
223
+ private var _buf : Data ? = nil // current input data
224
+ private let _bufCapacity : Int // size to read when refilling _buf
225
+ private let _readFunc : ( Int ) throws -> Data ?
226
+ private var _eofReached : Bool = false // did we read end-of-file from the input?
227
+ private var _endReached : Bool = false // did we reach end-of-file from the decoder stream?
239
228
240
229
/**
241
230
Initialize an input filter
@@ -246,13 +235,14 @@ public class InputFilter {
246
235
- bufferCapacity: capacity of the internal data buffer
247
236
- readFunc: called to read the input data
248
237
249
- - Throws: `FilterError.FilterInitError ` if filter initialization failed
238
+ - Throws: `FilterError.filterInitError ` if filter initialization failed
250
239
*/
251
- public init ( _ operation: FilterOperation ,
252
- using algorithm : Algorithm ,
253
- bufferCapacity : Int = 65536 ,
254
- readingFrom readFunc: @escaping ( Int ) throws -> Data ? ) throws
255
- {
240
+ public init (
241
+ _ operation: FilterOperation ,
242
+ using algorithm: Algorithm ,
243
+ bufferCapacity: Int = 65536 ,
244
+ readingFrom readFunc: @escaping ( Int ) throws -> Data ?
245
+ ) throws {
256
246
_stream = try compression_stream ( operation: operation, algorithm: algorithm)
257
247
_bufCapacity = bufferCapacity
258
248
_readFunc = readFunc
@@ -271,19 +261,18 @@ public class InputFilter {
271
261
- Returns: a new Data object containing at most `count` output bytes, or nil if no more data is available
272
262
273
263
- Throws:
274
- `FilterError.FilterProcessError ` if an error occurs during processing
275
- `FilterError.ReadCountInvalid ` if `count` <= 0
264
+ `FilterError.filterProcessError ` if an error occurs during processing
265
+ `FilterError.readCountInvalid ` if `count` <= 0
276
266
*/
277
- public func readData( ofLength count: Int ) throws -> Data ?
278
- {
267
+ public func readData( ofLength count: Int ) throws -> Data ? {
279
268
// Sanity check
280
- guard count > 0 else { throw FilterError . ReadCountInvalid }
269
+ guard count > 0 else { throw FilterError . readCountInvalid }
281
270
282
271
// End reached, return early, nothing to do
283
272
if _endReached { return nil }
284
273
285
274
// Allocate result
286
- var result = Data . init ( count: count)
275
+ var result = Data ( count: count)
287
276
288
277
try result. withUnsafeMutableBytes { ( dst_ptr: UnsafeMutablePointer < UInt8 > ) in
289
278
@@ -295,30 +284,27 @@ public class InputFilter {
295
284
296
285
// Refill _buf if needed, and EOF was not yet read
297
286
if _stream. src_size == 0 && !_eofReached {
298
- try _buf = _readFunc ( _bufCapacity) // may be nil
287
+ _buf = try _readFunc ( _bufCapacity) // may be nil
299
288
// Reset src_size to full _buf size
300
- if _buf == nil || _buf! . count == 0 { _eofReached = true ; _stream . src_size = 0 }
301
- else { _stream. src_size = _buf! . count }
289
+ if _buf? . count ?? 0 == 0 { _eofReached = true }
290
+ _stream. src_size = _buf? . count ?? 0
302
291
}
303
292
304
293
// Process some data
305
- if _buf != nil {
306
-
307
- let bufCount = _buf!. count
308
- try _buf!. withUnsafeBytes { ( src_ptr: UnsafePointer < UInt8 > ) in
294
+ if let buf = _buf {
295
+ try buf. withUnsafeBytes { ( src_ptr: UnsafePointer < UInt8 > ) in
309
296
310
297
// Next byte to read
311
- _stream. src_ptr = src_ptr. advanced ( by: bufCount - _stream. src_size)
298
+ _stream. src_ptr = src_ptr. advanced ( by: buf . count - _stream. src_size)
312
299
313
300
let status = compression_stream_process ( & _stream, ( _eofReached ? Int32 ( COMPRESSION_STREAM_FINALIZE . rawValue) : 0 ) )
314
- guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . FilterProcessError }
301
+ guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . filterProcessError }
315
302
if status == COMPRESSION_STATUS_END { _endReached = true }
316
303
}
317
-
318
- } else {
319
-
304
+ }
305
+ else {
320
306
let status = compression_stream_process ( & _stream, ( _eofReached ? Int32 ( COMPRESSION_STREAM_FINALIZE . rawValue) : 0 ) )
321
- guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . FilterProcessError }
307
+ guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . filterProcessError }
322
308
if status == COMPRESSION_STATUS_END { _endReached = true }
323
309
324
310
}
@@ -332,8 +318,7 @@ public class InputFilter {
332
318
}
333
319
334
320
// Cleanup resources
335
- deinit
336
- {
321
+ deinit {
337
322
compression_stream_destroy ( & _stream)
338
323
}
339
324
0 commit comments