You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2010/10/06 04:36:39 UTC

svn commit: r1004882 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Author: sebb
Date: Wed Oct  6 02:36:39 2010
New Revision: 1004882

URL: http://svn.apache.org/viewvc?rev=1004882&view=rev
Log:
Make reader and encoding fields immutable

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java?rev=1004882&r1=1004881&r2=1004882&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java Wed Oct  6 02:36:39 2010
@@ -91,9 +91,9 @@ public class XmlStreamReader extends Rea
     };
 
 
-    private Reader reader;
+    private final Reader reader;
 
-    private String encoding;
+    private final String encoding;
 
     private final String defaultEncoding;
 
@@ -201,7 +201,8 @@ public class XmlStreamReader extends Rea
      */
     public XmlStreamReader(InputStream is, boolean lenient, String defaultEncoding) throws IOException {
         this.defaultEncoding = defaultEncoding;
-        doRawStream(is, lenient);
+        this.encoding = doRawStream(is, lenient);
+        this.reader = new InputStreamReader(is, encoding);
     }
 
     /**
@@ -248,11 +249,13 @@ public class XmlStreamReader extends Rea
         this.defaultEncoding = defaultEncoding;
         boolean lenient = true;
         String contentType = conn.getContentType();
+        InputStream is = conn.getInputStream();
         if (conn instanceof HttpURLConnection || contentType != null) {
-            doHttpStream(conn.getInputStream(), contentType, lenient);
+            this.encoding = doHttpStream(is, contentType, lenient);
         } else {
-            doRawStream(conn.getInputStream(), lenient);
+            this.encoding = doRawStream(is, lenient);
         }
+        this.reader = new InputStreamReader(is, encoding);
     }
 
     /**
@@ -314,7 +317,8 @@ public class XmlStreamReader extends Rea
     public XmlStreamReader(InputStream is, String httpContentType,
             boolean lenient, String defaultEncoding) throws IOException {
         this.defaultEncoding = defaultEncoding;
-        doHttpStream(is, httpContentType, lenient);
+        this.encoding = doHttpStream(is, httpContentType, lenient);
+        this.reader = new InputStreamReader(is, encoding);
     }
 
     /**
@@ -425,9 +429,10 @@ public class XmlStreamReader extends Rea
      * @param is InputStream to create the reader from.
      * @param lenient indicates if the charset encoding detection should be
      *        relaxed.
+     * @return the encoding to be used
      * @throws IOException thrown if there is a problem reading the stream.
      */
-    private void doRawStream(InputStream is, boolean lenient)
+    private String doRawStream(InputStream is, boolean lenient)
             throws IOException {
         BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS);
         BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);
@@ -435,15 +440,14 @@ public class XmlStreamReader extends Rea
         String xmlGuessEnc = pis.getBOMCharsetName();
         String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
         try {
-            this.encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
+            return calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
         } catch (XmlStreamReaderException ex) {
             if (lenient) {
-                this.encoding = doLenientDetection(null, is, ex);
+                return doLenientDetection(null, is, ex);
             } else {
                 throw ex;
             }
         }
-        this.reader = new InputStreamReader(is, encoding);
     }
 
     /**
@@ -453,9 +457,10 @@ public class XmlStreamReader extends Rea
      * @param httpContentType The HTTP content type
      * @param lenient indicates if the charset encoding detection should be
      *        relaxed.
+     * @return the encoding to be used
      * @throws IOException thrown if there is a problem reading the stream.
      */
-    private void doHttpStream(InputStream is, String httpContentType,
+    private String doHttpStream(InputStream is, String httpContentType,
             boolean lenient) throws IOException {
         BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS);
         BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);
@@ -463,16 +468,15 @@ public class XmlStreamReader extends Rea
         String xmlGuessEnc = pis.getBOMCharsetName();
         String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
         try {
-            this.encoding = calculateHttpEncoding(httpContentType, bomEnc,
+            return calculateHttpEncoding(httpContentType, bomEnc,
                     xmlGuessEnc, xmlEnc, lenient);
         } catch (XmlStreamReaderException ex) {
             if (lenient) {
-                this.encoding = doLenientDetection(httpContentType, is, ex);
+                return doLenientDetection(httpContentType, is, ex);
             } else {
                 throw ex;
             }
         }
-        this.reader = new InputStreamReader(is, encoding);
     }
 
     /**



Re: svn commit: r1004882 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Posted by sebb <se...@gmail.com>.
On 6 October 2010 03:57, Niall Pemberton <ni...@gmail.com> wrote:
> On Wed, Oct 6, 2010 at 3:41 AM, sebb AT ASF <se...@apache.org> wrote:
>> On 6 October 2010 03:36,  <se...@apache.org> wrote:
>>> Author: sebb
>>> Date: Wed Oct  6 02:36:39 2010
>>> New Revision: 1004882
>>>
>>> URL: http://svn.apache.org/viewvc?rev=1004882&view=rev
>>> Log:
>>> Make reader and encoding fields immutable
>>
>> Note: the same changes could be applied to the identically named class
>> in the compatibility package, if required.
>> However, that class will remain thread-hostile because of the mutable
>> static field.
>
> The compatibility package is only in the "test" directory and just
> intended to sanity check some of the refactoring I did. So I'd prefer
> if possible it be left as close to its original state.

OK, makes sense.

Might be worth adding a header comment to the file to record this
information if the class is going to remain there for a while.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1004882 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Posted by Niall Pemberton <ni...@gmail.com>.
On Wed, Oct 6, 2010 at 3:41 AM, sebb AT ASF <se...@apache.org> wrote:
> On 6 October 2010 03:36,  <se...@apache.org> wrote:
>> Author: sebb
>> Date: Wed Oct  6 02:36:39 2010
>> New Revision: 1004882
>>
>> URL: http://svn.apache.org/viewvc?rev=1004882&view=rev
>> Log:
>> Make reader and encoding fields immutable
>
> Note: the same changes could be applied to the identically named class
> in the compatibility package, if required.
> However, that class will remain thread-hostile because of the mutable
> static field.

The compatibility package is only in the "test" directory and just
intended to sanity check some of the refactoring I did. So I'd prefer
if possible it be left as close to its original state.

Niall


>> Modified:
>>    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1004882 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Posted by sebb AT ASF <se...@apache.org>.
On 6 October 2010 03:36,  <se...@apache.org> wrote:
> Author: sebb
> Date: Wed Oct  6 02:36:39 2010
> New Revision: 1004882
>
> URL: http://svn.apache.org/viewvc?rev=1004882&view=rev
> Log:
> Make reader and encoding fields immutable

Note: the same changes could be applied to the identically named class
in the compatibility package, if required.
However, that class will remain thread-hostile because of the mutable
static field.

> Modified:
>    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/XmlStreamReader.java

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org