[ROCKETMQ-206] Fix bug when non-1byte character exists in JSON config files.
Project: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/commit/ceeef8ec Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/tree/ceeef8ec Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/diff/ceeef8ec Branch: refs/heads/release-4.1.0-incubating Commit: ceeef8ec25758eea490b39400328dd8a702b0175 Parents: 1d966b5 Author: yukon <yu...@apache.org> Authored: Thu May 25 13:48:22 2017 +0800 Committer: yukon <yu...@apache.org> Committed: Thu May 25 13:48:22 2017 +0800 ---------------------------------------------------------------------- .../java/org/apache/rocketmq/common/MixAll.java | 26 ++++++++------------ .../org/apache/rocketmq/common/MixAllTest.java | 20 +++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/ceeef8ec/common/src/main/java/org/apache/rocketmq/common/MixAll.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/rocketmq/common/MixAll.java b/common/src/main/java/org/apache/rocketmq/common/MixAll.java index e75efd9..36d81d0 100644 --- a/common/src/main/java/org/apache/rocketmq/common/MixAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/MixAll.java @@ -18,7 +18,7 @@ package org.apache.rocketmq.common; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; @@ -187,30 +187,24 @@ public class MixAll { } } - public static String file2String(final String fileName) { + public static String file2String(final String fileName) throws IOException { File file = new File(fileName); return file2String(file); } - public static String file2String(final File file) { + public static String file2String(final File file) throws IOException { if (file.exists()) { - char[] data = new char[(int) file.length()]; - boolean result = false; + byte[] data = new byte[(int) file.length()]; + boolean result; - FileReader fileReader = null; + FileInputStream inputStream = null; try { - fileReader = new FileReader(file); - int len = fileReader.read(data); + inputStream = new FileInputStream(file); + int len = inputStream.read(data); result = len == data.length; - } catch (IOException e) { - // e.printStackTrace(); } finally { - if (fileReader != null) { - try { - fileReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } + if (inputStream != null) { + inputStream.close(); } } http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/ceeef8ec/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java ---------------------------------------------------------------------- diff --git a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java index 8220981..218b36d 100644 --- a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java @@ -21,6 +21,10 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; +import java.nio.ByteOrder; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import org.junit.Test; @@ -68,6 +72,22 @@ public class MixAllTest { } @Test + public void testFile2String_WithChinese() throws IOException { + String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis(); + File file = new File(fileName); + if (file.exists()) { + file.delete(); + } + file.createNewFile(); + PrintWriter out = new PrintWriter(fileName); + out.write("TestForMixAll_ä¸æ"); + out.close(); + String string = MixAll.file2String(fileName); + assertThat(string).isEqualTo("TestForMixAll_ä¸æ"); + file.delete(); + } + + @Test public void testString2File() throws IOException { String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis(); MixAll.string2File("MixAll_testString2File", fileName);