Class UnicodeSupportingInputStream

java.lang.Object
java.io.InputStream
marauroa.common.io.UnicodeSupportingInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class UnicodeSupportingInputStream extends InputStream
This inputstream will recognize unicode BOM marks and will skip bytes if getEncoding() method is called before any of the read(...) methods. Usage pattern:
     String enc = "ISO-8859-1"; // or NULL to use systemdefault
     FileInputStream fis = new FileInputStream(file);
     UnicodeInputStream uin = new UnicodeInputStream(fis, enc);
     enc = uin.getEncoding(); // check and skip possible BOM bytes
     InputStreamReader in;
     if (enc == null) in = new InputStreamReader(uin);
     else in = new InputStreamReader(uin, enc);
    
Since:
2.1
  • Constructor Details

    • UnicodeSupportingInputStream

      public UnicodeSupportingInputStream(InputStream in)
      Creates a new UnicodeSupportingInputStream
      Parameters:
      in - inputstream to be read
    • UnicodeSupportingInputStream

      public UnicodeSupportingInputStream(InputStream in, String defaultEnc)
      Creates a new UnicodeSupportingInputStream
      Parameters:
      in - inputstream to be read
      defaultEnc - default encoding if stream does not have BOM marker. Give NULL to use system-level default.
  • Method Details

    • getDefaultEncoding

      public String getDefaultEncoding()
      returns the default encoding
      Returns:
      default encoding
    • getEncoding

      public String getEncoding()
      Get stream encoding or NULL if stream is uninitialized. Call init() or read() method to initialize it.
      Returns:
      actual encoding used to read this file
    • init

      protected void init() throws IOException
      Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only BOM bytes are skipped.
      Throws:
      IOException - in cases of an I/O error
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException