public StreamCache copy(Exchange exchange) throws IOException { if (byteArrayForCopy == null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(in.available()); IOHelper.copy(in, baos); // reset so that the stream can be reused reset(); // cache the byte array, in order not to copy the byte array in the next call again byteArrayForCopy = baos.toByteArray(); } return new InputStreamCache(byteArrayForCopy); }
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph); GZIPOutputStream zipOutput = new GZIPOutputStream(stream); try { IOHelper.copy(is, zipOutput); } finally { IOHelper.close(is); IOHelper.close(zipOutput); } }
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { InputStream is = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class); GZIPInputStream unzipInput = new GZIPInputStream(is); // Create an expandable byte array to hold the inflated data ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { IOHelper.copy(unzipInput, bos); return bos.toByteArray(); } finally { IOHelper.close(unzipInput); } }
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); } }