Skip to content

Commit 198e47a

Browse files
committed
Use java.util.Base64 instead of BASE64Encoder
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.
1 parent 3e5259f commit 198e47a

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

project.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
:dependencies [[org.clojure/clojure "1.9.0"]
88
[org.clojure/tools.macro "0.1.1"]
99
[org.clojure/tools.reader "0.7.2"]]
10-
:aliases {"testall" ["with-profile" "1.6:1.7:1.8:1.9:1.10" "test"]}
11-
:profiles {:1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]}
10+
:aliases {"testall" ["with-profile" "+1.6:+1.7:+1.8:+1.9:+1.10" "test"]}
11+
:profiles {:dev {:dependencies [[org.clojure/test.check "0.10.0-alpha3"]]}
12+
:1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]}
1213
:1.9 {}
1314
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
1415
:1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}

src/flatland/useful/compress.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns flatland.useful.compress
22
(:import [java.util.zip DeflaterOutputStream InflaterInputStream]
33
[java.io ByteArrayOutputStream ByteArrayInputStream]
4-
[sun.misc BASE64Decoder BASE64Encoder]))
4+
[sun.misc BASE64Decoder BASE64Encoder]
5+
[java.util Base64]))
56

67
(defn smash [^String str]
78
(let [out (ByteArrayOutputStream.)]
@@ -15,3 +16,20 @@
1516
(let [bytes (-> (BASE64Decoder.) (.decodeBuffer str))
1617
in (ByteArrayInputStream. bytes)]
1718
(slurp (InflaterInputStream. in))))
19+
20+
21+
22+
23+
(defn unsmash-new [^String s]
24+
(let [bytes (.decode (Base64/getDecoder) (subs s 0 (dec (count s))))
25+
in (ByteArrayInputStream. bytes)]
26+
(slurp (InflaterInputStream. in))))
27+
28+
(defn smash-new [^String s]
29+
(let [out (ByteArrayOutputStream.)]
30+
(doto (DeflaterOutputStream. out)
31+
(.write (.getBytes s))
32+
(.finish))
33+
(-> (.encodeToString (Base64/getEncoder) (.toByteArray out))
34+
(str (System/getProperty "line.separator")))))
35+
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
(ns flatland.useful.compress-test
2-
(:use clojure.test flatland.useful.compress))
2+
(:use clojure.test
3+
flatland.useful.compress)
4+
(:require [clojure.test.check.clojure-test :refer [defspec]]
5+
[clojure.test.check.generators :as gen]
6+
[clojure.test.check.properties :as prop]))
37

48

59
(deftest round-trip
610
(let [s "f3509ruwqerfwoa reo1u30`1 ewf dfgjdsf sfc saf65sad+ f5df3
711
g2 sd35g4szdf sdf4 as89faw76fwfwf210
812
"]
913
(is (= s (unsmash (smash s))))))
14+
15+
(deftest smash-test
16+
(is (= "eJwDAAAAAAE=\n" (smash-new "")))
17+
(is (= "eJxLBAAAYgBi\n" (smash-new "a"))))
18+
19+
(deftest unsmash-test
20+
(is (= "" (unsmash-new "eJwDAAAAAAE=\n")))
21+
(is (= "a" (unsmash-new "eJxLBAAAYgBi\n"))))
22+
23+
(defspec round-trip-gen
24+
100
25+
(prop/for-all [s gen/string]
26+
(= s (unsmash-new (smash s)))))

0 commit comments

Comments
 (0)