You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Gary Gregory <ga...@gmail.com> on 2019/11/17 21:15:42 UTC

[httpcore] EntityUtils use case for consuming all until an EOF is caught

Hi All,

I have a use case where calling a web service (which I have no control
over) returns with a 201, indicated a resource was correctly created. The
service also returns compress content that immediately throws an
EOFException when I call EntityUtils.toString(), it just seems to be
'empty' but still compressed since decompression code shows up in the
debugger. I need to generically consume the body of messages from this
service and process whatever it may give back.

To do so, I cloned and hacked a version of EntityUtils.toString() like so
below to always return however much can be consumed and indicate separately
if any blowing up was caught.

Any thoughts on whether this kind use case should be handled by EntityUtils?

        private static String toString(final HttpEntity entity, final
ContentType contentType, AtomicReference<IOException> exceptionRef) throws
IOException {
            final InputStream inputStream = entity.getContent();
            if (inputStream == null) {
                return null;
            }
            try {
                Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
"HTTP entity too large to be buffered in memory");
                int capacity = (int) entity.getContentLength();
                if (capacity < 0) {
                    capacity = DEFAULT_BUFFER_SIZE;
                }
                Charset charset = null;
                if (contentType != null) {
                    charset = contentType.getCharset();
                    if (charset == null) {
                        final ContentType defaultContentType =
ContentType.getByMimeType(contentType.getMimeType());
                        charset = defaultContentType != null ?
defaultContentType.getCharset() : null;
                    }
                }
                if (charset == null) {
                    charset = HTTP.DEF_CONTENT_CHARSET;
                }
                @SuppressWarnings("resource") // Managed by enclosing
InputStream.
                final Reader reader = new InputStreamReader(inputStream,
charset);
                final CharArrayBuffer buffer = new
CharArrayBuffer(capacity);
                final char[] tmp = new char[DEFAULT_BUFFER_SIZE];
                int nCharRead;
                try {
                    while ((nCharRead = reader.read(tmp)) != -1) {
                        buffer.append(tmp, 0, nCharRead);
                    }
                } catch (IOException e) {
                    exceptionRef.set(e);
                }
                return buffer.toString();
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }

Thank you,
Gary