-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccbe4cc
commit 3a24d92
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |