com.ibm.commons.util.io.LookAheadReader Maven / Gradle / Ivy
/*
* © 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.Reader;
import java.io.Writer;
import com.ibm.commons.util.FastStringBuffer;
/**
* The lexical reader is a specific reader that cache some data of another reader
* and that permits a lookup of characters.
* @ibm-api
*/
public class LookAheadReader extends Reader {
/**
* Constructor.
* @ibm-api
*/
public LookAheadReader( Reader reader, int bufferLength ) {
this.reader = reader;
this.buffer = new char[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 final int readCDATABase64() throws IOException {
if( count==0 ) {
if( !readData(1) ) {
return -1;
}
}
char c = buffer[position];
if( c==']' ) {
return -1;
}
position++;
count--;
return (int)c;
}
/**
* @ibm-api
*/
public final int readCDATABase64Bytes(byte[] b) throws IOException {
return readCDATABase64Bytes(b,0,b.length);
}
/**
* @ibm-api
*/
public final int readCDATABase64Bytes(byte[] b, int off, int len) throws IOException {
if( count==0 ) {
if( !readData(1) ) {
return -1;
}
}
int max = Math.min(len,count);
for( int i=0; i0 ) {
// 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 {
reader.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 buffer[position+pos];
}
/**
* Read one character ahead.
* @return the character, -1 if not available (not enough character in the reader)
* @ibm-api
*/
public final int getChar() throws IOException {
//return getCharAt(0);
// If the character not is already available, read it
if( count==0 ) {
if( !readData(1) ) {
return -1;
}
}
return buffer[position];
}
/**
* Get a string of # characters.
* Mainly for debugging!
* @ibm-api
*/
public String getStringAhead( int maxChars ) throws IOException {
// Ensure that the buffer is long enough
if( maxChars>count ) {
readData(maxChars);
}
// Get each character
char[] c = new char[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; i0 ) {
w.write( b, 0, r );
} else {
return;
}
} while(true);
}
private Reader reader;
private char[] buffer;
private int count;
private int position;
private boolean eof;
private int index; // Index in the (embedded) stream of the next character
// = number of characters already read
}