Andy Clark wrote:
> Aleksander Slominski wrote:
> > another great feature would be ability to "lock" parser
> > buffer content so when i receive multiple character() call-backs
> > i know that XMLString offset and length will be valid
> > as parser will just create bigger buffer to keep larger content.
>
> Sounds like you're asking for a more advanced buffer
> management system built into the parser. I was looking
> at implementing the "don't re-use character buffers"
> feature today and it's not as easy as I had hoped. So...
i have implemented something like this already by wrapping
real reader with special reader that is cumulating input when
asked for. that allows me to keep buffer offsets from
XMLString and uses content in cumulative reader buffer
even though Xerces may reuse its internal buffer
(see below how i implemented CumulativeReader - it
works but is inefficient as it adds extra layer of indirection
and another buffer potentially for all IO operations).
> I may end up implementing a system where the application
> can register a buffer manager and all of the components
> would ask the manager for new buffers, etc. Then, using
> this method, the Xerces2 parser would by default re-use
> buffers but a pull parser could install a new buffer
> manager that always created new buffers.
>
> But I need to do some more thinking on the topic...
that would be great!
i think that this is important feature to tune for perfromance.
thanks,
alek
ps. here is how i implemented cumulative reader:
protected class CumulativeReader extends Reader
{
private Reader source;
private boolean cumulative;
private int bufAbsoluteStart;
private int bufAbsoluteEnd;
private char[] buf = new char[10 * 1024];
private int bufStart;
private int bufEnd;
/** Constructs this reader from another reader. */
public CumulativeReader(Reader reader) {
source = reader;
}
public void setCumulative(boolean value) {
cumulative = value;
if(cumulative) {
char[] newBuf = new char[buf.length + 10 * 1024];
if(bufEnd > bufStart) {
System.arraycopy(buf, bufStart, newBuf, 0, bufEnd - bufStart);
}
bufEnd = bufEnd - bufStart;
bufStart = 0;
}
}
public boolean getCumulative() { return cumulative; }
public char[] getCumulativeBuffer() {
return buf;
}
public int getCumulativeBufferAbsoluteStart() {
return bufAbsoluteStart;
}
public int getCumulativeBufferAbsoluteEnd() {
return bufAbsoluteEnd;
}
public int getCumulativeBufferStart() {
return bufStart;
}
public int getCumulativeBufferEnd() {
return bufEnd;
}
//
// Reader methods
//
// ignore closing
public void close() { }
public int read(char[] ch, int offset, int length)
throws IOException
{
// read form original
int ret = source.read(ch, offset, length);
if(ret > 0) {
if(!cumulative) {
buf = ch;
bufStart = offset;
bufEnd = offset + ret;
bufAbsoluteStart = bufAbsoluteEnd;
} else {
// append ch to buf at bufAbsoluteEnd
int newLen = bufEnd + length;
if(buf.length < newLen) {
char[] newBuf = new char[newLen + 10 * 1024];
System.arraycopy(buf, bufStart, newBuf, 0, bufEnd - bufStart);
bufEnd = bufEnd - bufStart;
bufStart = 0;
}
System.arraycopy(ch, offset, buf, bufEnd, ret);
bufEnd += ret;
}
bufAbsoluteEnd += ret;
}
return ret;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]