Package org.apache.commons.io.input
Class BOMInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.ProxyInputStream
org.apache.commons.io.input.BOMInputStream
- All Implemented Interfaces:
- Closeable,- AutoCloseable
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:
 
- UTF-8 - ByteOrderMark.UTF_8
- UTF-16BE - ByteOrderMark.UTF_16LE
- UTF-16LE - ByteOrderMark.UTF_16BE
- UTF-32BE - ByteOrderMark.UTF_32LE
- UTF-32LE - ByteOrderMark.UTF_32BE
 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.
 
This class is not thread-safe.
- Since:
- 2.0
- See Also:
- 
Nested Class SummaryNested ClassesNested classes/interfaces inherited from class org.apache.commons.io.input.ProxyInputStreamProxyInputStream.AbstractBuilder<T,B extends AbstractStreamBuilder<T, B>> 
- 
Field SummaryFields inherited from class java.io.FilterInputStreamin
- 
Constructor SummaryConstructorsConstructorDescriptionBOMInputStream(InputStream delegate) Deprecated.BOMInputStream(InputStream delegate, boolean include) Deprecated.BOMInputStream(InputStream delegate, boolean include, ByteOrderMark... boms) Deprecated.BOMInputStream(InputStream delegate, ByteOrderMark... boms) Deprecated.
- 
Method SummaryModifier and TypeMethodDescriptionstatic BOMInputStream.Builderbuilder()Constructs a newBOMInputStream.Builder.getBOM()Gets the ByteOrderMark (Byte Order Mark).Gets the BOM charset Name -ByteOrderMark.getCharsetName().booleanhasBOM()Tests whether the stream contains one of the specified BOMs.booleanhasBOM(ByteOrderMark bom) Tests whether the stream contains the specified BOM.voidmark(int readLimit) Invokes the delegate'smark(int)method.intread()Invokes the delegate'sread()method, detecting and optionally skipping BOM.intread(byte[] buf) Invokes the delegate'sread(byte[])method, detecting and optionally skipping BOM.intread(byte[] buf, int off, int len) Invokes the delegate'sread(byte[], int, int)method, detecting and optionally skipping BOM.voidreset()Invokes the delegate'sreset()method.longskip(long n) Invokes the delegate'sskip(long)method, detecting and optionally skipping BOM.Methods inherited from class org.apache.commons.io.input.ProxyInputStreamafterRead, available, beforeRead, close, handleIOException, markSupported, setReference, unwrap
- 
Constructor Details- 
BOMInputStreamDeprecated.Constructs a new BOM InputStream that excludes aByteOrderMark.UTF_8BOM.- Parameters:
- delegate- the InputStream to delegate to
 
- 
BOMInputStreamDeprecated.Constructs a new BOM InputStream that detects aByteOrderMark.UTF_8and optionally includes it.- Parameters:
- delegate- the InputStream to delegate to
- include- true to include the UTF-8 BOM or false to exclude it
 
- 
BOMInputStreamDeprecated.Constructs a new BOM InputStream that detects the specified BOMs and optionally includes them.- Parameters:
- delegate- the InputStream to delegate to
- include- true to include the specified BOMs or false to exclude them
- boms- The BOMs to detect and optionally exclude
 
- 
BOMInputStreamDeprecated.Constructs a new BOM InputStream that excludes the specified BOMs.- Parameters:
- delegate- the InputStream to delegate to
- boms- The BOMs to detect and exclude
 
 
- 
- 
Method Details- 
builderConstructs a newBOMInputStream.Builder.- Returns:
- a new BOMInputStream.Builder.
- Since:
- 2.12.0
 
- 
getBOMGets the ByteOrderMark (Byte Order Mark).- Returns:
- The BOM or null if none matched.
- Throws:
- IOException- if an error reading the first bytes of the stream occurs.
 
- 
getBOMCharsetNameGets the BOM charset Name -ByteOrderMark.getCharsetName().- Returns:
- The BOM charset Name or null if no BOM found
- Throws:
- IOException- if an error reading the first bytes of the stream occurs
 
- 
hasBOMTests whether the stream contains one of the specified BOMs.- Returns:
- true if the stream has one of the specified BOMs, otherwise false if it does not
- Throws:
- IOException- if an error reading the first bytes of the stream occurs
 
- 
hasBOMTests whether the stream contains the specified BOM.- Parameters:
- bom- The BOM to check for
- Returns:
- true if the stream has the specified BOM, otherwise false if it does not
- Throws:
- IllegalArgumentException- if the BOM is not one the stream is configured to detect
- IOException- if an error reading the first bytes of the stream occurs
 
- 
markInvokes the delegate'smark(int)method.- Overrides:
- markin class- ProxyInputStream
- Parameters:
- readLimit- read ahead limit
 
- 
readInvokes the delegate'sread()method, detecting and optionally skipping BOM.- Overrides:
- readin class- ProxyInputStream
- Returns:
- the byte read (excluding BOM) or -1 if the end of stream
- Throws:
- IOException- if an I/O error occurs
 
- 
readInvokes the delegate'sread(byte[])method, detecting and optionally skipping BOM.- Overrides:
- readin class- ProxyInputStream
- Parameters:
- buf- the buffer to read the bytes into
- Returns:
- the number of bytes read (excluding BOM) or -1 if the end of stream
- Throws:
- IOException- if an I/O error occurs
 
- 
readInvokes the delegate'sread(byte[], int, int)method, detecting and optionally skipping BOM.- Overrides:
- readin class- ProxyInputStream
- Parameters:
- buf- the buffer to read the bytes into
- off- The start offset
- len- The number of bytes to read (excluding BOM)
- Returns:
- the number of bytes read or -1 if the end of stream
- Throws:
- IOException- if an I/O error occurs
 
- 
resetInvokes the delegate'sreset()method.- Overrides:
- resetin class- ProxyInputStream
- Throws:
- IOException- if an I/O error occurs
 
- 
skipInvokes the delegate'sskip(long)method, detecting and optionally skipping BOM.- Overrides:
- skipin class- ProxyInputStream
- Parameters:
- n- the number of bytes to skip
- Returns:
- the number of bytes to skipped or -1 if the end of stream
- Throws:
- IOException- if an I/O error occurs
 
 
- 
builder(),BOMInputStream.Builder, andBOMInputStream.Builder.get()