('use [clojure.java.io :only [input-stream]])
=> (with-out-str (ofn var))
"it works!"
Now when I use ring:
{:body (input-stream (ofn var))}
java.lang.IllegalArgumentException: No implementation of method: :make-
input-stream of protocol: #'clojure.java.io/IOFactory found for class:
nil
Is there something special I need to do to convert output stream into
an input stream ?
Thanks,
Trevor
You can probably consider PipedOutputStream and PipedInputStream:
http://docs.oracle.com/javase/6/docs/api/java/io/PipedOutputStream.html
http://docs.oracle.com/javase/6/docs/api/java/io/PipedInputStream.html
A Java example is here (section "Using Piped Streams"):
http://www.cs.princeton.edu/courses/archive/spr96/cs333/java/tutorial/java/io/streampairs.html
Regards,
Shantanu
I'm turning an input stream into another input stream, but you can do
something similar. It's a bit weird, because you're really *not*
attempting to convert an outputstream to an inputstream in your
example. You're calling a function that writes to stdout and returns
nil, and hoping that somehow a stream gets created. Really using with-
out-str is a reasonable approach here; just create a StringReader from
the resulting string.
But if you need asynchronicity, you can probably do something like
(let [pipe-in (PipedInputStream.)
pipe-out (PipedOutputStream. pipe-in)]
(future ; new thread to prevent blocking deadlock
(binding [*out* (PrintWriter. pipe-out)]
(ofn var)))
pipe-in)
This lets ofn run in another thread, writing its output to *out*,
which passes through the pipe and becomes an input stream for ring to
use.
(let [pipe-in (PipedInputStream.)]
(future ; new thread to prevent blocking deadlock
(with-open [out (-> pipe-in (PipedOutputStream.) (PrintWriter.))]
(binding [*out* out]
(do-whatever))))
pipe-in)
On Dec 16, 1:49 am, Alan Malloy <a...@malloys.org> wrote:
> You can't really do this in a single thread without risking blocking.
> But with another thread, it's fairly simple. For example, I do this in
> my gzip-middleware, copying an InputStream through a pipe with GZIP
> wrapping:https://github.com/amalloy/ring-gzip-middleware/blob/master/src/ring/...