Author: niallp
Date: Mon Oct 4 04:35:52 2010
New Revision: 1004109
URL: http://svn.apache.org/viewvc?rev=1004109&view=rev
Log:
IO-162 Refactoring to remove the InputStream from XmlStreamReaderException
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReader.java
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReaderException.java
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReader.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReader.java?rev=1004109&r1=1004108&r2=1004109&view=diff
==============================================================================
---
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReader.java
(original)
+++
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReader.java
Mon Oct 4 04:35:52 2010
@@ -184,7 +184,7 @@ public class XmlStreamReader extends Rea
if (!lenient) {
throw ex;
} else {
- doLenientDetection(null, ex);
+ doLenientDetection(null, is, ex);
}
}
}
@@ -231,25 +231,19 @@ public class XmlStreamReader extends Rea
public XmlStreamReader(URLConnection conn) throws IOException {
defaultEncoding = staticDefaultEncoding;
boolean lenient = true;
- if (conn instanceof HttpURLConnection) {
+ InputStream is = conn.getInputStream();
+ String contentType = conn.getContentType();
+ if (conn instanceof HttpURLConnection || contentType != null) {
try {
- doHttpStream(conn.getInputStream(), conn.getContentType(),
- lenient);
+ doHttpStream(is, contentType, lenient);
} catch (XmlStreamReaderException ex) {
- doLenientDetection(conn.getContentType(), ex);
- }
- } else if (conn.getContentType() != null) {
- try {
- doHttpStream(conn.getInputStream(), conn.getContentType(),
- lenient);
- } catch (XmlStreamReaderException ex) {
- doLenientDetection(conn.getContentType(), ex);
+ doLenientDetection(contentType, is, ex);
}
} else {
try {
- doRawStream(conn.getInputStream(), lenient);
+ doRawStream(is, lenient);
} catch (XmlStreamReaderException ex) {
- doLenientDetection(null, ex);
+ doLenientDetection(null, is, ex);
}
}
}
@@ -321,7 +315,7 @@ public class XmlStreamReader extends Rea
if (!lenient) {
throw ex;
} else {
- doLenientDetection(httpContentType, ex);
+ doLenientDetection(httpContentType, is, ex);
}
}
}
@@ -369,10 +363,11 @@ public class XmlStreamReader extends Rea
*
* @param httpContentType content-type header to use for the resolution of
* the charset encoding.
+ * @param is the unconsumed InputStream
* @param ex The thrown exception
* @throws IOException thrown if there is a problem reading the stream.
*/
- private void doLenientDetection(String httpContentType,
+ private void doLenientDetection(String httpContentType, InputStream is,
XmlStreamReaderException ex) throws IOException {
if (httpContentType != null) {
if (httpContentType.startsWith("text/html")) {
@@ -380,7 +375,7 @@ public class XmlStreamReader extends Rea
.length());
httpContentType = "text/xml" + httpContentType;
try {
- doHttpStream(ex.getInputStream(), httpContentType, true);
+ doHttpStream(is, httpContentType, true);
ex = null;
} catch (XmlStreamReaderException ex2) {
ex = ex2;
@@ -395,7 +390,7 @@ public class XmlStreamReader extends Rea
if (encoding == null) {
encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding;
}
- prepareReader(ex.getInputStream(), encoding);
+ prepareReader(is, encoding);
}
}
@@ -538,12 +533,12 @@ public class XmlStreamReader extends Rea
if (xmlGuessEnc != null && !xmlGuessEnc.equals(UTF_8)) {
throw new XmlStreamReaderException(RAW_EX_1
.format(new Object[] { bomEnc, xmlGuessEnc, xmlEnc }),
- bomEnc, xmlGuessEnc, xmlEnc, is);
+ bomEnc, xmlGuessEnc, xmlEnc);
}
if (xmlEnc != null && !xmlEnc.equals(UTF_8)) {
throw new XmlStreamReaderException(RAW_EX_1
.format(new Object[] { bomEnc, xmlGuessEnc, xmlEnc }),
- bomEnc, xmlGuessEnc, xmlEnc, is);
+ bomEnc, xmlGuessEnc, xmlEnc);
}
encoding = UTF_8;
} else if (bomEnc.equals(UTF_16BE) || bomEnc.equals(UTF_16LE)) {
@@ -555,13 +550,13 @@ public class XmlStreamReader extends Rea
&& !xmlEnc.equals(bomEnc)) {
throw new XmlStreamReaderException(RAW_EX_1
.format(new Object[] { bomEnc, xmlGuessEnc, xmlEnc }),
- bomEnc, xmlGuessEnc, xmlEnc, is);
+ bomEnc, xmlGuessEnc, xmlEnc);
}
encoding = bomEnc;
} else {
throw new XmlStreamReaderException(RAW_EX_2.format(new Object[] {
bomEnc, xmlGuessEnc, xmlEnc }), bomEnc, xmlGuessEnc,
- xmlEnc, is);
+ xmlEnc);
}
return encoding;
}
@@ -604,7 +599,7 @@ public class XmlStreamReader extends Rea
throw new XmlStreamReaderException(HTTP_EX_1
.format(new Object[] { cTMime, cTEnc, bomEnc,
xmlGuessEnc, xmlEnc }), cTMime, cTEnc,
- bomEnc, xmlGuessEnc, xmlEnc, is);
+ bomEnc, xmlGuessEnc, xmlEnc);
} else if (cTEnc.equals(UTF_16)) {
if (bomEnc != null && bomEnc.startsWith(UTF_16)) {
encoding = bomEnc;
@@ -612,7 +607,7 @@ public class XmlStreamReader extends Rea
throw new XmlStreamReaderException(HTTP_EX_2
.format(new Object[] { cTMime, cTEnc, bomEnc,
xmlGuessEnc, xmlEnc }), cTMime, cTEnc,
- bomEnc, xmlGuessEnc, xmlEnc, is);
+ bomEnc, xmlGuessEnc, xmlEnc);
}
} else {
encoding = cTEnc;
@@ -621,7 +616,7 @@ public class XmlStreamReader extends Rea
throw new XmlStreamReaderException(HTTP_EX_3
.format(new Object[] { cTMime, cTEnc, bomEnc,
xmlGuessEnc, xmlEnc }), cTMime, cTEnc, bomEnc,
- xmlGuessEnc, xmlEnc, is);
+ xmlGuessEnc, xmlEnc);
}
}
return encoding;
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReaderException.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReaderException.java?rev=1004109&r1=1004108&r2=1004109&view=diff
==============================================================================
---
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReaderException.java
(original)
+++
commons/proper/io/trunk/src/java/org/apache/commons/io/input/XmlStreamReaderException.java
Mon Oct 4 04:35:52 2010
@@ -16,7 +16,6 @@
*/
package org.apache.commons.io.input;
-import java.io.InputStream;
import java.io.IOException;
/**
@@ -33,6 +32,9 @@ import java.io.IOException;
* @version $Id$
*/
public class XmlStreamReaderException extends IOException {
+
+ private static final long serialVersionUID = 1L;
+
private final String bomEncoding;
private final String xmlGuessEncoding;
@@ -43,8 +45,6 @@ public class XmlStreamReaderException ex
private final String contentTypeEncoding;
- private final InputStream is;
-
/**
* Creates an exception instance if the charset encoding could not be
* determined.
@@ -55,11 +55,10 @@ public class XmlStreamReaderException ex
* @param bomEnc BOM encoding.
* @param xmlGuessEnc XML guess encoding.
* @param xmlEnc XML prolog encoding.
- * @param is the unconsumed InputStream.
*/
public XmlStreamReaderException(String msg, String bomEnc,
- String xmlGuessEnc, String xmlEnc, InputStream is) {
- this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc, is);
+ String xmlGuessEnc, String xmlEnc) {
+ this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc);
}
/**
@@ -74,17 +73,15 @@ public class XmlStreamReaderException ex
* @param bomEnc BOM encoding.
* @param xmlGuessEnc XML guess encoding.
* @param xmlEnc XML prolog encoding.
- * @param is the unconsumed InputStream.
*/
public XmlStreamReaderException(String msg, String ctMime, String ctEnc,
- String bomEnc, String xmlGuessEnc, String xmlEnc, InputStream is) {
+ String bomEnc, String xmlGuessEnc, String xmlEnc) {
super(msg);
contentTypeMime = ctMime;
contentTypeEncoding = ctEnc;
bomEncoding = bomEnc;
xmlGuessEncoding = xmlGuessEnc;
xmlEncoding = xmlEnc;
- this.is = is;
}
/**
@@ -136,14 +133,4 @@ public class XmlStreamReaderException ex
public String getContentTypeEncoding() {
return contentTypeEncoding;
}
-
- /**
- * Returns the unconsumed InputStream to allow the application to do an
- * alternate encoding detection on the InputStream.
- *
- * @return the unconsumed InputStream.
- */
- public InputStream getInputStream() {
- return is;
- }
}