Created
October 20, 2017 21:33
-
-
Save slipset/c671e0116e784c1ce182300564da2530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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