Package org.miaixz.bus.core.io.stream
Class BOMInputStream
java.lang.Object
java.io.InputStream
org.miaixz.bus.core.io.stream.BOMInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
读取带BOM头的流内容,
getCharset()方法调用后会得到BOM头的编码,且会去除BOM头
BOM定义:http://www.unicode.org/unicode/faq/utf_bom.html
- 00 00 FE FF = UTF-32, big-endian
- FF FE 00 00 = UTF-32, little-endian
- EF BB BF = UTF-8
- FE FF = UTF-16, big-endian
- FF FE = UTF-16, little-endian
String enc = "UTF-8"; // or NULL to use systemdefault
FileInputStream fis = new FileInputStream(file);
BOMInputStream uin = new BOMInputStream(fis, enc);
enc = uin.getCharset(); // check and skip possible BOM bytes
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
BOMInputStream
构造- Parameters:
in- 流
-
BOMInputStream
构造- Parameters:
in- 流defaultCharset- 默认编码
-
-
Method Details
-
getDefaultCharset
获取默认编码- Returns:
- 默认编码
-
getCharset
获取BOM头中的编码- Returns:
- 编码
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
init
Read-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only BOM bytes are skipped.- Throws:
IOException- 读取引起的异常
-