Skip to content

Instantly share code, notes, and snippets.

@slipset
Created October 20, 2017 21:33
Show Gist options
  • Select an option

  • Save slipset/c671e0116e784c1ce182300564da2530 to your computer and use it in GitHub Desktop.

Select an option

Save slipset/c671e0116e784c1ce182300564da2530 to your computer and use it in GitHub Desktop.
(ns foo.core
(:require [clojure.java.io :as io])
(:import [java.net URL]
[java.io FileInputStream]
(javax.xml.bind DatatypeConverter)))
(defn- open-input-stream [^URL url]
(if-let [user-info (.getUserInfo url)]
(let [uc (.openConnection url)
creds (DatatypeConverter/printBase64Binary (.getBytes user-info))
basic (str "Basic " creds)]
(.setRequestProperty uc "Authorization" basic)
(.getInputStream uc))
(.openStream url)))
(extend URL
io/IOFactory
(assoc io/default-streams-impl
:make-input-stream (fn [^URL x opts]
(io/make-input-stream
(if (= "file" (.getProtocol x))
(FileInputStream. (io/as-file x))
(open-input-stream x)) opts))
:make-output-stream (fn [^URL x opts]
(if (= "file" (.getProtocol x))
(io/make-output-stream (io/as-file x) opts)
(throw (IllegalArgumentException. (str "Can not write to non-file URL <" x ">")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment