Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Last active March 7, 2020 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfuenzalida/cb5056bfa0743200f787185d3275026e to your computer and use it in GitHub Desktop.
Save dfuenzalida/cb5056bfa0743200f787185d3275026e to your computer and use it in GitHub Desktop.
InputStream to Base64 stream
;; An example InputStream made of 128 bytes:
(defn bytes-is []
(java.io.ByteArrayInputStream. (into-array Byte/TYPE (range 128))))
(defn b64encode [s]
(String. (.encode (java.util.Base64/getEncoder) (.getBytes s))))
;; Now we read the input stream *in memory* (with slurp) and encode as base64:
(-> (bytes-is) slurp b64encode)
;; => "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJC...n8="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment