     // Finalize stream
     _stream.src_size = 0
     var status = COMPRESSION_STATUS_OK
-    while (status != COMPRESSION_STATUS_END) { status = try process(true) }
+    while (status != COMPRESSION_STATUS_END) { status = try process(finalizing: true) }
+
+    // Update state
+    _finalized = true
 
     // Notify end of stream
     try _writeFunc(nil)
-
-    // Update state
-    _finalized = true
   }
 
   // Cleanup resources.  The filter is finalized now if it was not finalized yet.

          try buf.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in

            // Next byte to read
            _stream.src_ptr = src_ptr.advanced(by: buf.count - _stream.src_size)

            let status = compression_stream_process(&_stream, (_eofReached ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0))
            guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.filterProcessError }


  // Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
  // Return status
  private func process(finalizing finalize: Bool) throws -> compression_status {
    // Process current input, and write to buf
    _stream.dst_ptr = _buf
    _stream.dst_size = _bufCapacity
