2727import java .net .HttpURLConnection ;
2828import java .net .URL ;
2929import java .net .URLConnection ;
30+ import java .nio .charset .Charset ;
3031import java .nio .charset .StandardCharsets ;
3132import java .nio .file .Files ;
3233import java .nio .file .Path ;
3738import java .util .regex .Pattern ;
3839
3940import org .apache .commons .io .ByteOrderMark ;
41+ import org .apache .commons .io .Charsets ;
4042import org .apache .commons .io .IOUtils ;
4143import org .apache .commons .io .build .AbstractStreamBuilder ;
4244import org .apache .commons .io .function .IOConsumer ;
@@ -97,6 +99,7 @@ public class XmlStreamReader extends Reader {
9799 * <p>
98100 * For example:
99101 * </p>
102+ *
100103 * <pre>{@code
101104 * XmlStreamReader r = XmlStreamReader.builder()
102105 * .setPath(path)
@@ -109,42 +112,45 @@ public class XmlStreamReader extends Reader {
109112 */
110113 public static class Builder extends AbstractStreamBuilder <XmlStreamReader , Builder > {
111114
115+ private boolean nullCharset = true ;
112116 private boolean lenient = true ;
113117 private String httpContentType ;
114118
115- public Builder setHttpContentType (final String httpContentType ) {
116- this .httpContentType = httpContentType ;
117- return this ;
118- }
119-
120119 @ SuppressWarnings ("resource" )
121120 @ Override
122121 public XmlStreamReader get () throws IOException {
123- final String defaultEncoding = getCharset (). equals ( getCharsetDefault ()) ? null : getCharset ().name ();
122+ final String defaultEncoding = nullCharset ? null : getCharset ().name ();
124123 // @formatter:off
125124 return httpContentType == null
126125 ? new XmlStreamReader (getOrigin ().getInputStream (), lenient , defaultEncoding )
127126 : new XmlStreamReader (getOrigin ().getInputStream (), httpContentType , lenient , defaultEncoding );
128127 // @formatter:on
129128 }
130129
130+ @ Override
131+ public Builder setCharset (final Charset charset ) {
132+ nullCharset = charset == null ;
133+ return super .setCharset (charset );
134+ }
135+
136+ @ Override
137+ public Builder setCharset (final String charset ) {
138+ nullCharset = charset == null ;
139+ return super .setCharset (Charsets .toCharset (charset , getCharsetDefault ()));
140+ }
141+
142+ public Builder setHttpContentType (final String httpContentType ) {
143+ this .httpContentType = httpContentType ;
144+ return this ;
145+ }
146+
131147 public Builder setLenient (final boolean lenient ) {
132148 this .lenient = lenient ;
133149 return this ;
134150 }
135151
136152 }
137153
138- /**
139- * Constructs a new {@link Builder}.
140- *
141- * @return a new {@link Builder}.
142- * @since 2.12.0
143- */
144- public static Builder builder () {
145- return new Builder ();
146- }
147-
148154 private static final String UTF_8 = StandardCharsets .UTF_8 .name ();
149155
150156 private static final String US_ASCII = StandardCharsets .US_ASCII .name ();
@@ -190,6 +196,16 @@ public static Builder builder() {
190196
191197 private static final String HTTP_EX_3 = "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME" ;
192198
199+ /**
200+ * Constructs a new {@link Builder}.
201+ *
202+ * @return a new {@link Builder}.
203+ * @since 2.12.0
204+ */
205+ public static Builder builder () {
206+ return new Builder ();
207+ }
208+
193209 /**
194210 * Gets the charset parameter value, NULL if not present, NULL if httpContentType is NULL.
195211 *
0 commit comments