File tree 3 files changed +40
-4
lines changed
3 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 7
7
:dependencies [[org.clojure/clojure " 1.9.0" ]
8
8
[org.clojure/tools.macro " 0.1.1" ]
9
9
[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" ]]}
12
13
:1.9 {}
13
14
:1.8 {:dependencies [[org.clojure/clojure " 1.8.0" ]]}
14
15
:1.7 {:dependencies [[org.clojure/clojure " 1.7.0" ]]}
Original file line number Diff line number Diff line change 1
1
(ns flatland.useful.compress
2
2
(:import [java.util.zip DeflaterOutputStream InflaterInputStream]
3
3
[java.io ByteArrayOutputStream ByteArrayInputStream]
4
- [sun.misc BASE64Decoder BASE64Encoder]))
4
+ [sun.misc BASE64Decoder BASE64Encoder]
5
+ [java.util Base64]))
5
6
6
7
(defn smash [^String str]
7
8
(let [out (ByteArrayOutputStream. )]
15
16
(let [bytes (-> (BASE64Decoder. ) (.decodeBuffer str))
16
17
in (ByteArrayInputStream. bytes)]
17
18
(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
+
Original file line number Diff line number Diff line change 1
1
(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]))
3
7
4
8
5
9
(deftest round-trip
6
10
(let [s " f3509ruwqerfwoa reo1u30`1 ewf dfgjdsf sfc saf65sad+ f5df3
7
11
g2 sd35g4szdf sdf4 as89faw76fwfwf210
8
12
" ]
9
13
(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)))))
You can’t perform that action at this time.
0 commit comments