2929import java .nio .charset .CodingErrorAction ;
3030import java .util .Objects ;
3131
32+ import org .apache .commons .io .Charsets ;
33+ import org .apache .commons .io .charset .CharsetEncoders ;
34+
3235/**
3336 * {@link InputStream} implementation that reads a character stream from a {@link Reader} and transforms it to a byte
3437 * stream using a specified charset encoding. The stream is transformed using a {@link CharsetEncoder} object,
@@ -146,7 +149,7 @@ public ReaderInputStream(final Reader reader, final Charset charset) {
146149 public ReaderInputStream (final Reader reader , final Charset charset , final int bufferSize ) {
147150 // @formatter:off
148151 this (reader ,
149- charset .newEncoder ()
152+ Charsets . toCharset ( charset ) .newEncoder ()
150153 .onMalformedInput (CodingErrorAction .REPLACE )
151154 .onUnmappableCharacter (CodingErrorAction .REPLACE ),
152155 bufferSize );
@@ -168,14 +171,14 @@ public ReaderInputStream(final Reader reader, final CharsetEncoder charsetEncode
168171 * Constructs a new {@link ReaderInputStream}.
169172 *
170173 * @param reader the target {@link Reader}
171- * @param charsetEncoder the charset encoder
174+ * @param charsetEncoder the charset encoder, null defauls to the default Charset encoder.
172175 * @param bufferSize the size of the input buffer in number of characters
173176 * @since 2.1
174177 */
175178 public ReaderInputStream (final Reader reader , final CharsetEncoder charsetEncoder , final int bufferSize ) {
176179 this .reader = reader ;
177- this .charsetEncoder = charsetEncoder ;
178- this .encoderIn = CharBuffer .allocate (checkMinBufferSize (charsetEncoder , bufferSize ));
180+ this .charsetEncoder = CharsetEncoders . toCharsetEncoder ( charsetEncoder ) ;
181+ this .encoderIn = CharBuffer .allocate (checkMinBufferSize (this . charsetEncoder , bufferSize ));
179182 this .encoderIn .flip ();
180183 this .encoderOut = ByteBuffer .allocate (128 );
181184 this .encoderOut .flip ();
@@ -196,11 +199,11 @@ public ReaderInputStream(final Reader reader, final String charsetName) {
196199 * Constructs a new {@link ReaderInputStream}.
197200 *
198201 * @param reader the target {@link Reader}
199- * @param charsetName the name of the charset encoding
202+ * @param charsetName the name of the charset encoding, null maps to the default Charset.
200203 * @param bufferSize the size of the input buffer in number of characters
201204 */
202205 public ReaderInputStream (final Reader reader , final String charsetName , final int bufferSize ) {
203- this (reader , Charset . forName (charsetName ), bufferSize );
206+ this (reader , Charsets . toCharset (charsetName ), bufferSize );
204207 }
205208
206209 /**
@@ -244,6 +247,15 @@ private void fillBuffer() throws IOException {
244247 encoderOut .flip ();
245248 }
246249
250+ /**
251+ * Gets the CharsetEncoder.
252+ *
253+ * @return the CharsetEncoder.
254+ */
255+ CharsetEncoder getCharsetEncoder () {
256+ return charsetEncoder ;
257+ }
258+
247259 /**
248260 * Read a single byte.
249261 *
0 commit comments