You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Bhathiya Jayasekara (Jira)" <ji...@apache.org> on 2020/05/09 05:32:00 UTC

[jira] [Commented] (CXF-8279) Attachment inputstream starts with -1 for slow connections

    [ https://issues.apache.org/jira/browse/CXF-8279?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17103115#comment-17103115 ] 

Bhathiya Jayasekara commented on CXF-8279:
------------------------------------------

This change also works perfectly fine. So it seems like a race condition or a timeout caused this.

Hope this will help.
{code:java}
    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.
        try {                                       <<<<<<<<<<
            Thread.sleep(500);                      <<<<<<<<<<
        } catch (InterruptedException e) {          <<<<<<<<<<
            e.printStackTrace();                    <<<<<<<<<<
        }                                           <<<<<<<<<<
        int value = pbs.read();
        pbs.unread(value);
        while (value != -1) {
{code}

> Attachment inputstream starts with -1 for slow connections
> ----------------------------------------------------------
>
>                 Key: CXF-8279
>                 URL: https://issues.apache.org/jira/browse/CXF-8279
>             Project: CXF
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 3.2.8
>            Reporter: Bhathiya Jayasekara
>            Priority: Major
>
> 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 out why. Now I'm looking at this code[1].
> {code:java}
>     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();
> {code}
> When the client to server connection is very slow, the first {{read()}} of the input stream almost always is -1. That results {{Couldn't find MIME boundary error}} at the later on the flow.
> If I change the code (just to debug) to skip the first byte when it's -1, like below, it works smoothly. See the 3 lines shown with "<<<<<<<". That means the data I expect is available after -1.  
> {code:java}
>     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();
> {code}
> Another important thing is this happens only for non-localhost and https connections.  
> Any idea what could be the reason for giving -1 for the first read always when the connection is slow?
> [1] https://github.com/apache/cxf/blob/cxf-3.2.8/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java#L264



--
This message was sent by Atlassian Jira
(v8.3.4#803005)