From d31bf853f7dadf4e9fe19af805d8f7c1e0a32557 Mon Sep 17 00:00:00 2001 From: Stuart Sierra Date: Fri, 5 Jun 2009 21:42:47 +0000 Subject: duck_streams.clj: added methods to copy for String and byte[] --- src/clojure/contrib/duck_streams.clj | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/clojure/contrib/duck_streams.clj b/src/clojure/contrib/duck_streams.clj index 60cbe9ac..fe0cc5fc 100644 --- a/src/clojure/contrib/duck_streams.clj +++ b/src/clojure/contrib/duck_streams.clj @@ -62,7 +62,8 @@ (java.io Reader InputStream InputStreamReader PushbackReader BufferedReader File PrintWriter OutputStream OutputStreamWriter BufferedWriter Writer - FileInputStream FileOutputStream ByteArrayOutputStream) + FileInputStream FileOutputStream ByteArrayOutputStream + StringReader ByteArrayInputStream) (java.net URI URL MalformedURLException))) @@ -76,6 +77,11 @@ copying streams."} *buffer-size* 1024) +(def + #^{:doc "Type object for a Java primitive byte array."} + *byte-array-type* (class (make-array Byte/TYPE 0))) + + (defn #^File file-str "Concatenates args as strings and returns a java.io.File. Replaces all / and \\ with File/separatorChar. Replaces ~ at the start of @@ -284,7 +290,7 @@ (defmulti #^{:doc "Copies input to output. Returns nil. - Input may be an InputStream, Reader, or File. + Input may be an InputStream, Reader, File, byte[], or String. Output may be an OutputStream, Writer, or File. Does not close any streams except those it opens itself @@ -350,10 +356,24 @@ out (FileOutputStream. output)] (copy in out))) +(defmethod copy [String OutputStream] [input output] + (copy (StringReader. input) output)) + +(defmethod copy [String Writer] [input output] + (copy (StringReader. input) output)) + +(defmethod copy [String File] [input output] + (copy (StringReader. input) output)) + +(defmethod copy [*byte-array-type* OutputStream] [input output] + (copy (ByteArrayInputStream. input) output)) + +(defmethod copy [*byte-array-type* Writer] [input output] + (copy (ByteArrayInputStream. input) output)) + +(defmethod copy [*byte-array-type* File] [input output] + (copy (ByteArrayInputStream. input) output)) -(def - #^{:doc "Type object for a Java primitive byte array."} - *byte-array-type* (class (make-array Byte/TYPE 0))) (defn make-parents "Creates all parent directories of file." -- cgit v1.2.3-18-g5258