/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.apache.activeblaze.wire;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Very similar to the java.io.ByteArrayInputStream but this version is not
 * thread safe.
 */
final public class BufferInputStream extends InputStream implements DataInput {
    private byte readBuffer[] = new byte[8];
    private char[] charArray = new char[32];
    byte buffer[];
    int limit;
    int pos;
    int mark;

    public BufferInputStream(byte data[]) {
        this(data, 0, data.length);
    }

    public BufferInputStream(Buffer sequence) {
        this(sequence.getData(), sequence.getOffset(), sequence.getLength());
    }

    public BufferInputStream(byte data[], int offset, int size) {
        this.buffer = data;
        this.mark = offset;
        this.pos = offset;
        this.limit = offset + size;
    }

    public final int read() {
        return this.buffer[this.pos++] & 0xff;
    }

    public int read(byte[] b) {
        return read(b, 0, b.length);
    }

    public int read(byte b[], int off, int len) {
        if (pos < limit) {
            len = Math.min(len, limit - pos);
            System.arraycopy(buffer, pos, b, off, len);
            pos += len;
            return len;
        } else {
            return -1;
        }
    }

    public Buffer readBuffer(int len) {
        Buffer rc = null;
        if (pos < limit) {
            len = Math.min(len, limit - pos);
            rc = new Buffer(buffer, pos, len);
            pos += len;
        }
        return rc;
    }

    public long skip(long len) {
        if (pos < limit) {
            len = Math.min(len, limit - pos);
            if (len > 0) {
                pos += len;
            }
            return len;
        } else {
            return -1;
        }
    }

    public int available() {
        return limit - pos;
    }

    public boolean markSupported() {
        return true;
    }

    public void mark(int markpos) {
        mark = pos;
    }

    public void reset() {
        pos = mark;
    }

    public boolean readBoolean() {
        int ch = (this.buffer[this.pos++] & 0xff);
        return ch != -1 && ch != 0;
    }

    public byte readByte() {
        return (byte) (this.buffer[this.pos++] & 0xff);
    }

    public char readChar() {
        int ch1 = (this.buffer[this.pos++] & 0xff);
        int ch2 = (this.buffer[this.pos++] & 0xff);
        return (char) ((ch1 << 8) + (ch2 << 0));
    }

    public double readDouble() throws IOException {
        return Double.longBitsToDouble(readLong());
    }

    public float readFloat() throws IOException {
        return Float.intBitsToFloat(readInt());
    }

    public void readFully(byte[] b) {
        read(b);
    }

    public void readFully(byte[] b, int off, int len) {
        read(b, off, len);
    }

    public Buffer readBuffer() {
        Buffer result = null;
        int len = readInt();
        if (len > 0) {
            byte[] data = new byte[len];
            readFully(data);
            result = new Buffer(data);

        }
        return result;
    }

    public int readInt() {
        int ch1 = (this.buffer[this.pos++] & 0xff);
        int ch2 = (this.buffer[this.pos++] & 0xff);
        int ch3 = (this.buffer[this.pos++] & 0xff);
        int ch4 = (this.buffer[this.pos++] & 0xff);
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }

    public String readLine() {
        return null;
    }

    public long readLong() {
        read(this.readBuffer, 0, 8);
        return (((long) this.readBuffer[0] << 56) + ((long) (this.readBuffer[1] & 255) << 48)
                + ((long) (this.readBuffer[2] & 255) << 40) + ((long) (this.readBuffer[3] & 255) << 32)
                + ((long) (this.readBuffer[4] & 255) << 24) + ((this.readBuffer[5] & 255) << 16)
                + ((this.readBuffer[6] & 255) << 8) + ((this.readBuffer[7] & 255) << 0));
    }

    public short readShort() {
        int ch1 = (this.buffer[this.pos++] & 0xff);
        int ch2 = (this.buffer[this.pos++] & 0xff);
        return (short) ((ch1 << 8) + (ch2 << 0));
    }

    public String readUTF() throws IOException {
        return DataInputStream.readUTF(this);
    }

    public String readString() {
        String result = null;
        int len = readShort();
        if (len > 0) {
            if (len > this.charArray.length) {
                this.charArray = new char[len];
            }
            for (int i = 0; i < len; i++) {
                this.charArray[i] = (char) (this.buffer[this.pos++] & 0xff);
            }
            result = String.copyValueOf(this.charArray, 0, len);
        } else if (len == 0) {
            result = new String();
        }
        return result;
    }

    public Object readObject() throws IOException {
        return IOUtils.readObject(this);
    }

    public int readUnsignedByte() {
        return (this.buffer[this.pos++] & 0xff);
    }

    public int readUnsignedShort() {
        int ch1 = (this.buffer[this.pos++] & 0xff);
        int ch2 = (this.buffer[this.pos++] & 0xff);
        return (ch1 << 8) + (ch2 << 0);
    }

    public int skipBytes(int n) {
        int total = 0;
        int cur = 0;
        while ((total < n) && ((cur = (int) skip(n - total)) > 0)) {
            total += cur;
        }
        return total;
    }
}
