Search code examples
javacxfinputstreamattachment

Inputstream starts with a -1


When a file is uploaded with a slow connection (only), using my CXF REST API, I get Couldn't find MIME boundary error. So I debugged the CXF core code to find why. Now I'm looking at this CXF Core code[1].

    private static boolean readTillFirstBoundary(PushbackInputStream pbs, byte[] bp) throws IOException {

        // work around a bug in PushBackInputStream where the buffer isn't
        // initialized
        // and available always returns 0.
        int value = pbs.read();
        pbs.unread(value);
        while (value != -1) {
            value = pbs.read();

When the client to server connection is very slow, the first value of the input stream is almost always -1. That results Couldn't find MIME boundary error at the later on the flow.

If I change the code to skip the first byte if it's -1 like below, it works smoothly.

    private static boolean readTillFirstBoundary(PushbackInputStream pbs, byte[] bp) throws IOException {

        // work around a bug in PushBackInputStream where the buffer isn't
        // initialized
        // and available always returns 0.
        int value = pbs.read();
        if (value == -1) {                <<<<<< if the first byte is -1,
            value = pbs.read();           <<<<<< ignore that and read the  
        }                                 <<<<<< next byte
        pbs.unread(value);
        while (value != -1) {
            value = pbs.read();

Any idea what could be the reason?

[1] https://github.com/apache/cxf/blob/cxf-3.2.8/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java#L264


Solution

  • It turned out to be a tomcat bug[1]. :-/

    Works file with later Tomcat versions.

    [1] https://bz.apache.org/bugzilla/show_bug.cgi?id=64195