2121
2222import java .io .IOException ;
2323import java .io .StringWriter ;
24+ import java .nio .charset .Charset ;
25+ import java .nio .charset .CharsetDecoder ;
2426import java .nio .charset .StandardCharsets ;
2527import java .util .Random ;
2628
29+ import org .apache .commons .io .Charsets ;
30+ import org .apache .commons .io .charset .CharsetDecoders ;
2731import org .junit .jupiter .api .Test ;
2832
2933public class WriterOutputStreamTest {
@@ -51,6 +55,16 @@ public void testFlush() throws IOException {
5155 }
5256 }
5357
58+ @ Test
59+ public void testLargeUTF8CharsetWithBufferedWrite () throws IOException {
60+ testWithBufferedWrite (LARGE_TEST_STRING , "UTF-8" );
61+ }
62+
63+ @ Test
64+ public void testLargeUTF8CharsetWithSingleByteWrite () throws IOException {
65+ testWithSingleByteWrite (LARGE_TEST_STRING , StandardCharsets .UTF_8 );
66+ }
67+
5468 @ Test
5569 public void testLargeUTF8WithBufferedWrite () throws IOException {
5670 testWithBufferedWrite (LARGE_TEST_STRING , "UTF-8" );
@@ -61,6 +75,21 @@ public void testLargeUTF8WithSingleByteWrite() throws IOException {
6175 testWithSingleByteWrite (LARGE_TEST_STRING , "UTF-8" );
6276 }
6377
78+ @ Test
79+ public void testNullCharsetNameWithSingleByteWrite () throws IOException {
80+ testWithSingleByteWrite (TEST_STRING , (String ) null );
81+ }
82+
83+ @ Test
84+ public void testNullCharsetWithSingleByteWrite () throws IOException {
85+ testWithSingleByteWrite (TEST_STRING , (Charset ) null );
86+ }
87+
88+ @ Test
89+ public void testNullCharsetDecoderWithSingleByteWrite () throws IOException {
90+ testWithSingleByteWrite (TEST_STRING , (CharsetDecoder ) null );
91+ }
92+
6493 @ Test
6594 public void testUTF16BEWithBufferedWrite () throws IOException {
6695 testWithBufferedWrite (TEST_STRING , "UTF-16BE" );
@@ -128,8 +157,30 @@ private void testWithBufferedWrite(final String testString, final String charset
128157 }
129158
130159
160+ private void testWithSingleByteWrite (final String testString , final Charset charset ) throws IOException {
161+ final byte [] bytes = testString .getBytes (Charsets .toCharset (charset ));
162+ final StringWriter writer = new StringWriter ();
163+ try (final WriterOutputStream out = new WriterOutputStream (writer , charset )) {
164+ for (final byte b : bytes ) {
165+ out .write (b );
166+ }
167+ }
168+ assertEquals (testString , writer .toString ());
169+ }
170+
171+ private void testWithSingleByteWrite (final String testString , final CharsetDecoder charsetDecoder ) throws IOException {
172+ final byte [] bytes = testString .getBytes (CharsetDecoders .toCharsetDecoder (charsetDecoder ).charset ());
173+ final StringWriter writer = new StringWriter ();
174+ try (final WriterOutputStream out = new WriterOutputStream (writer , charsetDecoder )) {
175+ for (final byte b : bytes ) {
176+ out .write (b );
177+ }
178+ }
179+ assertEquals (testString , writer .toString ());
180+ }
181+
131182 private void testWithSingleByteWrite (final String testString , final String charsetName ) throws IOException {
132- final byte [] bytes = testString .getBytes (charsetName );
183+ final byte [] bytes = testString .getBytes (Charsets . toCharset ( charsetName ) );
133184 final StringWriter writer = new StringWriter ();
134185 try (final WriterOutputStream out = new WriterOutputStream (writer , charsetName )) {
135186 for (final byte b : bytes ) {
0 commit comments