All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.commons.util.io.LookAheadInputStream Maven / Gradle / Ivy

The newest version!
/*
 * © Copyright IBM Corp. 2012-2013
 * 
 * Licensed 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 com.ibm.commons.util.io;

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

import com.ibm.commons.util.FastStringBuffer;

/**
 * The lexical inputstream is a specific inputstrean that cache some data of another inputstream
 * and that permits a lookup of characters.
 * @ibm-api
 */
public class LookAheadInputStream extends InputStream {

    /**
     * Constructor.
     * @ibm-api
     */
    public LookAheadInputStream( InputStream is, int bufferLength ) {
        this.is = is;
        this.buffer = new byte[bufferLength];

        // Initialize the reader data
        this.position = 0;
        this.count = 0;
        this.eof = false;
        this.index=0;
    }

    /**
     * @ibm-api
     */
    public int getBufferLength() {
        return buffer.length;
    }

    /**
     * @ibm-api
     */
    public final int read() throws IOException {
        // Need to read the buffer ?
        if( count==0 && !readData(1) ) {
            return -1;
        }

        // Return the next available char
        count--;
        index++;
        return buffer[position++];
    }

    /**
     * @ibm-api
     */
    public int read( byte cbuf[], int off, int len) throws IOException {
        if( len>0 ) {
            // Ensure that the buffer have some data
            if( count==0 && !readData(1) ) {
                return -1;
            }

            // Copy as many data as possible
            int n = Math.min( len, count );
            if( cbuf!=null ) {
                System.arraycopy( buffer, position, cbuf, off, n );
            }
            position += n;
            count -= n;
            index += n;
            return n;
        }
        return 0;
    }

    /**
     * @ibm-api
     */
    public long skip(long len) throws IOException {
        if( len>0 ) {
            if( len==1 ) {
                read(); return 1;
            } else {
                return read( null, 0, (int)len );
            }
        }
        return 0;
    }

    /**
     * @ibm-api
     */
    public void close() throws IOException {
        is.close();
    }

    /**
     * @ibm-api
     */
    public int getCurrentIndex() {
        return index;
    }

    private boolean readData(int minData) throws IOException {
        // Check for the end of the reader
        if( eof ) {
            return false;
        }

        // Move the existing data at the beggining of the buffer
        if( count>0 ) {
            System.arraycopy( buffer, position, buffer, 0, count );
        }
        position = 0;

        // Read as many data as possible
        while(count=count ) {
            if( !readData(pos+1) ) {
                return -1;
            }
        }

        // And return it
        //if( (position+pos)>=buffer.length ) {
        //    return -1;
        //}
        return ((int)buffer[position+pos]) & 0xFF;
    }

    /**
     * Read one byte ahead.
     * @return the character, -1 if not available (not enough character in the reader)
     * @ibm-api
     */
    public final int getByte() throws IOException {
        //return getCharAt(0);
        // If the character not is already available, read it
        if( count==0 ) {
            if( !readData(1) ) {
                return -1;
            }
        }
        return ((int)buffer[position]) & 0xFF;
    }

    /**
     * Get a string of # characters.
     * Mainly for debugging!
     * @ibm-api
     */
    public byte[] getByteAhead( int maxChars ) throws IOException {
        // Ensure that the buffer is long enough
        if( maxChars>count ) {
            readData(maxChars);
        }

        // Get each character
        byte[] c = new byte[Math.min(maxChars,count)];
        for( int i=0; icount ) {
            if( !readData(length) ) {
                return false;
            }
        }

        // Compare each character
        for( int i=0; icount ) {
            if( !readData(length) ) {
                return false;
            }
        }

        // Compare each character
        for( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy