Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete redundant response.disconnect() from moveFolderToUser() #485

Merged
merged 1 commit into from
Jan 2, 2018

Conversation

rlatntjr
Copy link
Contributor

Summary: please comment out line 407 of the BoxUser.java class in the SDK and attempt again:

response.disconnect();

Explanation: Line 405 of BoxUser.java, which is inside BoxUser.moveFolderToUser(), includes this call:

JsonObject responseJSON = JsonObject.readFrom(response.getJSON());

response.jgetJSON() is defined in the class BoxJSONResponse.java.

     * Gets the body of the response as a JSON string. When this method is called, the response's body will be read and
     * the response will be disconnected, meaning that the stream returned by {@link #getBody} can no longer be used.
     * @return the body of the response as a JSON string.
     */
    public String getJSON() {
        if (this.json != null) {
            return this.json;
        }

        InputStreamReader reader = new InputStreamReader(this.getBody(), StandardCharsets.UTF_8);
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[BUFFER_SIZE];

        try {
            int read = reader.read(buffer, 0, BUFFER_SIZE);
            while (read != -1) {
                builder.append(buffer, 0, read);
                read = reader.read(buffer, 0, BUFFER_SIZE);
            }

            this.disconnect();
            reader.close();
        } catch (IOException e) {
            throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
        }
        this.json = builder.toString();
        return this.json;
    }

Note that this method also calls “this.disconnect()“. Since this happens twice (once in response.getJSON(), once in line 407) the second execution throws this exception from BoxAPIResponse.java:

        if (this.connection == null) {
            return;
        }

        try {
            if (this.rawInputStream == null) {
                this.rawInputStream = this.connection.getInputStream();
            }

            // We need to manually read from the raw input stream in case there are any remaining bytes. There's a bug
            // where a wrapping GZIPInputStream may not read to the end of a chunked response, causing Java to not
            // return the connection to the connection pool.
            byte[] buffer = new byte[BUFFER_SIZE];
            int n = this.rawInputStream.read(buffer);
            while (n != -1) {
                n = this.rawInputStream.read(buffer);
            }
            this.rawInputStream.close();

            if (this.inputStream != null) {
                this.inputStream.close();
            }
        } catch (IOException e) {
            throw new BoxAPIException("Couldn't finish closing the connection to the Box API due to a network error or "
                + "because the stream was already closed.", e);
        }```

@boxcla
Copy link

boxcla commented Nov 17, 2017

Hi @jasonkimchi, thanks for the pull request. Before we can merge it, we need you to sign our Contributor License Agreement. You can do so electronically here: http://opensource.box.com/cla

Once you have signed, just add a comment to this pull request saying, "CLA signed". Thanks!

@rlatntjr
Copy link
Contributor Author

CLA Signed!

@boxcla
Copy link

boxcla commented Nov 17, 2017

Verified that @jasonkimchi has just signed the CLA. Thanks, and we look forward to your contribution.

Copy link

@carycheng carycheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me @jasonkimchi, feel free to merge it whenever, sorry for the delay, combing through the backlog now, will definitely be MUCH quicker going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants