提交 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;
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();
}
}
......
......@@ -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;
......@@ -67,6 +71,22 @@ public class MixAllTest {
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
public void testString2File() throws IOException {
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.
先完成此消息的编辑!
想要评论请 注册