Skip to content

Commit e23402c

Browse files
committed
Added test for broken UTF-16 on IBM jdk's to WriterOutputStream, adjusted
testcases to expect UOE git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1721095 13f79535-47bb-0310-9956-ffa450edef68
1 parent c3a8348 commit e23402c

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/main/java/org/apache/commons/io/output/WriterOutputStream.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) {
119119
*/
120120
public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, final int bufferSize,
121121
final boolean writeImmediately) {
122+
checkIbmJdkWithBrokenUTF16( decoder.charset());
122123
this.writer = writer;
123124
this.decoder = decoder;
124125
this.writeImmediately = writeImmediately;
@@ -308,4 +309,32 @@ private void flushOutput() throws IOException {
308309
decoderOut.rewind();
309310
}
310311
}
312+
313+
private static void checkIbmJdkWithBrokenUTF16(Charset charset){
314+
if (!"UTF-16".equals(charset.name())) return;
315+
final String TEST_STRING_2 = "v\u00e9s";
316+
byte[] bytes = TEST_STRING_2.getBytes(charset);
317+
318+
final CharsetDecoder charsetDecoder2 = charset.newDecoder();
319+
ByteBuffer bb2 = ByteBuffer.allocate(16);
320+
CharBuffer cb2 = CharBuffer.allocate(TEST_STRING_2.length());
321+
final int len = bytes.length;
322+
for (int i = 0; i < len; i++) {
323+
bb2.put(bytes[i]);
324+
bb2.flip();
325+
try {
326+
charsetDecoder2.decode(bb2, cb2, i == (len - 1));
327+
} catch ( IllegalArgumentException e){
328+
throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " +
329+
"Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream");
330+
}
331+
bb2.compact();
332+
}
333+
cb2.rewind();
334+
if (!TEST_STRING_2.equals(cb2.toString())){
335+
throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " +
336+
"Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream");
337+
};
338+
339+
}
311340
}

src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Random;
2424

2525
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.fail;
2627

2728
public class WriterOutputStreamTest {
2829
private static final String TEST_STRING = "\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre";
@@ -85,12 +86,24 @@ public void testLargeUTF8WithBufferedWrite() throws IOException {
8586

8687
@Test
8788
public void testUTF16WithSingleByteWrite() throws IOException {
88-
testWithSingleByteWrite(TEST_STRING, "UTF-16");
89+
try {
90+
testWithSingleByteWrite(TEST_STRING, "UTF-16");
91+
} catch (UnsupportedOperationException e){
92+
if (!System.getProperty("java.vendor").contains("IBM")){
93+
fail("This test should only throw UOE on IBM JDKs with broken UTF-16");
94+
}
95+
}
8996
}
9097

9198
@Test
9299
public void testUTF16WithBufferedWrite() throws IOException {
93-
testWithBufferedWrite(TEST_STRING, "UTF-16");
100+
try {
101+
testWithBufferedWrite(TEST_STRING, "UTF-16");
102+
} catch (UnsupportedOperationException e) {
103+
if (!System.getProperty("java.vendor").contains("IBM")) {
104+
fail("This test should only throw UOE on IBM JDKs with broken UTF-16");
105+
}
106+
}
94107
}
95108

96109
@Test

0 commit comments

Comments
 (0)