From 43aaa41d62f071b4c9fb4677aa706e0261639960 Mon Sep 17 00:00:00 2001
From: David Powell <djpowell@djpowell.net>
Date: Sat, 5 Jun 2010 16:31:03 +0100
Subject: [PATCH 4/6] added :inenc option, specifying the input character set
 renamed :out input option to :outenc

---
 src/clj/clojure/java/shell.clj |   40 ++++++++++++++++++++++------------------
 1 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/clj/clojure/java/shell.clj b/src/clj/clojure/java/shell.clj
index 58d2432..2c2315a 100644
--- a/src/clj/clojure/java/shell.clj
+++ b/src/clj/clojure/java/shell.clj
@@ -48,7 +48,7 @@ collecting its stdout"}
 
 (defn- parse-args
   [args]
-  (let [default-opts {:out "UTF-8" :dir *sh-dir* :env *sh-env*}
+  (let [default-opts {:outenc "UTF-8" :inenc "UTF-8" :dir *sh-dir* :env *sh-env*}
         [cmd opts] (split-with string? args)]
     [cmd (merge default-opts (apply hash-map opts))]))
 
@@ -77,17 +77,21 @@ collecting its stdout"}
 
   Options are
 
-  :in    may be given followed by a String specifying text to be fed to the 
-         sub-process's stdin.  
-  :out   option may be given followed by :bytes or a String. If a String 
-         is given, it will be used as a character encoding name (for 
-         example \"UTF-8\" or \"ISO-8859-1\") to convert the 
-         sub-process's stdout to a String which is returned.
-         If :bytes is given, the sub-process's stdout will be stored in 
-         a byte array and returned.  Defaults to UTF-8.
-  :env   override the process env with a map (or the underlying Java
-         String[] if you are a masochist).
-  :dir   override the process dir with a String or java.io.File.
+  :in      may be given followed by a String specifying text to be fed to
+           the sub-process's stdin.
+  :inenc   option may be given followed by a String, used as a character
+           encoding name (for example \"UTF-8\" or \"ISO-8859-1\") to
+           convert the input specified by the :in option to the
+           sub-process's stdin.  Defaults to UTF-8.
+  :outenc  option may be given followed by :bytes or a String. If a
+           String is given, it will be used as a character encoding
+           name (for example \"UTF-8\" or \"ISO-8859-1\") to convert
+           the sub-process's stdout to a String which is returned.
+           If :bytes is given, the sub-process's stdout will be stored
+           in a byte array and returned.  Defaults to UTF-8.
+  :env     override the process env with a map (or the underlying Java
+           String[] if you are a masochist).
+  :dir     override the process dir with a String or java.io.File.
 
   You can bind :env or :dir for multiple operations using with-sh-env
   and with-sh-dir.
@@ -104,17 +108,17 @@ collecting its stdout"}
 		    (as-env-string (:env opts))
 		    (as-file (:dir opts)))]
     (if (:in opts)
-      (with-open [osw (OutputStreamWriter. (.getOutputStream proc))]
+      (with-open [osw (OutputStreamWriter. (.getOutputStream proc) (:inenc opts))]
         (.write osw (:in opts)))
       (.close (.getOutputStream proc)))
     (with-open [stdout (.getInputStream proc)
                 stderr (.getErrorStream proc)]
       (let [[out err]
-            (if (= (:out opts) :bytes)
+            (if (= (:outenc opts) :bytes)
               (pmap #(stream-to-bytes %) [stdout stderr])
-              (pmap #(stream-to-string % (:out opts)) [stdout stderr]))
+              (pmap #(stream-to-string % (:outenc opts)) [stdout stderr]))
             exit-code (.waitFor proc)]
-        {:exit exit-code :out out :err err}))))
+        {:exit exit-code :outenc out :err err}))))
 
 (comment
 
@@ -123,8 +127,8 @@ collecting its stdout"}
 (println (sh "sed" "s/[aeiou]/oo/g" :in "hello there\n"))
 (println (sh "cat" :in "x\u25bax\n"))
 (println (sh "echo" "x\u25bax"))
-(println (sh "echo" "x\u25bax" :out "ISO-8859-1")) ; reads 4 single-byte chars
-(println (sh "cat" "myimage.png" :out :bytes)) ; reads binary file into bytes[]
+(println (sh "echo" "x\u25bax" :outenc "ISO-8859-1")) ; reads 4 single-byte chars
+(println (sh "cat" "myimage.png" :outenc :bytes)) ; reads binary file into bytes[]
 (println (sh "cmd" "/c dir 1>&2"))
 
 )
-- 
1.6.5.1.1367.gcd48

