Class BOMInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class BOMInputStream extends ProxyInputStream
This class is used to wrap a stream that includes an encoded ByteOrderMark as its first bytes.

This class detects these bytes and, if required, can automatically skip them and return the subsequent byte as the first byte in the stream.

The ByteOrderMark implementation has the following predefined BOMs:

To build an instance, use BOMInputStream.Builder.

Example 1 - Detecting and excluding a UTF-8 BOM

 BOMInputStream bomIn = BOMInputStream.builder().setInputStream(in).get();
 if (bomIn.hasBOM()) {
     // has a UTF-8 BOM
 }
 

Example 2 - Detecting a UTF-8 BOM without excluding it

 boolean include = true;
 BOMInputStream bomIn = BOMInputStream.builder()
     .setInputStream(in)
     .setInclude(include)
     .get();
 if (bomIn.hasBOM()) {
     // has a UTF-8 BOM
 }
 

Example 3 - Detecting Multiple BOMs

 BOMInputStream bomIn = BOMInputStream.builder()
   .setInputStream(in)
   .setByteOrderMarks(ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE)
   .get();
 if (bomIn.hasBOM() == false) {
     // No BOM found
 } else if (bomIn.hasBOM(ByteOrderMark.UTF_16LE)) {
     // has a UTF-16LE BOM
 } else if (bomIn.hasBOM(ByteOrderMark.UTF_16BE)) {
     // has a UTF-16BE BOM
 } else if (bomIn.hasBOM(ByteOrderMark.UTF_32LE)) {
     // has a UTF-32LE BOM
 } else if (bomIn.hasBOM(ByteOrderMark.UTF_32BE)) {
     // has a UTF-32BE BOM
 }
 

To build an instance, use BOMInputStream.Builder.

Since:
2.0
See Also: