@@ -184,7 +184,7 @@ public XmlStreamReader(InputStream is, boolean lenient) throws IOException,
184184 if (!lenient ) {
185185 throw ex ;
186186 } else {
187- doLenientDetection (null , ex );
187+ doLenientDetection (null , is , ex );
188188 }
189189 }
190190 }
@@ -231,25 +231,19 @@ public XmlStreamReader(URL url) throws IOException {
231231 public XmlStreamReader (URLConnection conn ) throws IOException {
232232 defaultEncoding = staticDefaultEncoding ;
233233 boolean lenient = true ;
234- if (conn instanceof HttpURLConnection ) {
234+ InputStream is = conn .getInputStream ();
235+ String contentType = conn .getContentType ();
236+ if (conn instanceof HttpURLConnection || contentType != null ) {
235237 try {
236- doHttpStream (conn .getInputStream (), conn .getContentType (),
237- lenient );
238+ doHttpStream (is , contentType , lenient );
238239 } catch (XmlStreamReaderException ex ) {
239- doLenientDetection (conn .getContentType (), ex );
240- }
241- } else if (conn .getContentType () != null ) {
242- try {
243- doHttpStream (conn .getInputStream (), conn .getContentType (),
244- lenient );
245- } catch (XmlStreamReaderException ex ) {
246- doLenientDetection (conn .getContentType (), ex );
240+ doLenientDetection (contentType , is , ex );
247241 }
248242 } else {
249243 try {
250- doRawStream (conn . getInputStream () , lenient );
244+ doRawStream (is , lenient );
251245 } catch (XmlStreamReaderException ex ) {
252- doLenientDetection (null , ex );
246+ doLenientDetection (null , is , ex );
253247 }
254248 }
255249 }
@@ -321,7 +315,7 @@ public XmlStreamReader(InputStream is, String httpContentType,
321315 if (!lenient ) {
322316 throw ex ;
323317 } else {
324- doLenientDetection (httpContentType , ex );
318+ doLenientDetection (httpContentType , is , ex );
325319 }
326320 }
327321 }
@@ -369,18 +363,19 @@ public XmlStreamReader(InputStream is, String httpContentType,
369363 *
370364 * @param httpContentType content-type header to use for the resolution of
371365 * the charset encoding.
366+ * @param is the unconsumed InputStream
372367 * @param ex The thrown exception
373368 * @throws IOException thrown if there is a problem reading the stream.
374369 */
375- private void doLenientDetection (String httpContentType ,
370+ private void doLenientDetection (String httpContentType , InputStream is ,
376371 XmlStreamReaderException ex ) throws IOException {
377372 if (httpContentType != null ) {
378373 if (httpContentType .startsWith ("text/html" )) {
379374 httpContentType = httpContentType .substring ("text/html"
380375 .length ());
381376 httpContentType = "text/xml" + httpContentType ;
382377 try {
383- doHttpStream (ex . getInputStream () , httpContentType , true );
378+ doHttpStream (is , httpContentType , true );
384379 ex = null ;
385380 } catch (XmlStreamReaderException ex2 ) {
386381 ex = ex2 ;
@@ -395,7 +390,7 @@ private void doLenientDetection(String httpContentType,
395390 if (encoding == null ) {
396391 encoding = (defaultEncoding == null ) ? UTF_8 : defaultEncoding ;
397392 }
398- prepareReader (ex . getInputStream () , encoding );
393+ prepareReader (is , encoding );
399394 }
400395 }
401396
@@ -538,12 +533,12 @@ private String calculateRawEncoding(String bomEnc, String xmlGuessEnc,
538533 if (xmlGuessEnc != null && !xmlGuessEnc .equals (UTF_8 )) {
539534 throw new XmlStreamReaderException (RAW_EX_1
540535 .format (new Object [] { bomEnc , xmlGuessEnc , xmlEnc }),
541- bomEnc , xmlGuessEnc , xmlEnc , is );
536+ bomEnc , xmlGuessEnc , xmlEnc );
542537 }
543538 if (xmlEnc != null && !xmlEnc .equals (UTF_8 )) {
544539 throw new XmlStreamReaderException (RAW_EX_1
545540 .format (new Object [] { bomEnc , xmlGuessEnc , xmlEnc }),
546- bomEnc , xmlGuessEnc , xmlEnc , is );
541+ bomEnc , xmlGuessEnc , xmlEnc );
547542 }
548543 encoding = UTF_8 ;
549544 } else if (bomEnc .equals (UTF_16BE ) || bomEnc .equals (UTF_16LE )) {
@@ -555,13 +550,13 @@ private String calculateRawEncoding(String bomEnc, String xmlGuessEnc,
555550 && !xmlEnc .equals (bomEnc )) {
556551 throw new XmlStreamReaderException (RAW_EX_1
557552 .format (new Object [] { bomEnc , xmlGuessEnc , xmlEnc }),
558- bomEnc , xmlGuessEnc , xmlEnc , is );
553+ bomEnc , xmlGuessEnc , xmlEnc );
559554 }
560555 encoding = bomEnc ;
561556 } else {
562557 throw new XmlStreamReaderException (RAW_EX_2 .format (new Object [] {
563558 bomEnc , xmlGuessEnc , xmlEnc }), bomEnc , xmlGuessEnc ,
564- xmlEnc , is );
559+ xmlEnc );
565560 }
566561 return encoding ;
567562 }
@@ -604,15 +599,15 @@ private String calculateHttpEncoding(String cTMime, String cTEnc,
604599 throw new XmlStreamReaderException (HTTP_EX_1
605600 .format (new Object [] { cTMime , cTEnc , bomEnc ,
606601 xmlGuessEnc , xmlEnc }), cTMime , cTEnc ,
607- bomEnc , xmlGuessEnc , xmlEnc , is );
602+ bomEnc , xmlGuessEnc , xmlEnc );
608603 } else if (cTEnc .equals (UTF_16 )) {
609604 if (bomEnc != null && bomEnc .startsWith (UTF_16 )) {
610605 encoding = bomEnc ;
611606 } else {
612607 throw new XmlStreamReaderException (HTTP_EX_2
613608 .format (new Object [] { cTMime , cTEnc , bomEnc ,
614609 xmlGuessEnc , xmlEnc }), cTMime , cTEnc ,
615- bomEnc , xmlGuessEnc , xmlEnc , is );
610+ bomEnc , xmlGuessEnc , xmlEnc );
616611 }
617612 } else {
618613 encoding = cTEnc ;
@@ -621,7 +616,7 @@ private String calculateHttpEncoding(String cTMime, String cTEnc,
621616 throw new XmlStreamReaderException (HTTP_EX_3
622617 .format (new Object [] { cTMime , cTEnc , bomEnc ,
623618 xmlGuessEnc , xmlEnc }), cTMime , cTEnc , bomEnc ,
624- xmlGuessEnc , xmlEnc , is );
619+ xmlGuessEnc , xmlEnc );
625620 }
626621 }
627622 return encoding ;
0 commit comments