Skip to content

Commit

Permalink
[ROCKETMQ-206] Fix bug when non-1byte character exists in JSON config…
Browse files Browse the repository at this point in the history
… files.
zhouxinyu authored and dongeforever committed Jun 6, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c8e84ad commit f009ba0
Showing 2 changed files with 30 additions and 16 deletions.
26 changes: 10 additions & 16 deletions common/src/main/java/org/apache/rocketmq/common/MixAll.java
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@

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 static void string2FileNotSafe(final String str, final String fileName) t
}
}

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();
}
}

20 changes: 20 additions & 0 deletions common/src/test/java/org/apache/rocketmq/common/MixAllTest.java
Original file line number Diff line number Diff line change
@@ -21,6 +21,10 @@
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 void testFile2String() throws IOException {
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();

0 comments on commit f009ba0

Please sign in to comment.