I've been using SAX2XMLReader for quite some time to read an XML file.
I just pass the name of the file to the parse() function. Now, I have a
new requirement: I have to optionally be able to read a file which is
encrypted, the underlying contents of which are XML.
My first thought was perhaps there is a way to pass memory, instead of a
filename, to parse(), and I could just decrypt the contents and pass
them to parse() as I do so. In fact, there is a version of parse() that
takes an "InputSource", and there's a class derived from InputSource
called "MemBufInputSource". So this looked promising.
But then I realized that the files I'm decoding are potentially huge.
MemBufInputSource seems to require that the buffer be completely
populated before passing it off to parse(). It would be much, much
nicer if, instead, there were some InputSource (or something) that would
have some callback function that would automatically ask for more data
whenever parse() has run out of stuff to parse. That way, I could read
and decrypt the file in reasonably-sized chunks (the crypto algorithm in
use supports streaming decryption).
So, first of all, is there such a thing? In case it matters, I am using
Xerces 2.6; I could upgrade if absolutely necessary, but I would prefer
not to at this point.
If not, I'm guessing I can build my own InputSource-derived class. But
the documentation is not entirely clear.
Obviously I have to implement the pure virtual function, makeStream().
This is supposed to return a BinInputStream. This has two pure virtual
functions, curPos() and readBytes(). Neither of these has informative
documentation, at least not that I have found.
I assume that, in curPos(), I am supposed to return the total number of
bytes that have been read (using readBytes()) so far. Correct?
Then, readBytes(): I assume I am supposed to return the number of bytes
read during this call. Correct?
What if an error occurs (such as me being unable to read from the
underlying file)? I throw... something? Or what?
When there are no more bytes to read (and not because of an error
condition), I assume I am supposed to return zero. Correct? Or do I
throw something? Or what?
Any help, regarding these questions specifically, or a better way to
accomplish what I want, or some hints in general, would be appreciated.
Thanks.
- A streamed InputSource? Robert William Vesterman
-