天天看點

BufferedInputStream.java

package textconvert;

import java.io.IOException;

import java.io.InputStream;

public class BufferedInputStream extends InputStream{

    private InputStream inStream;

    protected byte[] buffer = null;

    private int buffer_index = 0;

    private int buffer_size = 0;

    private int markpos;

    private ConstByteArray byteArray = null;

    private boolean byteArrayLocked = false;

    private boolean isInStreamEOF = false;

    public BufferedInputStream(InputStream inStream){

        this(inStream, 8192);

    }

    public BufferedInputStream(InputStream inStream, int size){

        this.inStream = inStream;

        if(size % 2 != 0){

            size ++;

        }

        this.buffer = new byte[size];

        this.byteArray = new ConstByteArray(this.buffer, 0, size);

    }

    public final int read() throws IOException{

        if(buffer_index>=buffer_size){           

            if(buffer_getNext()==-1)

                return -1;

        }

        return buffer[buffer_index++] & 0xff;   

    }

    public final ConstByteArray findBytePeekLock(byte b, int maxCount) throws IOException{

        byteBufferPeekLock(maxCount);

        byteBufferUnlock(0);

        int i;

        for(i=this.buffer_index; i<this.buffer_size; i++){

            if(buffer[i] == b){

                break;

            }

        }

        if(i == this.buffer_size)

            return null;

        return byteBufferPeekLock(i - this.buffer_index);

    }

    public final ConstByteArray byteBufferPeekLock(int size) throws IOException{

        if(size > this.buffer.length/2)

            throw new IOException("error:size > buffer.length/2");

        if(!this.isInStreamEOF && buffer_size - buffer_index -1 <size){

            this.moveBufferAndFill();

        }   

        int len = Math.min(size, buffer_size - buffer_index -1);

        this.byteArray.resetSize(this.buffer_index,len);

        byteArrayLocked = true;

        return this.byteArray;

    }

    public final void byteBufferUnlock(int skipByteCount) throws IOException{

        byteArrayLocked = false;

        this.skip(skipByteCount);

    }

    public void mark(int readlimit){

        if(readlimit>buffer.length/2)

            return;

        try {

            if(buffer_index>buffer.length/2 && readlimit > buffer_size - buffer_index - 1){               

                moveBufferAndFill();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        markpos = buffer_index;

    }

    public boolean markSupported() {

        return true;

    }

    public void reset(){

        buffer_index = markpos;

    }

    public void close(){

        buffer = null;

    }

    public final int getBufferSize(){

        return this.buffer.length;

    }

    public int matchBytesPeek(byte[] bytes, int peekLength) throws IOException{       

        int halfCap = buffer.length /2;

        if(bytes.length >  halfCap)

            return -1;

        if(buffer_index >= halfCap &&

                buffer_size - buffer_index -1 < bytes.length){

            if(moveBufferAndFill()==-1)

                return -1;

                }

        int maxIndex = buffer_index + peekLength;

        for(int i=buffer_index; i<maxIndex; i++){

            int j;

            for( j=0; j<bytes.length; j++){

                if(buffer[i + j] != bytes[j]){

                    break;

                }

            }

            if(j==bytes.length)

                return i;

        }

        return -1;

    }

    public void skip(int n) throws IOException{

        if(n <= buffer_size - buffer_index)   

            buffer_index += n;

        else{

            super.skip(n);

            buffer_size = 0;

            buffer_index = 0;

        }

    }

    private int moveBufferAndFill() throws IOException{

        if(this.byteArrayLocked)

            throw new IOException("buffer locked!");

        int len = -1;

        if(buffer_index >= buffer.length /2){

            try {

                System.arraycopy(buffer, buffer_index, buffer, 0, buffer_size - buffer_index - 1);

                len = this.inStream.read(buffer, buffer_size, buffer.length-buffer_size);

                if(len!=-1)

                    buffer_size += len;

                else

                    this.isInStreamEOF = true;

                buffer_index = 0;               

            } catch (IOException e) {

                e.printStackTrace();

            }           

        }   

        return len;

    }

    private int buffer_getNext() throws IOException{

        if(this.byteArrayLocked)

            throw new IOException("buffer locked!");

        int len = -1;

        try{

            len = this.inStream.read(buffer, 0, buffer.length);

            if(len != -1)

                buffer_size = len;

            else

                this.isInStreamEOF = true;

            buffer_index = 0;

        }catch(Exception e){

            e.printStackTrace();

        }

        return len;

    }

}