Class BOMInputStreamReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class BOMInputStreamReader
    extends java.io.InputStreamReader
    A reader that attempts to auto-detect the charset of an input stream from the beginning byte order mark (BOM).

    The input stream must be at its beginning and must support marking and resetting.

    By default the reader will be configured to report coding errors, although constructors are provided to override this. Note that InputStreamReader by default chooses to replace malformed input and unmappable characters rather than report them.

    Author:
    Garret Wilson
    See Also:
    ByteOrderMark, CodingErrorAction.REPORT
    • Field Summary

      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      BOMInputStreamReader​(java.io.InputStream inputStream)
      Constructs an input stream reader that uses UTF-8 as the default charset if the charset cannot be determined by the BOM.
      BOMInputStreamReader​(java.io.InputStream inputStream, java.nio.charset.Charset defaultCharset)
      Constructs an input stream reader that uses the given named charset as a default if the charset cannot be determined by the BOM.
      BOMInputStreamReader​(java.io.InputStream inputStream, java.nio.charset.Charset defaultCharset, java.nio.charset.CodingErrorAction codingErrorAction)
      Constructs an input stream reader that uses the given named charset as a default if the charset cannot be determined by the BOM.
      BOMInputStreamReader​(java.io.InputStream inputStream, java.nio.charset.CodingErrorAction codingErrorAction)
      Constructs an input stream reader that uses UTF-8 as the default charset if the charset cannot be determined by the BOM.
    • Method Summary

      • Methods inherited from class java.io.InputStreamReader

        close, getEncoding, read, read, ready
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, read, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BOMInputStreamReader

        public BOMInputStreamReader​(java.io.InputStream inputStream)
                             throws java.io.IOException,
                                    java.io.UnsupportedEncodingException
        Constructs an input stream reader that uses UTF-8 as the default charset if the charset cannot be determined by the BOM.

        The reader will be configured to report coding errors.

        Parameters:
        inputStream - An input stream; must be at its beginning and must support marking and resetting.
        Throws:
        java.io.IOException - if there is an error attempting to read the byte order mark from the input stream.
        java.io.UnsupportedEncodingException - if the named charset is not supported.
        See Also:
        CodingErrorAction.REPORT
      • BOMInputStreamReader

        public BOMInputStreamReader​(java.io.InputStream inputStream,
                                    java.nio.charset.Charset defaultCharset)
                             throws java.io.IOException,
                                    java.io.UnsupportedEncodingException
        Constructs an input stream reader that uses the given named charset as a default if the charset cannot be determined by the BOM.

        The reader will be configured to report coding errors.

        Parameters:
        inputStream - An input stream; must be at its beginning and must support marking and resetting.
        defaultCharset - The default charset to use if one cannot be determined by the BOM.
        Throws:
        java.io.IOException - if there is an error attempting to read the byte order mark from the input stream.
        java.io.UnsupportedEncodingException - if the named charset is not supported.
        See Also:
        CodingErrorAction.REPORT
      • BOMInputStreamReader

        public BOMInputStreamReader​(java.io.InputStream inputStream,
                                    @Nonnull
                                    java.nio.charset.CodingErrorAction codingErrorAction)
                             throws java.io.IOException,
                                    java.io.UnsupportedEncodingException
        Constructs an input stream reader that uses UTF-8 as the default charset if the charset cannot be determined by the BOM.
        Parameters:
        inputStream - An input stream; must be at its beginning and must support marking and resetting.
        codingErrorAction - The action to take if the data being decoded is invalid.
        Throws:
        java.io.IOException - if there is an error attempting to read the byte order mark from the input stream.
        java.io.UnsupportedEncodingException - if the named charset is not supported.
      • BOMInputStreamReader

        public BOMInputStreamReader​(java.io.InputStream inputStream,
                                    java.nio.charset.Charset defaultCharset,
                                    @Nonnull
                                    java.nio.charset.CodingErrorAction codingErrorAction)
                             throws java.io.IOException,
                                    java.io.UnsupportedEncodingException
        Constructs an input stream reader that uses the given named charset as a default if the charset cannot be determined by the BOM.
        Parameters:
        inputStream - An input stream; must be at its beginning and must support marking and resetting.
        defaultCharset - The default charset to use if one cannot be determined by the BOM.
        codingErrorAction - The action to take if the data being decoded is invalid.
        Throws:
        java.io.IOException - if there is an error attempting to read the byte order mark from the input stream.
        java.io.UnsupportedEncodingException - if the named charset is not supported.