Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java.util.Base64 instead of BASE64Encoder #41

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/tools.macro "0.1.1"]
[org.clojure/tools.reader "0.7.2"]]
:aliases {"testall" ["with-profile" "1.6:1.7:1.8:1.9:1.10" "test"]}
:profiles {:1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]}
:aliases {"testall" ["with-profile" "+1.6:+1.7:+1.8:+1.9:+1.10" "test"]}
:profiles {:dev {:dependencies [[org.clojure/test.check "0.10.0-alpha3"]]}
:1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]}
:1.9 {}
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
:1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}
Expand Down
20 changes: 19 additions & 1 deletion src/flatland/useful/compress.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns flatland.useful.compress
(:import [java.util.zip DeflaterOutputStream InflaterInputStream]
[java.io ByteArrayOutputStream ByteArrayInputStream]
[sun.misc BASE64Decoder BASE64Encoder]))
[sun.misc BASE64Decoder BASE64Encoder]
[java.util Base64]))

(defn smash [^String str]
(let [out (ByteArrayOutputStream.)]
Expand All @@ -15,3 +16,20 @@
(let [bytes (-> (BASE64Decoder.) (.decodeBuffer str))
in (ByteArrayInputStream. bytes)]
(slurp (InflaterInputStream. in))))




(defn unsmash-new [^String s]
(let [bytes (.decode (Base64/getDecoder) (subs s 0 (dec (count s))))
in (ByteArrayInputStream. bytes)]
(slurp (InflaterInputStream. in))))

(defn smash-new [^String s]
(let [out (ByteArrayOutputStream.)]
(doto (DeflaterOutputStream. out)
(.write (.getBytes s))
(.finish))
(-> (.encodeToString (Base64/getEncoder) (.toByteArray out))
(str (System/getProperty "line.separator")))))

19 changes: 18 additions & 1 deletion test/flatland/useful/compress_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
(ns flatland.useful.compress-test
(:use clojure.test flatland.useful.compress))
(:use clojure.test
flatland.useful.compress)
(:require [clojure.test.check.clojure-test :refer [defspec]]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]))


(deftest round-trip
(let [s "f3509ruwqerfwoa reo1u30`1 ewf dfgjdsf sfc saf65sad+ f5df3
g2 sd35g4szdf sdf4 as89faw76fwfwf210
"]
(is (= s (unsmash (smash s))))))

(deftest smash-test
(is (= "eJwDAAAAAAE=\n" (smash-new "")))
(is (= "eJxLBAAAYgBi\n" (smash-new "a"))))

(deftest unsmash-test
(is (= "" (unsmash-new "eJwDAAAAAAE=\n")))
(is (= "a" (unsmash-new "eJxLBAAAYgBi\n"))))

(defspec round-trip-gen
100
(prop/for-all [s gen/string]
(= s (unsmash-new (smash s)))))