Author: krosenvold Date: Mon Dec 21 06:47:22 2015 New Revision: 1721095 URL: http://svn.apache.org/viewvc?rev=1721095&view=rev Log: Added test for broken UTF-16 on IBM jdk's to WriterOutputStream, adjusted testcases to expect UOE
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/WriterOutputStream.java commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/WriterOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/WriterOutputStream.java?rev=1721095&r1=1721094&r2=1721095&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/WriterOutputStream.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/WriterOutputStream.java Mon Dec 21 06:47:22 2015 @@ -119,6 +119,7 @@ public class WriterOutputStream extends */ public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, final int bufferSize, final boolean writeImmediately) { + checkIbmJdkWithBrokenUTF16( decoder.charset()); this.writer = writer; this.decoder = decoder; this.writeImmediately = writeImmediately; @@ -308,4 +309,32 @@ public class WriterOutputStream extends decoderOut.rewind(); } } + + private static void checkIbmJdkWithBrokenUTF16(Charset charset){ + if (!"UTF-16".equals(charset.name())) return; + final String TEST_STRING_2 = "v\u00e9s"; + byte[] bytes = TEST_STRING_2.getBytes(charset); + + final CharsetDecoder charsetDecoder2 = charset.newDecoder(); + ByteBuffer bb2 = ByteBuffer.allocate(16); + CharBuffer cb2 = CharBuffer.allocate(TEST_STRING_2.length()); + final int len = bytes.length; + for (int i = 0; i < len; i++) { + bb2.put(bytes[i]); + bb2.flip(); + try { + charsetDecoder2.decode(bb2, cb2, i == (len - 1)); + } catch ( IllegalArgumentException e){ + throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " + + "Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); + } + bb2.compact(); + } + cb2.rewind(); + if (!TEST_STRING_2.equals(cb2.toString())){ + throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " + + "Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream"); + }; + + } } Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java?rev=1721095&r1=1721094&r2=1721095&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java (original) +++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/WriterOutputStreamTest.java Mon Dec 21 06:47:22 2015 @@ -23,6 +23,7 @@ import java.io.StringWriter; import java.util.Random; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class WriterOutputStreamTest { private static final String TEST_STRING = "\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre"; @@ -85,12 +86,24 @@ public class WriterOutputStreamTest { @Test public void testUTF16WithSingleByteWrite() throws IOException { - testWithSingleByteWrite(TEST_STRING, "UTF-16"); + try { + testWithSingleByteWrite(TEST_STRING, "UTF-16"); + } catch (UnsupportedOperationException e){ + if (!System.getProperty("java.vendor").contains("IBM")){ + fail("This test should only throw UOE on IBM JDKs with broken UTF-16"); + } + } } @Test public void testUTF16WithBufferedWrite() throws IOException { - testWithBufferedWrite(TEST_STRING, "UTF-16"); + try { + testWithBufferedWrite(TEST_STRING, "UTF-16"); + } catch (UnsupportedOperationException e) { + if (!System.getProperty("java.vendor").contains("IBM")) { + fail("This test should only throw UOE on IBM JDKs with broken UTF-16"); + } + } } @Test