}
}
protected void fillBuf() throws XMLStreamException, EOFException {
if(reader == null) throw new XMLStreamException(
"reader must be set before parsing is started");
// see if we are in compaction area
if(bufEnd > bufSoftLimit) {
// expand buffer it makes sense!!!!
boolean compact = bufStart > bufSoftLimit;
boolean expand = false;
if(!compact) {
//freeSpace
if(bufStart < buf.length / 2) {
// less then half buffer available for compacting --> expand instead!!!
expand = true;
} else {
// at least half of buffer can be reclaimed --> worthwhile effort!!!
compact = true;
}
}
// if buffer almost full then compact it
if(compact) {
//TODO: look on trashing
// //assert bufStart > 0
System.arraycopy(buf, bufStart, buf, 0, bufEnd - bufStart);
if(TRACE_SIZING) System.out.println("fillBuf() compacting "+bufStart);
} else if(expand) {
int newSize = 2 * buf.length;
char newBuf[] = new char[ newSize ];
if(TRACE_SIZING) System.out.println("fillBuf() "+buf.length+" => "+newSize);
System.arraycopy(buf, bufStart, newBuf, 0, bufEnd - bufStart);
buf = newBuf;
if(bufLoadFactor > 0) {
bufSoftLimit = ( bufLoadFactor * buf.length ) /100;
}
} else {
throw new XMLStreamException("internal error in fillBuffer()");
}
bufEnd -= bufStart;
pos -= bufStart;
posStart -= bufStart;
posEnd -= bufStart;
bufAbsoluteStart += bufStart;
bufStart = 0;
}
// at least one character must be read or error
int room = (buf.length - bufEnd);
int len = (room > READ_CHUNK_SIZE) ? READ_CHUNK_SIZE : room;
int ret;
try {
ret = reader.read(buf, bufEnd, len);
} catch (IOException ioe) {
throw new XMLStreamException(ioe);
}
if(ret > 0) {
bufEnd += ret;
return;
}
if(ret == -1) {
throw new EOFException("no more data available");
} else {
throw new XMLStreamException("error reading input, returned "+ret);
}
}