|
| 1 | +(ns zlib-tiny.core |
| 2 | + (:import (java.util.zip InflaterInputStream |
| 3 | + GZIPInputStream |
| 4 | + GZIPOutputStream |
| 5 | + DeflaterInputStream |
| 6 | + Inflater |
| 7 | + ZipException |
| 8 | + CRC32) |
| 9 | + (zlib_tiny CRC64) |
| 10 | + (org.apache.commons.io IOUtils) |
| 11 | + (java.security MessageDigest) |
| 12 | + (java.io ByteArrayInputStream |
| 13 | + ByteArrayOutputStream |
| 14 | + BufferedInputStream |
| 15 | + InputStream))) |
| 16 | + |
| 17 | +(defn str->bytes |
| 18 | + "Returns the encoding's bytes corresponding to the given string. If no |
| 19 | + encoding is specified, UTF-8 is used." |
| 20 | + [^String s & [^String encoding]] |
| 21 | + (.getBytes s (or encoding "UTF-8"))) |
| 22 | + |
| 23 | +(defn bytes->str |
| 24 | + "Returns the String corresponding to the given encoding's decoding of the |
| 25 | + given bytes. If no encoding is specified, UTF-8 is used." |
| 26 | + [^bytes b & [^String encoding]] |
| 27 | + (String. b (or encoding "UTF-8"))) |
| 28 | + |
| 29 | +(defn gunzip |
| 30 | + "Returns a gunzip'd version of the given byte array." |
| 31 | + [b] |
| 32 | + (when b |
| 33 | + (cond |
| 34 | + (instance? InputStream b) |
| 35 | + (GZIPInputStream. b) |
| 36 | + :else |
| 37 | + (IOUtils/toByteArray (GZIPInputStream. (ByteArrayInputStream. b)))))) |
| 38 | + |
| 39 | +(defn gzip |
| 40 | + "Returns a gzip'd version of the given byte array." |
| 41 | + [b] |
| 42 | + (when b |
| 43 | + (let [baos (ByteArrayOutputStream.) |
| 44 | + gos (GZIPOutputStream. baos)] |
| 45 | + (IOUtils/copy (ByteArrayInputStream. b) gos) |
| 46 | + (.close gos) |
| 47 | + (.toByteArray baos)))) |
| 48 | + |
| 49 | +(defn force-byte-array |
| 50 | + "force b as byte array if it is an InputStream, also close the stream" |
| 51 | + ^bytes [b] |
| 52 | + (if (instance? InputStream b) |
| 53 | + (try (IOUtils/toByteArray ^InputStream b) |
| 54 | + (finally (.close ^InputStream b))) |
| 55 | + b)) |
| 56 | + |
| 57 | +(defn inflate |
| 58 | + "Returns a zlib inflate'd version of the given byte array or InputStream." |
| 59 | + [b] |
| 60 | + (when b |
| 61 | + ;; This weirdness is because we don't know about what kind of deflation |
| 62 | + ;; sender using, so we try one way, then if that doesn't work, reset and |
| 63 | + ;; try the other way |
| 64 | + (let [stream (BufferedInputStream. (if (instance? InputStream b) |
| 65 | + b |
| 66 | + (ByteArrayInputStream. b))) |
| 67 | + _ (.mark stream 512) |
| 68 | + iis (InflaterInputStream. stream) |
| 69 | + readable? (try (.read iis) true |
| 70 | + (catch ZipException _ false))] |
| 71 | + (.reset stream) |
| 72 | + (if readable? |
| 73 | + (InflaterInputStream. stream) |
| 74 | + (InflaterInputStream. stream (Inflater. true)))))) |
| 75 | + |
| 76 | +(defn deflate |
| 77 | + "Returns a deflate'd version of the given byte array." |
| 78 | + [b] |
| 79 | + (when b |
| 80 | + (IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b))))) |
| 81 | + |
| 82 | +(comment "ZLib Example" |
| 83 | + (bytes->str (force-byte-array (inflate (deflate (str->bytes "test it!")))))) |
| 84 | +(comment "GZip Example" |
| 85 | + (bytes->str (gunzip (gzip (str->bytes "test it!"))))) |
| 86 | + |
| 87 | +(defn crc32 |
| 88 | + ^Long [^bytes b] |
| 89 | + (let [o (CRC32.)] |
| 90 | + (.update o b) |
| 91 | + (.getValue o))) |
| 92 | + |
| 93 | +(defn crc64 |
| 94 | + ^Long [^bytes b] |
| 95 | + (let [o (CRC64.)] |
| 96 | + (.update o b) |
| 97 | + (.getValue o))) |
| 98 | + |
| 99 | +(defn md5 |
| 100 | + ^bytes [^bytes b] |
| 101 | + (let [o (MessageDigest/getInstance "md5")] |
| 102 | + (.update o b) |
| 103 | + (.digest o))) |
| 104 | + |
| 105 | +(defn sha-1 |
| 106 | + ^bytes [^bytes b] |
| 107 | + (let [o (MessageDigest/getInstance "sha1")] |
| 108 | + (.update o b) |
| 109 | + (.digest o))) |
| 110 | + |
| 111 | +(defn sha-256 |
| 112 | + ^bytes [^bytes b] |
| 113 | + (let [o (MessageDigest/getInstance "sha256")] |
| 114 | + (.update o b) |
| 115 | + (.digest o))) |
0 commit comments