Assuming you are responsible for placing documents into the stream in the first place should be easy to delimit the documents in some fashion. For example:
// Any value that is invalid for an XML character will do.
static final char DOC_TERMINATOR=4;
BOOL addDocumentToStream(BufferedWriter streamOut, char xmlData[])
{
streamOut.write(xmlData);
streamOut.write(DOC_TERMINATOR);
}
Then when reading from the stream read into a array until DOC_TERMINATOR is encountered.
char *getNextDocuument(BufferedReader streamIn)
{
StringBuffer buffer = new StringBuffer();
int character;
while (true)
{
character = streamIn.read();
if (character == DOC_TERMINATOR)
break;
buffer.append(character);
}
return buffer.toString().toCharArray();
}
Since 4 is an invalid character value you won't encounter except where you explicitly add it. Thus allowing you to split the documents. Now just wrap the resuling char array for input into SAX and your good to go.
...
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
...
while (true)
{
char xmlDoc = getNextDocument(streamIn);
if (xmlDoc.length == 0)
break;
InputSource saxInputSource = new InputSource(new CharArrayReader(xmlDoc));
xmlReader.parse(saxInputSource);
}
...
Note that the loop terminates when it gets a doc of length 0. This means that you should either add a second DOC_TERMINATOR after the last document of you need to add something to detect the end of the stream in getNextDocument().