/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.io.stream;
import java.io.EOFException;
import java.io.IOException;
import java.io.UTFDataFormatException;
/**
* @author kimchy (shay.banon)
*/
public class BytesStreamInput extends StreamInput {
protected byte buf[];
protected int pos;
protected int count;
public BytesStreamInput(byte buf[]) {
this(buf, 0, buf.length);
}
public BytesStreamInput(byte buf[], int offset, int length) {
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
}
public int position() {
return this.pos;
}
@Override public int read() throws IOException {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
@Override public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
if (pos >= count) {
return -1;
}
if (pos + len > count) {
len = count - pos;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
public byte[] underlyingBuffer() {
return buf;
}
@Override public byte readByte() throws IOException {
if (pos >= count) {
throw new EOFException();
}
return buf[pos++];
}
@Override public void readBytes(byte[] b, int offset, int len) throws IOException {
if (len == 0) {
return;
}
if (pos >= count) {
throw new EOFException();
}
if (pos + len > count) {
len = count - pos;
}
if (len <= 0) {
throw new EOFException();
}
System.arraycopy(buf, pos, b, offset, len);
pos += len;
}
@Override public void reset() throws IOException {
pos = 0;
}
@Override public void close() throws IOException {
// nothing to do here...
}
public String readUTF() throws IOException {
int utflen = readInt();
if (utflen == 0) {
return "";
}
if (chararr.length < utflen) {
chararr = new char[utflen * 2];
}
char[] chararr = this.chararr;
byte[] bytearr = buf;
int endPos = pos + utflen;
int c, char2, char3;
int count = pos;
int chararr_count = 0;
while (count < endPos) {
c = (int) bytearr[count] & 0xff;
if (c > 127) break;
count++;
chararr[chararr_count++] = (char) c;
}
while (count < endPos) {
c = (int) bytearr[count] & 0xff;
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
/* 0xxxxxxx*/
count++;
chararr[chararr_count++] = (char) c;
break;
case 12:
case 13:
/* 110x xxxx 10xx xxxx*/
count += 2;
if (count > endPos)
throw new UTFDataFormatException(
"malformed input: partial character at end");
char2 = (int) bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
throw new UTFDataFormatException(
"malformed input around byte " + count);
chararr[chararr_count++] = (char) (((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > endPos)
throw new UTFDataFormatException(
"malformed input: partial character at end");
char2 = (int) bytearr[count - 2];
char3 = (int) bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new UTFDataFormatException(
"malformed input around byte " + (count - 1));
chararr[chararr_count++] = (char) (((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
/* 10xx xxxx, 1111 xxxx */
throw new UTFDataFormatException(
"malformed input around byte " + count);
}
}
pos += utflen;
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
}
}