aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-06-05 21:42:47 +0000
committerStuart Sierra <mail@stuartsierra.com>2009-06-05 21:42:47 +0000
commitd31bf853f7dadf4e9fe19af805d8f7c1e0a32557 (patch)
tree262a5d11e2f3cf145e20670d8e47d33a7db05c79
parente0463965b50f27e2fb806526f4f581bb6b0f1a3a (diff)
duck_streams.clj: added methods to copy for String and byte[]
-rw-r--r--src/clojure/contrib/duck_streams.clj30
1 files 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."