Class UnicodeBOMInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.github.dannil.scbjavaclient.utility.UnicodeBOMInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class UnicodeBOMInputStream extends InputStream
TheUnicodeBOMInputStream
class wraps anyInputStream
and detects the presence of any Unicode BOM (Byte Order Mark) at its beginning, as defined by RFC 3629 - UTF-8, a transformation format of ISO 10646.The Unicode FAQ defines 5 types of BOMs:
-
00 00 FE FF = UTF-32, big-endian
-
FF FE 00 00 = UTF-32, little-endian
-
FE FF = UTF-16, big-endian
-
FF FE = UTF-16, little-endian
-
EF BB BF = UTF-8
Use the
getBOM()
method to know whether a BOM has been detected or not.Use the
skipBOM()
method to remove the detected BOM from the wrappedInputStream
object.- Version:
- 1.0
- Author:
- Gregory Pakosz
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UnicodeBOMInputStream.BOM
Type safe enumeration class that describes the different types of Unicode BOMs.
-
Constructor Summary
Constructors Constructor Description UnicodeBOMInputStream(InputStream inputStream)
Constructs a newUnicodeBOMInputStream
that wraps the specifiedInputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
UnicodeBOMInputStream.BOM
getBOM()
Returns theBOM
that was detected in the wrappedInputStream
object.UnicodeBOMInputStream.BOM
getUtf16Bom(byte[] bomRawBytes)
Retrieves the UTF-16BOM
from the specifiedbyte
array.UnicodeBOMInputStream.BOM
getUtf32Bom(byte[] bomRawBytes)
Retrieves the UTF-32BOM
from the specifiedbyte
array.UnicodeBOMInputStream.BOM
getUtf8Bom(byte[] bomRawBytes)
Retrieves the UTF-8BOM
from the specifiedbyte
array.void
mark(int readlimit)
boolean
markSupported()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
void
reset()
long
skip(long n)
UnicodeBOMInputStream
skipBOM()
Skips theBOM
that was found in the wrappedInputStream
object.-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Constructor Detail
-
UnicodeBOMInputStream
public UnicodeBOMInputStream(InputStream inputStream) throws IOException
Constructs a newUnicodeBOMInputStream
that wraps the specifiedInputStream
.- Parameters:
inputStream
- anInputStream
.- Throws:
IOException
- on reading from the specifiedInputStream
when trying to detect the Unicode BOM.
-
-
Method Detail
-
getUtf32Bom
public final UnicodeBOMInputStream.BOM getUtf32Bom(byte[] bomRawBytes)
Retrieves the UTF-32BOM
from the specifiedbyte
array.- Parameters:
bomRawBytes
- the bytes to read- Returns:
- an
BOM
(if any) present in the specified array
-
getUtf8Bom
public final UnicodeBOMInputStream.BOM getUtf8Bom(byte[] bomRawBytes)
Retrieves the UTF-8BOM
from the specifiedbyte
array.- Parameters:
bomRawBytes
- the bytes to read- Returns:
- an
BOM
(if any) present in the specified array
-
getUtf16Bom
public final UnicodeBOMInputStream.BOM getUtf16Bom(byte[] bomRawBytes)
Retrieves the UTF-16BOM
from the specifiedbyte
array.- Parameters:
bomRawBytes
- the bytes to read- Returns:
- an
BOM
(if any) present in the specified array
-
getBOM
public final UnicodeBOMInputStream.BOM getBOM()
Returns theBOM
that was detected in the wrappedInputStream
object.- Returns:
- a
BOM
value.
-
skipBOM
public final UnicodeBOMInputStream skipBOM() throws IOException
Skips theBOM
that was found in the wrappedInputStream
object.- Returns:
- this
UnicodeBOMInputStream
. - Throws:
IOException
- when trying to skip the BOM from the wrappedInputStream
object.
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit)
- Overrides:
mark
in classInputStream
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classInputStream
-
-