Permalink
Please sign in to comment.
Showing
with
30 additions
and 0 deletions.
22
ring-core/src/ring/util/io.clj
| @@ -0,0 +1,22 @@ | ||
| +(ns ring.util.io | ||
| + "Utility functions for I/O in Ring." | ||
| + (:require [clojure.java.io :as io]) | ||
| + (:import [java.io PipedInputStream PipedOutputStream])) | ||
| + | ||
| +(defn piped-input-stream | ||
| + "Create an input stream from a function that takes an output stream as its | ||
| + argument. The function will be executed in a separate thread. The stream | ||
| + will be automatically closed after the function finishes. | ||
| + | ||
| + e.g. (piped-input-stream | ||
| + (fn [ostream] | ||
| + (spit ostream \"Hello\")))" | ||
| + [func] | ||
| + (let [input (PipedInputStream.) | ||
| + output (PipedOutputStream.)] | ||
| + (.connect input output) | ||
| + (future | ||
| + (try | ||
| + (func output) | ||
| + (finally (.close output)))) | ||
| + input)) |
8
ring-core/test/ring/util/test/io.clj
| @@ -0,0 +1,8 @@ | ||
| +(ns ring.util.test.io | ||
| + (:use clojure.test | ||
| + ring.util.io) | ||
| + (:require [clojure.java.io :as io])) | ||
| + | ||
| +(deftest test-piped-input-stream | ||
| + (let [stream (piped-input-stream #(spit % "Hello World"))] | ||
| + (is (= (slurp stream) "Hello World")))) |
0 comments on commit
965c1d6