File tree Expand file tree Collapse file tree 3 files changed +40
-4
lines changed
Expand file tree Collapse file tree 3 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 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" ]]}
Original file line number Diff line number Diff line change 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. )]
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+
Original file line number Diff line number Diff line change 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
711g2 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)))))
You can’t perform that action at this time.
0 commit comments