/** * Reads the response body from the given input stream. * * @param is the input stream * @param exchange the exchange * @return the response body, can be <tt>null</tt> if no body * @throws IOException is thrown if error reading response body */ public static Object readResponseBodyFromInputStream(InputStream is, Exchange exchange) throws IOException { if (is == null) { return null; } // convert the input stream to StreamCache if the stream cache is not disabled if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) { return is; } else { CachedOutputStream cos = new CachedOutputStream(exchange); IOHelper.copyAndCloseInput(is, cos); return cos.newStreamCache(); } }
private static InputStream doExtractResponseBodyAsStream(InputStream is, Exchange exchange) throws IOException { // As httpclient is using a AutoCloseInputStream, it will be closed when the connection is // closed // we need to cache the stream for it. CachedOutputStream cos = null; try { // This CachedOutputStream will not be closed when the exchange is onCompletion cos = new CachedOutputStream(exchange, false); IOHelper.copy(is, cos); // When the InputStream is closed, the CachedOutputStream will be closed return cos.getWrappedInputStream(); } catch (IOException ex) { // try to close the CachedOutputStream when we get the IOException try { cos.close(); } catch (IOException ignore) { // do nothing here } throw ex; } finally { IOHelper.close(is, "Extracting response body", LOG); } }
@Override public void close() throws IOException { inputStream.close(); cachedOutputStream.close(); }