ReaderWrapper.java 968 Bytes
package ipipan.clarin.tei.impl.io.read;

import java.io.IOException;
import java.io.Reader;

/**
 *
 * @author mlenart
 */
class ReaderWrapper extends Reader {

	private final static int BYTE_ORDER_MARK = 65279;
	private final Reader reader;
	private final char[] firstChar = new char[1];
	private int off = 0;

	ReaderWrapper(Reader reader) throws IOException {
		this.reader = reader;
		int n = this.reader.read(firstChar);
		if (n < 1) {
			throw new IOException("Stream is too short");
		} else {
			if (firstChar[0] == BYTE_ORDER_MARK) {
				// logger.warn("EQUALS");
				off = 1;
			}
		}
	}

	@Override
	public int read(char[] cbuf, int off, int len) throws IOException {
		int i = 0;
		if (this.off < 1 && len > 0) {
			cbuf[off + i] = firstChar[this.off];
			this.off++;
			i++;
		}
		if (i < len) {
			return reader.read(cbuf, off + i, len - i) + i;
		} else {
			return i;
		}
	}

	@Override
	public void close() throws IOException {
		reader.close();
	}
}