Skip to content

Commit

Permalink
Merge branch 'ROCKETMQ-206' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dongeforever committed May 26, 2017
2 parents 37fbb7b + ceeef8e commit e5d01b4
Show file tree
Hide file tree
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e5d01b4

Please sign in to comment.