提交 ceeef8ec 编写于 作者: Y yukon

[ROCKETMQ-206] Fix bug when non-1byte character exists in JSON config files.

上级 1d966b50
...@@ -18,7 +18,7 @@ package org.apache.rocketmq.common; ...@@ -18,7 +18,7 @@ package org.apache.rocketmq.common;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -187,30 +187,24 @@ public class MixAll { ...@@ -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); File file = new File(fileName);
return file2String(file); return file2String(file);
} }
public static String file2String(final File file) { public static String file2String(final File file) throws IOException {
if (file.exists()) { if (file.exists()) {
char[] data = new char[(int) file.length()]; byte[] data = new byte[(int) file.length()];
boolean result = false; boolean result;
FileReader fileReader = null; FileInputStream inputStream = null;
try { try {
fileReader = new FileReader(file); inputStream = new FileInputStream(file);
int len = fileReader.read(data); int len = inputStream.read(data);
result = len == data.length; result = len == data.length;
} catch (IOException e) {
// e.printStackTrace();
} finally { } finally {
if (fileReader != null) { if (inputStream != null) {
try { inputStream.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
......
...@@ -21,6 +21,10 @@ import java.io.File; ...@@ -21,6 +21,10 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress; 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.List;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test; import org.junit.Test;
...@@ -67,6 +71,22 @@ public class MixAllTest { ...@@ -67,6 +71,22 @@ public class MixAllTest {
file.delete(); file.delete();
} }
@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 @Test
public void testString2File() throws IOException { public void testString2File() throws IOException {
String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis(); String fileName = System.getProperty("java.io.tmpdir") + File.separator + "MixAllTest" + System.currentTimeMillis();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册