Skip to content

Commit

Permalink
Use java.util.Base64 instead of BASE64Encoder
Browse files Browse the repository at this point in the history
For backwards/forwards compatibility between implementations, adds a
trailing newline to the string to match the behaviour of the sun.misc.BASE64Decoder.

This effectively sets the minimum JVM to Java 8, at least to use this
namespace.
  • Loading branch information
danielcompton committed Dec 6, 2018
1 parent 3e5259f commit fdf4463
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[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"]]}
: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")))))

20 changes: 19 additions & 1 deletion test/flatland/useful/compress_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
(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]
(println s)
(= s (unsmash-new (smash s)))))

0 comments on commit fdf4463

Please sign in to comment.