@@ -53,9 +53,6 @@ public enum FilterError: Error {
53
53
54
54
/// Non-empty write after an output filter has been finalized
55
55
case writeToFinalizedFilter
56
-
57
- /// Invalid count argument in a read() call
58
- case readCountInvalid
59
56
}
60
57
61
58
/**
@@ -154,7 +151,7 @@ public class OutputFilter {
154
151
try data!. withUnsafeBytes { ( src_ptr: UnsafePointer < UInt8 > ) in
155
152
_stream. src_size = data!. count
156
153
_stream. src_ptr = src_ptr
157
- while ( _stream. src_size > 0 ) { _ = try process ( false ) }
154
+ while ( _stream. src_size > 0 ) { _ = try process ( finalizing : false ) }
158
155
}
159
156
}
160
157
@@ -174,13 +171,13 @@ public class OutputFilter {
174
171
// Finalize stream
175
172
_stream. src_size = 0
176
173
var status = COMPRESSION_STATUS_OK
177
- while ( status != COMPRESSION_STATUS_END) { status = try process ( true ) }
178
-
179
- // Notify end of stream
180
- try _writeFunc ( nil )
174
+ while ( status != COMPRESSION_STATUS_END) { status = try process ( finalizing: true ) }
181
175
182
176
// Update state
183
177
_finalized = true
178
+
179
+ // Notify end of stream
180
+ try _writeFunc ( nil )
184
181
}
185
182
186
183
// Cleanup resources. The filter is finalized now if it was not finalized yet.
@@ -195,7 +192,7 @@ public class OutputFilter {
195
192
196
193
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
197
194
// Return status
198
- private func process( _ finalize: Bool ) throws -> compression_status {
195
+ private func process( finalizing finalize: Bool ) throws -> compression_status {
199
196
// Process current input, and write to buf
200
197
_stream. dst_ptr = _buf
201
198
_stream. dst_size = _bufCapacity
@@ -262,11 +259,10 @@ public class InputFilter {
262
259
263
260
- Throws:
264
261
`FilterError.filterProcessError` if an error occurs during processing
265
- `FilterError.readCountInvalid` if `count` <= 0
266
262
*/
267
263
public func readData( ofLength count: Int ) throws -> Data ? {
268
264
// Sanity check
269
- guard count > 0 else { throw FilterError . readCountInvalid }
265
+ precondition ( count > 0 , " number of bytes to read can't be 0 " )
270
266
271
267
// End reached, return early, nothing to do
272
268
if _endReached { return nil }
@@ -295,7 +291,7 @@ public class InputFilter {
295
291
try buf. withUnsafeBytes { ( src_ptr: UnsafePointer < UInt8 > ) in
296
292
297
293
// Next byte to read
298
- _stream. src_ptr = src_ptr. advanced ( by : buf. count - _stream. src_size)
294
+ _stream. src_ptr = src_ptr + buf. count - _stream. src_size
299
295
300
296
let status = compression_stream_process ( & _stream, ( _eofReached ? Int32 ( COMPRESSION_STREAM_FINALIZE . rawValue) : 0 ) )
301
297
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError . filterProcessError }
0 commit comments