From: Chris Jaekl Date: Sun, 1 Nov 2015 02:11:46 +0000 (+0900) Subject: Command: make reliance on system default encoding for reading streams explicit. X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=c16c63542d7b02bde428b5b16b2b6242cd2010cb;p=cfb.git Command: make reliance on system default encoding for reading streams explicit. --- diff --git a/prod/net/jaekl/cfb/util/Command.java b/prod/net/jaekl/cfb/util/Command.java index 672aac2..be3b07e 100644 --- a/prod/net/jaekl/cfb/util/Command.java +++ b/prod/net/jaekl/cfb/util/Command.java @@ -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