Skip to content

Commit e870317

Browse files
author
Niall Pemberton
committed
IO-162 Refactoring to remove the InputStream from XmlStreamReaderException
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004109 13f79535-47bb-0310-9956-ffa450edef68
1 parent a2800c8 commit e870317

2 files changed

Lines changed: 26 additions & 44 deletions

File tree

src/java/org/apache/commons/io/input/XmlStreamReader.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/java/org/apache/commons/io/input/XmlStreamReaderException.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19-
import java.io.InputStream;
2019
import java.io.IOException;
2120

2221
/**
@@ -33,6 +32,9 @@
3332
* @version $Id$
3433
*/
3534
public class XmlStreamReaderException extends IOException {
35+
36+
private static final long serialVersionUID = 1L;
37+
3638
private final String bomEncoding;
3739

3840
private final String xmlGuessEncoding;
@@ -43,8 +45,6 @@ public class XmlStreamReaderException extends IOException {
4345

4446
private final String contentTypeEncoding;
4547

46-
private final InputStream is;
47-
4848
/**
4949
* Creates an exception instance if the charset encoding could not be
5050
* determined.
@@ -55,11 +55,10 @@ public class XmlStreamReaderException extends IOException {
5555
* @param bomEnc BOM encoding.
5656
* @param xmlGuessEnc XML guess encoding.
5757
* @param xmlEnc XML prolog encoding.
58-
* @param is the unconsumed InputStream.
5958
*/
6059
public XmlStreamReaderException(String msg, String bomEnc,
61-
String xmlGuessEnc, String xmlEnc, InputStream is) {
62-
this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc, is);
60+
String xmlGuessEnc, String xmlEnc) {
61+
this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
6362
}
6463

6564
/**
@@ -74,17 +73,15 @@ public XmlStreamReaderException(String msg, String bomEnc,
7473
* @param bomEnc BOM encoding.
7574
* @param xmlGuessEnc XML guess encoding.
7675
* @param xmlEnc XML prolog encoding.
77-
* @param is the unconsumed InputStream.
7876
*/
7977
public XmlStreamReaderException(String msg, String ctMime, String ctEnc,
80-
String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is) {
78+
String bomEnc, String xmlGuessEnc, String xmlEnc) {
8179
super(msg);
8280
contentTypeMime = ctMime;
8381
contentTypeEncoding = ctEnc;
8482
bomEncoding = bomEnc;
8583
xmlGuessEncoding = xmlGuessEnc;
8684
xmlEncoding = xmlEnc;
87-
this.is = is;
8885
}
8986

9087
/**
@@ -136,14 +133,4 @@ public String getContentTypeMime() {
136133
public String getContentTypeEncoding() {
137134
return contentTypeEncoding;
138135
}
139-
140-
/**
141-
* Returns the unconsumed InputStream to allow the application to do an
142-
* alternate encoding detection on the InputStream.
143-
*
144-
* @return the unconsumed InputStream.
145-
*/
146-
public InputStream getInputStream() {
147-
return is;
148-
}
149136
}

0 commit comments

Comments
 (0)