Class UnicodeBOMInputStream

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

    public class UnicodeBOMInputStream
    extends java.io.InputStream
    The UnicodeBOMInputStream class wraps any InputStream and detects the presence of any Unicode BOM (Byte Order Mark) at its beginning, as defined by RFC 3629 - UTF-8, a transformation format of ISO 10646

    The Unicode FAQ defines 5 types of BOMs:

    • 00 00 FE FF  = UTF-32, big-endian
    • FF FE 00 00  = UTF-32, little-endian
    • FE FF        = UTF-16, big-endian
    • FF FE        = UTF-16, little-endian
    • EF BB BF     = UTF-8

    Use the getBOM() method to know whether a BOM has been detected or not.

    Use the skipBOM() method to remove the detected BOM from the wrapped InputStream object.

    Version:
    1.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  UnicodeBOMInputStream.BOM
      Type safe enumeration class that describes the different types of Unicode BOMs.
    • Constructor Summary

      Constructors 
      Constructor Description
      UnicodeBOMInputStream​(java.io.InputStream inputStream)
      Constructs a new UnicodeBOMInputStream that wraps the specified InputStream.
      UnicodeBOMInputStream​(java.io.InputStream inputStream, boolean skipIfFound)
      Constructs a new UnicodeBOMInputStream that wraps the specified InputStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      void close()
      UnicodeBOMInputStream.BOM getBOM()
      Returns the BOM that was detected in the wrapped InputStream object.
      void mark​(int readlimit)
      boolean markSupported()
      int read()
      int read​(byte[] b)
      int read​(byte[] b, int off, int len)
      void reset()
      long skip​(long n)
      UnicodeBOMInputStream skipBOM()
      Skips the BOM that was found in the wrapped InputStream object.
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • in

        private final java.io.PushbackInputStream in
      • skipped

        private boolean skipped
    • Constructor Detail

      • UnicodeBOMInputStream

        public UnicodeBOMInputStream​(java.io.InputStream inputStream)
                              throws java.io.IOException
        Constructs a new UnicodeBOMInputStream that wraps the specified InputStream. By default skip BOM bytes
        Parameters:
        inputStream - an InputStream.
        Throws:
        java.lang.NullPointerException - when inputStream is null.
        java.io.IOException - on reading from the specified InputStream when trying to detect the Unicode BOM.
      • UnicodeBOMInputStream

        public UnicodeBOMInputStream​(java.io.InputStream inputStream,
                                     boolean skipIfFound)
                              throws java.io.IOException
        Constructs a new UnicodeBOMInputStream that wraps the specified InputStream.
        Parameters:
        inputStream - an InputStream.
        skipIfFound - to automatically skip BOM bytes if found
        Throws:
        java.lang.NullPointerException - when inputStream is null.
        java.io.IOException - on reading from the specified InputStream when trying to detect the Unicode BOM.
    • Method Detail

      • getBOM

        public final UnicodeBOMInputStream.BOM getBOM()
        Returns the BOM that was detected in the wrapped InputStream object.
        Returns:
        a BOM value.
      • skipBOM

        public final UnicodeBOMInputStream skipBOM()
                                            throws java.io.IOException
        Skips the BOM that was found in the wrapped InputStream object.
        Returns:
        this UnicodeBOMInputStream.
        Throws:
        java.io.IOException - when trying to skip the BOM from the wrapped InputStream object.
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • mark

        public void mark​(int readlimit)
        Overrides:
        mark in class java.io.InputStream
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.InputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream