📄 utf8bomskipper.java
字号:
/**
* Created by IntelliJ IDEA.
* User: User
* Date: 2009-2-3
* Time: 13:10:12
* To change this template use File | Settings | File Templates.
*/
import java.io.*;
public class UTF8BOMSkipper
extends FilterInputStream {
//
// Data
//
/** Start of reading. */
private boolean fStart = true;
/** Byte offset. */
private int fOffset;
/** First three bytes. */
private int[] fFirst3Bytes;
//
// Constructors
//
/** Constructs a UTF-8 BOM skipper. */
public UTF8BOMSkipper(InputStream stream) {
super(stream);
} // <init>(InputStream)
//
// InputStream methods
//
/** Returns the next byte. */
public int read() throws IOException {
// read first three bytes in order to skip UTF-8 BOM, if present
if (fStart) {
fStart = false;
int b1 = super.read();
int b2 = super.read();
int b3 = super.read();
if (b1 != 0xEF || b2 != 0xBB || b3 != 0xBF) {
fFirst3Bytes = new int[3];
fFirst3Bytes[0] = b1;
fFirst3Bytes[1] = b2;
fFirst3Bytes[2] = b3;
}
}
// return read bytes
if (fFirst3Bytes != null) {
int b = fFirst3Bytes[fOffset++];
if (fOffset == fFirst3Bytes.length) {
fFirst3Bytes = null;
}
return b;
}
// return next char
return super.read();
} // read():int
/** Reads bytes into specified buffer and returns total bytes read. */
public int read(byte[] buffer, int offset, int length) throws IOException {
if (fStart || fFirst3Bytes != null) {
for (int i = 0; i < length; i++) {
int b = this.read();
if (b == -1) {
return i > 0 ? i : -1;
}
buffer[offset + i] = (byte)b;
}
return length;
}
return super.read(buffer, offset, length);
} // read(byte[],int,int):int
/** Mark is not supported for this input stream. */
public boolean markSupported() {
return false;
} // markSupported():boolean
/** Returns the number of bytes available. */
public int available() throws IOException {
if (fFirst3Bytes != null) {
return fFirst3Bytes.length - fOffset;
}
return super.available();
} // available():int
} // class UTF8BOMSkipper
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -