Skip to content

Commit

Permalink
mv decompress to io ns
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Oct 15, 2023
1 parent ccbe4cc commit 3a24d92
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
[dev.onionpancakes.hop.headers :as headers]
[dev.onionpancakes.hop.request :as request]
[dev.onionpancakes.hop.response :as response]
[dev.onionpancakes.hop.io :as io]
[dev.onionpancakes.hop.util :as util]
[dev.onionpancakes.hop.test-client :as test-client]
[dev.onionpancakes.hop.test-headers :as test-headers]
[dev.onionpancakes.hop.test-request :as test-request]
[dev.onionpancakes.hop.test-response :as test-response]))
[dev.onionpancakes.hop.test-response :as test-response]
[dev.onionpancakes.hop.test-io :as test-io]))


25 changes: 25 additions & 0 deletions src/dev/onionpancakes/hop/io.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns dev.onionpancakes.hop.io
(:require [clojure.string :refer [lower-case]])
(:import [java.io ByteArrayInputStream]
[java.util.zip GZIPInputStream]))

;; Decompress

(defmulti ^java.io.InputStream decompress
(fn [data encoding] [(class data) (some-> encoding (lower-case))]))

(defmethod decompress [(Class/forName "[B") "gzip"]
[data _]
(GZIPInputStream. (ByteArrayInputStream. data)))

(defmethod decompress [(Class/forName "[B") nil]
[data _]
(ByteArrayInputStream. data))

(defmethod decompress [java.io.InputStream "gzip"]
[data _]
(GZIPInputStream. data))

(defmethod decompress [java.io.InputStream nil]
[data _]
data)
21 changes: 21 additions & 0 deletions test/dev/onionpancakes/hop/test_io.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns dev.onionpancakes.hop.test-io
(:require [clojure.test :refer [deftest is are]]
[dev.onionpancakes.hop.io :as io])
(:import [java.io ByteArrayInputStream ByteArrayOutputStream]
[java.util.zip GZIPOutputStream]))

(defn gzip-bytes
[^bytes data]
(let [out (ByteArrayOutputStream.)
_ (doto (GZIPOutputStream. out)
(.write data)
(.close))]
(.toByteArray out)))

(deftest test-decompress
(are [data enc expected] (with-open [i (io/decompress data enc)]
(= (slurp i) expected))
(gzip-bytes (.getBytes "foo")) "gzip" "foo"
(.getBytes "foo") nil "foo"
(ByteArrayInputStream. (gzip-bytes (.getBytes "foo"))) "gzip" "foo"
(ByteArrayInputStream. (.getBytes "foo")) nil "foo"))

0 comments on commit 3a24d92

Please sign in to comment.