6060 *
6161 * @author Alejandro Abdelnur
6262 * @version $Id$
63- * @see XmlStreamWriter
63+ * @see org.apache.commons.io.output. XmlStreamWriter
6464 */
6565public class XmlStreamReader extends Reader {
6666 private static final int BUFFER_SIZE = 4096 ;
@@ -305,6 +305,7 @@ public XmlStreamReader(InputStream is, String httpContentType)
305305 * the charset encoding.
306306 * @param lenient indicates if the charset encoding detection should be
307307 * relaxed.
308+ * @param defaultEncoding The default encoding
308309 * @throws IOException thrown if there is a problem reading the file.
309310 * @throws XmlStreamReaderException thrown if the charset encoding could not
310311 * be determined according to the specs.
@@ -363,6 +364,14 @@ public XmlStreamReader(InputStream is, String httpContentType,
363364 this (is , httpContentType , lenient , null );
364365 }
365366
367+ /**
368+ * Do lenient detection.
369+ *
370+ * @param httpContentType content-type header to use for the resolution of
371+ * the charset encoding.
372+ * @param ex The thrown exception
373+ * @throws IOException thrown if there is a problem reading the stream.
374+ */
366375 private void doLenientDetection (String httpContentType ,
367376 XmlStreamReaderException ex ) throws IOException {
368377 if (httpContentType != null ) {
@@ -399,6 +408,15 @@ public String getEncoding() {
399408 return encoding ;
400409 }
401410
411+ /**
412+ * Invokes the underlying reader's <code>read(char[], int, int)</code> method.
413+ * @param buf the buffer to read the characters into
414+ * @param offset The start offset
415+ * @param len The number of bytes to read
416+ * @return the number of characters read or -1 if the end of stream
417+ * @throws IOException if an I/O error occurs
418+ */
419+ @ Override
402420 public int read (char [] buf , int offset , int len ) throws IOException {
403421 return reader .read (buf , offset , len );
404422 }
@@ -408,10 +426,19 @@ public int read(char[] buf, int offset, int len) throws IOException {
408426 *
409427 * @throws IOException thrown if there was a problem closing the stream.
410428 */
429+ @ Override
411430 public void close () throws IOException {
412431 reader .close ();
413432 }
414433
434+ /**
435+ * Process the raw stream.
436+ *
437+ * @param is InputStream to create the reader from.
438+ * @param lenient indicates if the charset encoding detection should be
439+ * relaxed.
440+ * @throws IOException thrown if there is a problem reading the stream.
441+ */
415442 private void doRawStream (InputStream is , boolean lenient )
416443 throws IOException {
417444 BOMInputStream bom = createBomStream (new BufferedInputStream (is , BUFFER_SIZE ));
@@ -423,6 +450,15 @@ private void doRawStream(InputStream is, boolean lenient)
423450 prepareReader (pis , encoding );
424451 }
425452
453+ /**
454+ * Process a HTTP stream.
455+ *
456+ * @param is InputStream to create the reader from.
457+ * @param httpContentType The HTTP content type
458+ * @param lenient indicates if the charset encoding detection should be
459+ * relaxed.
460+ * @throws IOException thrown if there is a problem reading the stream.
461+ */
426462 private void doHttpStream (InputStream is , String httpContentType ,
427463 boolean lenient ) throws IOException {
428464 BOMInputStream bom = createBomStream (new BufferedInputStream (is , BUFFER_SIZE ));
@@ -437,25 +473,54 @@ private void doHttpStream(InputStream is, String httpContentType,
437473 prepareReader (pis , encoding );
438474 }
439475
476+ /**
477+ * Create a stream to detect UTF-8, UTF-16BE and UTF-16LE BOMs and consume them.
478+ *
479+ * @param delegate The delegate input stream
480+ * @return BOM detection stream
481+ */
440482 private BOMInputStream createBomStream (InputStream delegate ) {
441483 BOMInputStream bis =
442484 new BOMInputStream (delegate , false , ByteOrderMark .UTF_8 , ByteOrderMark .UTF_16BE , ByteOrderMark .UTF_16LE );
443485 return bis ;
444486 }
445487
488+ /**
489+ * Create a stream to Guess UTF-8, UTF-16BE, UTF-16LE and EBCDIC encodings
490+ * in XML streams.
491+ *
492+ * @param delegate The delegate input stream
493+ * @return XML encoding detection stream
494+ */
446495 private BOMInputStream createXmlStream (InputStream delegate ) {
447496 BOMInputStream bis =
448497 new BOMInputStream (delegate , true , XML_UTF_8 , XML_UTF_16BE , XML_UTF_16LE , XML_EBCDIC );
449498 return bis ;
450499 }
451500
501+ /**
502+ * Prepare the underlying reader.
503+ *
504+ * @param is InputStream to create the reader from.
505+ * @param encoding The encoding
506+ * @throws IOException thrown if there is a problem creating the reader.
507+ */
452508 private void prepareReader (InputStream is , String encoding )
453509 throws IOException {
454510 reader = new InputStreamReader (is , encoding );
455511 this .encoding = encoding ;
456512 }
457513
458- // InputStream is passed for XmlStreamReaderException creation only
514+ /**
515+ * Calculate the raw encoding.
516+ *
517+ * @param bomEnc BOM encoding
518+ * @param xmlGuessEnc XML Guess encoding
519+ * @param xmlEnc XML encoding
520+ * @param is InputStream to create the reader from.
521+ * @return the raw encoding
522+ * @throws IOException thrown if there is a problem reading the stream.
523+ */
459524 private String calculateRawEncoding (String bomEnc , String xmlGuessEnc ,
460525 String xmlEnc , InputStream is ) throws IOException {
461526 String encoding ;
@@ -501,7 +566,21 @@ private String calculateRawEncoding(String bomEnc, String xmlGuessEnc,
501566 return encoding ;
502567 }
503568
504- // InputStream is passed for XmlStreamReaderException creation only
569+
570+ /**
571+ * Calculate the HTTP encoding.
572+ *
573+ * @param cTMime Mime Content Type
574+ * @param cTEnc the content type encoding
575+ * @param bomEnc BOM encoding
576+ * @param xmlGuessEnc XML Guess encoding
577+ * @param xmlEnc XML encoding
578+ * @param is InputStream to create the reader from.
579+ * @param lenient indicates if the charset encoding detection should be
580+ * relaxed.
581+ * @return the HTTP encoding
582+ * @throws IOException thrown if there is a problem reading the stream.
583+ */
505584 private String calculateHttpEncoding (String cTMime , String cTEnc ,
506585 String bomEnc , String xmlGuessEnc , String xmlEnc , InputStream is ,
507586 boolean lenient ) throws IOException {
@@ -548,7 +627,12 @@ private String calculateHttpEncoding(String cTMime, String cTEnc,
548627 return encoding ;
549628 }
550629
551- // returns MIME type or NULL if httpContentType is NULL
630+ /**
631+ * Returns MIME type or NULL if httpContentType is NULL.
632+ *
633+ * @param httpContentType the HTTP content type
634+ * @return The mime content type
635+ */
552636 private static String getContentTypeMime (String httpContentType ) {
553637 String mime = null ;
554638 if (httpContentType != null ) {
@@ -562,8 +646,13 @@ private static String getContentTypeMime(String httpContentType) {
562646 private static final Pattern CHARSET_PATTERN = Pattern
563647 .compile ("charset=[\" ']?([.[^; \" ']]*)[\" ']?" );
564648
565- // returns charset parameter value, NULL if not present, NULL if
566- // httpContentType is NULL
649+ /**
650+ * Returns charset parameter value, NULL if not present, NULL if
651+ * httpContentType is NULL.
652+ *
653+ * @param httpContentType the HTTP content type
654+ * @return The content type encoding
655+ */
567656 private static String getContentTypeEncoding (String httpContentType ) {
568657 String encoding = null ;
569658 if (httpContentType != null ) {
@@ -582,7 +671,14 @@ private static String getContentTypeEncoding(String httpContentType) {
582671 "<\\ ?xml.*encoding[\\ s]*=[\\ s]*((?:\" .[^\" ]*\" )|(?:'.[^']*'))" ,
583672 Pattern .MULTILINE );
584673
585- // returns the encoding declared in the <?xml encoding=...?>, NULL if none
674+ /**
675+ * Returns the encoding declared in the <?xml encoding=...?>, NULL if none.
676+ *
677+ * @param is InputStream to create the reader from.
678+ * @param guessedEnc guessed encoding
679+ * @return the encoding declared in the <?xml encoding=...?>
680+ * @throws IOException thrown if there is a problem reading the stream.
681+ */
586682 private static String getXmlProlog (InputStream is , String guessedEnc )
587683 throws IOException {
588684 String encoding = null ;
@@ -631,7 +727,13 @@ private static String getXmlProlog(InputStream is, String guessedEnc)
631727 return encoding ;
632728 }
633729
634- // indicates if the MIME type belongs to the APPLICATION XML family
730+ /**
731+ * Indicates if the MIME type belongs to the APPLICATION XML family.
732+ *
733+ * @param mime The mime type
734+ * @return true if the mime type belongs to the APPLICATION XML family,
735+ * otherwise false
736+ */
635737 private static boolean isAppXml (String mime ) {
636738 return mime != null
637739 && (mime .equals ("application/xml" )
@@ -641,7 +743,13 @@ private static boolean isAppXml(String mime) {
641743 .startsWith ("application/" ) && mime .endsWith ("+xml" )));
642744 }
643745
644- // indicates if the MIME type belongs to the TEXT XML family
746+ /**
747+ * Indicates if the MIME type belongs to the TEXT XML family.
748+ *
749+ * @param mime The mime type
750+ * @return true if the mime type belongs to the TEXT XML family,
751+ * otherwise false
752+ */
645753 private static boolean isTextXml (String mime ) {
646754 return mime != null
647755 && (mime .equals ("text/xml" )
@@ -650,18 +758,18 @@ private static boolean isTextXml(String mime) {
650758 }
651759
652760 private static final MessageFormat RAW_EX_1 = new MessageFormat (
653- "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch" );
761+ "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch" );
654762
655763 private static final MessageFormat RAW_EX_2 = new MessageFormat (
656- "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM" );
764+ "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM" );
657765
658766 private static final MessageFormat HTTP_EX_1 = new MessageFormat (
659- "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL" );
767+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL" );
660768
661769 private static final MessageFormat HTTP_EX_2 = new MessageFormat (
662- "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch" );
770+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch" );
663771
664772 private static final MessageFormat HTTP_EX_3 = new MessageFormat (
665- "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME" );
773+ "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME" );
666774
667775}
0 commit comments