输入流以 -1 开头

Inputstream starts with a -1

当使用我的 CXF REST API 使用慢速连接(仅)上传文件时,我收到 Couldn't find MIME boundary 错误。于是调试了CXF核心代码,寻找原因。现在我正在查看此 CXF 核心代码 [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();

当客户端到服务器的连接很慢时,输入流的第一个值几乎总是-1。这会导致 Couldn't find MIME boundary 稍后在流程中出现错误。

如果我更改代码以跳过第一个字节(如果它是 -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();
        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();

知道可能是什么原因吗?

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

原来是 tomcat 错误[1]。 :-/

Tomcat 以后版本的工作文件。

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