   (let [buf-len (alength buf)]
     (loop []
       (let [compressed-bytes (.deflate deflater buf 0 buf-len)]
-        (if-not (= 0 compressed-bytes)
+        (when-not (= 0 compressed-bytes)
           (let [channel-buffer (ChannelBuffers/copiedBuffer buf 0 compressed-bytes)]
             (downstream :body channel-buffer)
             (recur)))))))
 
-(defn- gzip-and-send
-  [state crc deflater downstream compressed-byte-buffer chunk]
-  (let [buffer-length (alength compressed-byte-buffer)
+(defn- gzip-and-send-chunk
+  [state downstream chunk deflater crc buf]
+  (let [buffer-length (alength buf)
         sent-header (:sent-header @state)]
     ;; update the crc and set the input
     (if (nil? chunk)

      (downstream :body (ChannelBuffers/wrappedBuffer GzipUtils/header)))

    ;; send the chunks
    (send-chunks deflater downstream compressed-byte-buffer)

    ;; send the trailer if needed
    (when (nil? chunk)


(defn- mk-trailer
  [deflater crc]
  (let [buf (byte-array trailer-length)]
    (GzipUtils/writeTrailer deflater crc buf 0)
    buf))

(defn- gzip-entire-body
  [body]
  (let [deflater (Deflater. Deflater/DEFAULT_COMPRESSION true)
        crc (CRC32.)
        baos (ByteArrayOutputStream.)
        buf (byte-array 4092)
        bytes (body->byte-array body)]
    ;; write header
