Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Set the HttpConnection State when close request InputStream manually #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nespjin
Copy link

@nespjin nespjin commented Mar 10, 2021

Fixed: Close request InputStream manually if not read request InputStream to end, then send response will throw java.lang.AssertionError

The Issue Sample:

        InputStream byteArrayInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            byteArrayInputStream = mHttpExchange.getRequestBody();
            fileOutputStream = new FileOutputStream(fileDownload);
            byte[] buffer = new byte[LocalNetConfig.BUFFER_SIZE];

            while ((size = byteArrayInputStream.read(buffer)) > 0 && !isCanceled) {
                fileOutputStream.write(buffer, 0, size);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {

                if (byteArrayInputStream != null) {
                    byteArrayInputStream.close();
                }

                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (Exception ignore) {

            }

           // =================================================================
           // If isCanceled = true, the code of below will throw java.lang.AssertionError
          // =================================================================
           final OutputStream responseBody = mHttpExchange.getResponseBody();
            mHttpExchange.sendResponseHeaders(HttpStatusCode.OK, 0);

            String responseBodyString = "";
            final byte[] responseBodyStringBytes = responseBodyString.getBytes(StandardCharsets.UTF_8);
            responseBody.write(responseBodyStringBytes, 0, responseBodyStringBytes.length);
            responseBody.flush();
            responseBody.close();
        }
    }

Fixed: Close request InputStream manually if not read request InputStream to end, then send response will throw java.lang.AssertionError
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant