]> jaekl.net Git - cfb.git/commitdiff
Command: make reliance on system default encoding for reading streams explicit.
authorChris Jaekl <cejaekl@yahoo.com>
Sun, 1 Nov 2015 02:11:46 +0000 (11:11 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sun, 1 Nov 2015 02:11:46 +0000 (11:11 +0900)
prod/net/jaekl/cfb/util/Command.java

index 672aac2fec9364a106922d40b14286536a4c3eff..be3b07e534c5c6f04ac7cd13e4d94c9b3ca945dc 100644 (file)
@@ -6,8 +6,14 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
 
 public class Command {
+       public static final String UTF_8 = "UTF-8";
+       private Charset m_charset;
+       
        public static class Result
        {
                private int m_retCode;
@@ -25,14 +31,14 @@ public class Command {
                public String getStderr() { return m_stderr; }
        }
        
-       private static class StreamGobbler extends Thread {
+       private class StreamGobbler extends Thread {
                
                private StringBuilder m_sb;
                private BufferedReader m_br;
                
                public StreamGobbler(InputStream is) {
                        m_sb = new StringBuilder();
-                       m_br = new BufferedReader(new InputStreamReader(is));
+                       m_br = new BufferedReader(new InputStreamReader(is, m_charset));
                }
                
                @Override
@@ -61,7 +67,12 @@ public class Command {
        
        public Command() 
        {
-               // no-op
+               try {
+                       m_charset = Charset.forName(System.getProperty("file.encoding", UTF_8));
+               }
+               catch (IllegalCharsetNameException | UnsupportedCharsetException exc) {
+                       m_charset = Charset.forName(UTF_8);
+               }
        }
        
        public Result exec(String cmd) throws IOException