I have a socket feeding into a sax parser, formatted as a ISO 8859/1 stream. Every so often there is an invalid character, and I get a SAXParseException with a row and column where that happened, so I need to see what the data is at that point (or more importantly log it).
Originally the lines that processed the data were:
InputSource is = new InputSource(new InputStreamReader(socket.getInputStream(), "ISO8859_1"));
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(new ResponseParseHandler(etc, id));
reader.parse(is);
Problem is that I can’t get the data after the event of this happening, so I changed it to read into a large byte buffer, convert it to a string and parse that data with a StringReader. Unfortunately the data coming from the socket is spread out in small chunks over a long time, so it will start with the root tag when it first connects, but then there will be thousands of separate messages without a closing tag.
Because I am parsing these strings individually when they come in the first one has an error that it doesn’t have a closing tag, and the following ones error as they don’t have a base tag. This doesn’t happen with the socket as I assume the stream is still open
Presumably I can feed these strings to another reader / writer but it seems to be getting really complicated just to find out what the block of data was at the time of the error.
Is there something really simple I am missing here?
The last time I had a problem similar to this, I solved it with a
SplittingWriter. This was a decorator style class around two otherWriters, and when something “wrote” to theSplittingWriterit simply delegated the write call to both of its two underlyingWriters.In your case, you would want something like a
SplittingInputStreamReader, which would implementInputStreamReaderand which you would pass in toInputSourceinstead of theInputStreamReaderyou are using at the moment.In its constructor the
SplittingInputStreamReaderwould take your currentInputStreamReaderand some other object, lets call itFoo. The implementation of thereadmethods onSplittingInputStreamReaderwould then delegate the read calls to the underlyingInputStreamReader, push the results of those calls toFoo, and then return the result of those calls back to the thing that called it. So your implementation of theint read()method would be something like:That way, as you read via the
SplittingInputStreamReader, you also write toFoo, allowing you to see where the write stopped assuming you gaveFooa decent interface. In the end, after implementingSplittingInputStreamReaderandFoo, your code would look something like this: