Class BomInput
- java.lang.Object
-
- java.io.InputStream
-
- com.univocity.parsers.common.input.BomInput
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class BomInput extends java.io.InputStream
A wrapper for anInputStream
that attempts to detect a Byte Order Mark (BOM) in the input and derive the character encoding that should be used to decode the incoming content.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BomInput.BytesProcessedNotification
Internal notification exception used to re-wrap the originalInputStream
into aReader
.
-
Field Summary
Fields Modifier and Type Field Description private int[]
bytes
private int
bytesRead
private int
consumed
private java.lang.String
encoding
private java.io.IOException
exception
private java.io.InputStream
input
static byte[]
UTF_16BE_BOM
static byte[]
UTF_16LE_BOM
static byte[]
UTF_32BE_BOM
static byte[]
UTF_32LE_BOM
static byte[]
UTF_8_BOM
-
Constructor Summary
Constructors Constructor Description BomInput(java.io.InputStream input)
Wraps anInputStream
and reads the first bytes found on it to attempt to read a BOM.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.nio.charset.Charset
getCharset()
Returns the detectedCharset
determined by the Byte Order Mark (BOM) available in the input provided in the constructor of this class.java.lang.String
getEncoding()
Returns the detected encoding name determined by the Byte Order Mark (BOM) available in the input provided in the constructor of this class.boolean
hasBytesStored()
Returns a flag indicating whether or not all bytes read from the wrapped input stream have been consumed.private int
next()
int
read()
private void
setEncoding(java.lang.String encoding)
-
-
-
Field Detail
-
UTF_8_BOM
public static final byte[] UTF_8_BOM
-
UTF_16BE_BOM
public static final byte[] UTF_16BE_BOM
-
UTF_16LE_BOM
public static final byte[] UTF_16LE_BOM
-
UTF_32BE_BOM
public static final byte[] UTF_32BE_BOM
-
UTF_32LE_BOM
public static final byte[] UTF_32LE_BOM
-
bytesRead
private int bytesRead
-
bytes
private int[] bytes
-
encoding
private java.lang.String encoding
-
consumed
private int consumed
-
input
private final java.io.InputStream input
-
exception
private java.io.IOException exception
-
-
Method Detail
-
setEncoding
private void setEncoding(java.lang.String encoding)
-
next
private int next() throws java.io.IOException
- Throws:
java.io.IOException
-
read
public final int read() throws java.io.IOException
- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
hasBytesStored
public final boolean hasBytesStored()
Returns a flag indicating whether or not all bytes read from the wrapped input stream have been consumed. This allows client code to determine if the original input stream can be used directly and safely, or if thisBomInput
wrapper class should be used instead. If there are stored bytes that need to be consumed before the wrapped input stream is consumed again, this method will returntrue
.- Returns:
false
if there are no bytes stored and the original input stream can be used directly. If this wrapper needs to be used to return stored bytes before, thentrue
will be returned.
-
getCharset
public final java.nio.charset.Charset getCharset()
Returns the detectedCharset
determined by the Byte Order Mark (BOM) available in the input provided in the constructor of this class. If no BOM was detected, this method will returnnull
.- Returns:
- the detected
Charset
ornull
if a BOM could not be matched.
-
getEncoding
public final java.lang.String getEncoding()
Returns the detected encoding name determined by the Byte Order Mark (BOM) available in the input provided in the constructor of this class. If no BOM was detected, this method will returnnull
.- Returns:
- the detected encoding name or
null
if a BOM could not be matched.
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
-
-