package com.xqbase.baiji.io;

import com.xqbase.baiji.util.ByteBufferInputStream;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

/**
 * A non-buffering version of {@link BinaryDecoder}.
 * <p/>
 * This implementation will not read-ahead from the provided InputStream
 * beyond the minimum required to service the API requests.
 *
 * @see Encoder
 *
 * @author Tony He
 */
public class DirectBinaryDecoder extends BinaryDecoder {

    private InputStream in;
    private ByteReader byteReader;

    public DirectBinaryDecoder(InputStream in) {
        super();
    }

    private DirectBinaryDecoder configure(InputStream in) {
        if (null == in) {
            throw new NullPointerException("InputStream cannot be null");
        }
        this.in = in;
        this.byteReader = (in instanceof ByteBufferInputStream)
                ? new ReuseByteReader((ByteBufferInputStream) in)
                : new ByteReader();
        return this;
    }

    private class ByteReader {

    }

    private class ReuseByteReader extends ByteReader {

        private final ByteBufferInputStream bbi;

        public ReuseByteReader(ByteBufferInputStream bbi) {
            this.bbi = bbi;
        }
    }

    @Override
    public boolean readBoolean() throws IOException {
        int n = in.read();
        if (n < 0) {
            throw new EOFException();
        }
        return n == 1;
    }

    @Override
    public int readInt() throws IOException {
        int n = 0;
        int b;
        int shift = 0;
        do {

        } while (shift < 32);
        return super.readInt();
    }

    @Override
    public long readLong() throws IOException {
        return super.readLong();
    }

    @Override
    public float readFloat() throws IOException {
        return super.readFloat();
    }

    @Override
    public double readDouble() throws IOException {
        return super.readDouble();
    }

    @Override
    public byte[] readBytes() throws IOException {
        return super.readBytes();
    }
}

@@ -1481,32 +1481,75 @@
 ws IOException %7B
+ // adapt variable-length, zig-zag encoding
 %0A        int n =
@@ -1603,16 +1603,361 @@
    do %7B%0A
+            b = in.read();%0A            if (b %3E= 0) %7B%0A                n %7C= (b & 0x7F) %3C%3C shift;%0A                if ((b & 0x80) == 0) %7B // no more data%0A                    return (n %3E%3E%3E 1) %5E -(n & 1); // back to two's-complement%0A                %7D%0A            %7D else %7B%0A                throw new EOFException();%0A            %7D%0A            shift += 7;
 %0A       
@@ -1991,28 +1991,30 @@
     
-return super.readInt
+throw new EOFException
 ();%0A
@@ -2085,38 +2085,503 @@
 on %7B
-%0A        return super.readL
+ // adapt variable-length, zig-zag encoding%0A        long n = 0L;%0A        int b;%0A        int shift = 0;%0A        do %7B%0A            b = in.read();%0A            if (b %3E= 0) %7B%0A                n %7C= (b & 0x7FL) %3C%3C shift;%0A                if ((b & 0x80) == 0) %7B // no more data%0A                    return (n %3E%3E%3E 1) & -(n & 1);%0A                %7D%0A            %7D else %7B%0A                throw new EOFException();%0A            %7D%0A            shift += 7;%0A        %7D while (shift %3C 64);%0A        throw new EOFExcepti
 on
-g
 ();%0A

add the int&long decode methods...