public class BOMInputStream extends ProxyInputStream
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:
ByteOrderMark.UTF_8ByteOrderMark.UTF_16LEByteOrderMark.UTF_16BEByteOrderMark.UTF_32LEByteOrderMark.UTF_32BE
To build an instance, see BOMInputStream.Builder.
BOMInputStream bomIn = BOMInputStream.builder().setInputStream(in).get();
if (bomIn.hasBOM()) {
// has a UTF-8 BOM
}
boolean include = true;
BOMInputStream bomIn = BOMInputStream.builder()
.setInputStream(in)
.setInclude(include)
.get();
if (bomIn.hasBOM()) {
// has a UTF-8 BOM
}
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
}
ByteOrderMark,
Wikipedia - Byte Order Mark| Modifier and Type | Class and Description |
|---|---|
static class |
BOMInputStream.Builder
Builds a new
BOMInputStream instance. |
| Constructor and Description |
|---|
BOMInputStream(java.io.InputStream delegate)
Deprecated.
|
BOMInputStream(java.io.InputStream delegate,
boolean include)
Deprecated.
|
BOMInputStream(java.io.InputStream delegate,
boolean include,
ByteOrderMark... boms)
Deprecated.
|
BOMInputStream(java.io.InputStream delegate,
ByteOrderMark... boms)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
static BOMInputStream.Builder |
builder()
Constructs a new
BOMInputStream.Builder. |
ByteOrderMark |
getBOM()
Gets the BOM (Byte Order Mark).
|
java.lang.String |
getBOMCharsetName()
Gets the BOM charset Name -
ByteOrderMark.getCharsetName(). |
boolean |
hasBOM()
Tests whether the stream contains one of the specified BOMs.
|
boolean |
hasBOM(ByteOrderMark bom)
Tests whether the stream contains the specified BOM.
|
void |
mark(int readlimit)
Invokes the delegate's
mark(int) method. |
int |
read()
Invokes the delegate's
read() method, detecting and optionally skipping BOM. |
int |
read(byte[] buf)
Invokes the delegate's
read(byte[]) method, detecting and optionally skipping BOM. |
int |
read(byte[] buf,
int off,
int len)
Invokes the delegate's
read(byte[], int, int) method, detecting and optionally skipping BOM. |
void |
reset()
Invokes the delegate's
reset() method. |
long |
skip(long n)
Invokes the delegate's
skip(long) method, detecting and optionally skipping BOM. |
available, close, markSupported@Deprecated public BOMInputStream(java.io.InputStream delegate)
builder(), BOMInputStream.Builder, and BOMInputStream.Builder.get()ByteOrderMark.UTF_8 BOM.delegate - the InputStream to delegate to@Deprecated
public BOMInputStream(java.io.InputStream delegate,
boolean include)
builder(), BOMInputStream.Builder, and BOMInputStream.Builder.get()ByteOrderMark.UTF_8 and optionally includes it.delegate - the InputStream to delegate toinclude - true to include the UTF-8 BOM or false to exclude it@Deprecated
public BOMInputStream(java.io.InputStream delegate,
boolean include,
ByteOrderMark... boms)
builder(), BOMInputStream.Builder, and BOMInputStream.Builder.get()delegate - the InputStream to delegate toinclude - true to include the specified BOMs or false to exclude themboms - The BOMs to detect and optionally exclude@Deprecated
public BOMInputStream(java.io.InputStream delegate,
ByteOrderMark... boms)
builder(), BOMInputStream.Builder, and BOMInputStream.Builder.get()delegate - the InputStream to delegate toboms - The BOMs to detect and excludepublic static BOMInputStream.Builder builder()
BOMInputStream.Builder.BOMInputStream.Builder.public ByteOrderMark getBOM() throws java.io.IOException
java.io.IOException - if an error reading the first bytes of the stream occurspublic java.lang.String getBOMCharsetName()
throws java.io.IOException
ByteOrderMark.getCharsetName().java.io.IOException - if an error reading the first bytes of the stream occurspublic boolean hasBOM()
throws java.io.IOException
java.io.IOException - if an error reading the first bytes of the stream occurspublic boolean hasBOM(ByteOrderMark bom) throws java.io.IOException
bom - The BOM to check forjava.lang.IllegalArgumentException - if the BOM is not one the stream is configured to detectjava.io.IOException - if an error reading the first bytes of the stream occurspublic void mark(int readlimit)
mark(int) method.mark in class ProxyInputStreamreadlimit - read ahead limitpublic int read()
throws java.io.IOException
read() method, detecting and optionally skipping BOM.read in class ProxyInputStreamjava.io.IOException - if an I/O error occurspublic int read(byte[] buf)
throws java.io.IOException
read(byte[]) method, detecting and optionally skipping BOM.read in class ProxyInputStreambuf - the buffer to read the bytes intojava.io.IOException - if an I/O error occurspublic int read(byte[] buf,
int off,
int len)
throws java.io.IOException
read(byte[], int, int) method, detecting and optionally skipping BOM.read in class ProxyInputStreambuf - the buffer to read the bytes intooff - The start offsetlen - The number of bytes to read (excluding BOM)java.io.IOException - if an I/O error occurspublic void reset()
throws java.io.IOException
reset() method.reset in class ProxyInputStreamjava.io.IOException - if an I/O error occurspublic long skip(long n)
throws java.io.IOException
skip(long) method, detecting and optionally skipping BOM.skip in class ProxyInputStreamn - the number of bytes to skipjava.io.IOException - if an I/O error occursCopyright © 2010 - 2023 Adobe. All Rights Reserved