Skip to content

Commit a053fef

Browse files
committed
IO-471 Support for additional encodings in ReversedLinesFileReader
Patch by Leandro Reis, applied with patch adjustments to trunk git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1686739 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2a819f7 commit a053fef

7 files changed

Lines changed: 62 additions & 4 deletions

File tree

src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
121121
// UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
122122
// http://en.wikipedia.org/wiki/UTF-8
123123
byteDecrement = 1;
124-
} else if (charset == Charset.forName("Shift_JIS")) {
125-
// Same as for UTF-8
126-
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
124+
} else if(charset == Charset.forName("Shift_JIS") || // Same as for UTF-8 http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
125+
charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
126+
charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
127+
charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
128+
charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
127129
byteDecrement = 1;
128130
} else if (charset == Charsets.UTF_16BE || charset == Charsets.UTF_16LE) {
129131
// UTF-16 new line sequences are not allowed as second tuple of four byte sequences,
@@ -356,4 +358,4 @@ private int getNewLineMatchByteCount(final byte[] data, final int i) {
356358
}
357359
}
358360

359-
}
361+
}

src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public ReversedLinesFileReaderTestParamBlockSize(final Integer testWithBlockSize
5959
private static final String TEST_LINE_SHIFT_JIS1 = "Hiragana letters: \u3041\u3042\u3043\u3044\u3045";
6060
// Kanji letters: 明輸�京
6161
private static final String TEST_LINE_SHIFT_JIS2 = "Kanji letters: \u660E\u8F38\u5B50\u4EAC";
62+
// windows-31j characters
63+
private static final String TEST_LINE_WINDOWS_31J_1 = "\u3041\u3042\u3043\u3044\u3045";
64+
private static final String TEST_LINE_WINDOWS_31J_2 = "\u660E\u8F38\u5B50\u4EAC";
65+
// gbk characters (Simplified Chinese)
66+
private static final String TEST_LINE_GBK_1 = "\u660E\u8F38\u5B50\u4EAC";
67+
private static final String TEST_LINE_GBK_2 = "\u7B80\u4F53\u4E2D\u6587";
68+
// x-windows-949 characters (Korean)
69+
private static final String TEST_LINE_X_WINDOWS_949_1 = "\uD55C\uAD6D\uC5B4";
70+
private static final String TEST_LINE_X_WINDOWS_949_2 = "\uB300\uD55C\uBBFC\uAD6D";
71+
// x-windows-950 characters (Traditional Chinese)
72+
private static final String TEST_LINE_X_WINDOWS_950_1 = "\u660E\u8F38\u5B50\u4EAC";
73+
private static final String TEST_LINE_X_WINDOWS_950_2 = "\u7E41\u9AD4\u4E2D\u6587";
6274

6375

6476
@After
@@ -127,6 +139,38 @@ public void testShiftJISFile() throws URISyntaxException, IOException {
127139
assertEqualsAndNoLineBreaks(TEST_LINE_SHIFT_JIS1, reversedLinesFileReader.readLine());
128140
}
129141

142+
@Test
143+
public void testWindows31jFile() throws URISyntaxException, IOException {
144+
final File testFileWindows31J = new File(this.getClass().getResource("/test-file-windows-31j.bin").toURI());
145+
reversedLinesFileReader = new ReversedLinesFileReader(testFileWindows31J, testParamBlockSize, "windows-31j");
146+
assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_2, reversedLinesFileReader.readLine());
147+
assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_1, reversedLinesFileReader.readLine());
148+
}
149+
150+
@Test
151+
public void testGBK() throws URISyntaxException, IOException {
152+
final File testFileGBK = new File(this.getClass().getResource("/test-file-gbk.bin").toURI());
153+
reversedLinesFileReader = new ReversedLinesFileReader(testFileGBK, testParamBlockSize, "GBK");
154+
assertEqualsAndNoLineBreaks(TEST_LINE_GBK_2, reversedLinesFileReader.readLine());
155+
assertEqualsAndNoLineBreaks(TEST_LINE_GBK_1, reversedLinesFileReader.readLine());
156+
}
157+
158+
@Test
159+
public void testxWindows949File() throws URISyntaxException, IOException {
160+
final File testFilexWindows949 = new File(this.getClass().getResource("/test-file-x-windows-949.bin").toURI());
161+
reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows949, testParamBlockSize, "x-windows-949");
162+
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_2, reversedLinesFileReader.readLine());
163+
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_1, reversedLinesFileReader.readLine());
164+
}
165+
166+
@Test
167+
public void testxWindows950File() throws URISyntaxException, IOException {
168+
final File testFilexWindows950 = new File(this.getClass().getResource("/test-file-x-windows-950.bin").toURI());
169+
reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows950, testParamBlockSize, "x-windows-950");
170+
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_2, reversedLinesFileReader.readLine());
171+
assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_1, reversedLinesFileReader.readLine());
172+
}
173+
130174
@Test // this test is run 3x for same block size as we want to test with 10
131175
public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException {
132176
final int blockSize = 10;

src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamFile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public static Collection<Object[]> blockSizes() {
5757
{"test-file-utf8-win-linebr.bin", "UTF-8", 3},
5858
{"test-file-utf8-win-linebr.bin", "UTF-8", 4},
5959
{"test-file-utf8.bin", "UTF-8", null},
60+
{"test-file-windows-31j.bin", "windows-31j", null},
61+
{"test-file-gbk.bin", "gbk", null},
62+
{"test-file-x-windows-949.bin", "x-windows-949", null},
63+
{"test-file-x-windows-950.bin", "x-windows-950", null},
6064
});
6165
}
6266

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
��ݔ�Ӿ�
2+
��������
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
����������
2+
���A�q��
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
�ѱ���
2+
���ѹα�
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
����l��
2+
�c�餤��

0 commit comments

Comments
 (0)