-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.clj
95 lines (79 loc) · 2.84 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(ns cljam.util
"General utilities."
(:require [clojure.java.io :refer [file] :as cio])
(:import [java.net MalformedURLException URL]
[org.apache.commons.compress.compressors
CompressorStreamFactory CompressorException]))
;; Disk cache
;; ----------
(def temp-dir (let [system-tmp-dir-path (or (System/getenv "TMPDIR")
(System/getProperty "java.io.tmpdir"))
dir-path (.getPath (file system-tmp-dir-path "cljam"))]
(.mkdirs (file dir-path))
dir-path))
;; byte array
;; ----------
(defn ubyte
"Casts to byte avoiding an error about out of range for byte."
[n]
{:pre [(<= 0 n 255)]}
(byte (if (< n 0x80) n (- n 0x100))))
;; string utils
;; ------------
(defn ^"[B" string->bytes [^String s]
(.getBytes s))
(defn ^String bytes->string [^bytes b]
(String. b 0 (alength b)))
(defn graph?
"Returns true if c is a visible character, false if not."
[c]
(<= 0x20 (byte c) 0x7E))
(defn space?
"Returns true if c is a character that creates \"white space\" in displayed
text."
[c]
(not (nil? (#{\space \tab \newline \formfeed \return (char 0x0b)} c))))
;; file utils
;; ---------
(defn ^URL as-url
[x]
(try
(cio/as-url x)
(catch MalformedURLException _
(cio/as-url (cio/file x)))))
(defn basename
[x]
(when-let [url (as-url x)]
(second (re-find #"([^/]+)\.(?=[^\./]+$)" (.getPath url)))))
(def ^:private compressor-map
{:gzip CompressorStreamFactory/GZIP
:bzip2 CompressorStreamFactory/BZIP2})
(defn ^java.io.InputStream compressor-input-stream
"Returns an compressor input stream from f, autodetecting the compressor type
from the first few bytes of f. Returns java.io.BufferedInputStream if the
compressor type is not known. Should be used inside with-open to ensure the
InputStream is properly closed."
[f]
(let [is (cio/input-stream f)]
(try
(-> (CompressorStreamFactory. true)
(.createCompressorInputStream is))
(catch CompressorException _
is))))
(defn ^java.io.OutputStream compressor-output-stream
"Returns an compressor output stream from f and a compressor type k. k must be
selected from :gzip or :bzip2. Autodetects the compressor type from the
extension of f if k is not passed. Returns java.io.BufferedOutputStream if the
compressor type is not known. Should be used inside with-open to ensure the
OutputStream is properly closed."
([f]
(compressor-output-stream f (condp re-find (.getPath (as-url f))
#"(?i)\.(gz|gzip)$" :gzip
#"(?i)\.(bz2|bzip2)$" :bzip2
nil)))
([f k]
(let [os (cio/output-stream f)]
(if-let [s (get compressor-map k)]
(-> (CompressorStreamFactory.)
(.createCompressorOutputStream s os))
os))))