views:

239

answers:

1

I'm reading XML data from the HttpServletRequest in my servlets doPost() and passing the Reader from req.getReader() to a JAXB unmarshaller. I've tried a couple of different input XMLs but I always get this exception.

SEVERE: Servlet.service() for servlet RESTPhotoAdmin threw exception
java.lang.ArrayIndexOutOfBoundsException: 8192
        at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:491)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2672)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:195)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
        at com.cantorva.gigcalendar.servlets.rest.RESTPhotoAdmin.doPost(RESTPhotoAdmin.java:41)

This only happens on my Live web server not on the local development machine. Whatever the input is (though I only tried approx 90k inputs) the message portion of the stack trace always reads "8192". I looked up XMLEntityScanner.java line 491 and can see a buffer being read, and the buffer is indeed an array. It makes sense to me that an 8k buffer can't be read using a zero based index of 8192 so this must be a bug - but what's the cause and what can I do?


I'm asking this having already found the answer through lots of detective work. I wanted to save the next poor guy the hassle since Apache have already refused to fix it.

A: 

I found an obscure bug XERCESJ-1275 which is itself a duplicate of XERCESJ-1015. The report doesn't mention my stack trace, but does mention a ArrayIndexOutOfBoundsException. The clue was the comment about 0 being a valid (or somewhat valid) response from a Reader's read method, but not from an InputStream according to the JavaDoc. XMLEntityScanner assumes that 0 can never occur - thus the error.

Michael Glavassevich interprets the documentation differently and marks the defect invalid, but the work around is simply to supply the Unmarshaller an InputStream using req.getInputStream() rather than a Reader and cross your fingers that some obscure part of the implementation is fulfilling the different interface contracts more perfectly - which they do.

Simon Gibbs