20 package org.matsim.core.utils.io;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.PushbackInputStream;
84 private final PushbackInputStream
in;
95 public UnicodeInputStream(
final InputStream inputStream)
throws NullPointerException, IOException {
96 this(inputStream,
true);
99 public UnicodeInputStream(
final InputStream inputStream,
final boolean skipBom)
throws NullPointerException, IOException {
100 if (inputStream == null) {
101 throw new NullPointerException(
"invalid input stream: null is not allowed");
104 in =
new PushbackInputStream(inputStream, 4);
106 final byte bytes[] =
new byte[4];
107 final int read = in.read(bytes);
111 if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE)
112 && (bytes[2] == (byte) 0x00) && (bytes[3] == (byte) 0x00)) {
115 }
else if ((bytes[0] == (byte) 0x00) && (bytes[1] == (byte) 0x00)
116 && (bytes[2] == (byte) 0xFE) && (bytes[3] == (byte) 0xFF)) {
122 if ((bytes[0] == (byte) 0xEF) && (bytes[1] == (byte) 0xBB)
123 && (bytes[2] == (byte) 0xBF)) {
129 if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE)) {
132 }
else if ((bytes[0] == (byte) 0xFE) && (bytes[1] == (byte) 0xFF)) {
143 in.unread(bytes, 0, read);
146 in.skip(bom.bytes.length);
155 public int read() throws IOException {
160 public int read(
final byte b[])
throws IOException, NullPointerException {
161 return in.read(b, 0, b.length);
165 public int read(
final byte b[],
final int off,
final int len)
166 throws IOException, NullPointerException {
167 return in.read(b, off, len);
171 public long skip(
final long n)
throws IOException {
177 return in.available();
181 public void close() throws IOException {
186 public synchronized void mark(
final int readlimit) {
191 public synchronized void reset() throws IOException {
197 return in.markSupported();
200 public static final class BOM {
201 public static final BOM NONE =
new BOM(
new byte[] {},
"NONE");
202 public static final BOM UTF_8 =
new BOM(
new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF },
"UTF-8");
203 public static final BOM UTF_16_LE =
new BOM(
new byte[] { (byte) 0xFF, (byte) 0xFE },
"UTF-16 little-endian");
204 public static final BOM UTF_16_BE =
new BOM(
new byte[] { (byte) 0xFE, (byte) 0xFF },
"UTF-16 big-endian");
205 public static final BOM UTF_32_LE =
new BOM(
new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x00, (byte) 0x00 },
"UTF-32 little-endian");
206 public static final BOM UTF_32_BE =
new BOM(
new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0xFE, (byte) 0xFF },
"UTF-32 big-endian");
217 final int length = bytes.length;
218 final byte[] result =
new byte[length];
221 System.arraycopy(bytes, 0, result, 0, length);
226 private BOM(
final byte bom[],
final String description) {