protected int copyStream(InputStream is, OutputStream os, int bufferSize) throws IOException { try { // copy stream, and must flush on each write as etc Jetty has better performance when // flushing after writing to its servlet output stream return IOHelper.copy(is, os, bufferSize, true); } finally { IOHelper.close(os, is); } }
@Override public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception { if (usingIterator) { ZipIterator zipIterator = new ZipIterator(exchange, inputStream); zipIterator.setAllowEmptyDirectory(allowEmptyDirectory); return zipIterator; } else { ZipInputStream zis = new ZipInputStream(inputStream); OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange); try { ZipEntry entry = zis.getNextEntry(); if (entry != null) { exchange.getOut().setHeader(FILE_NAME, entry.getName()); IOHelper.copy(zis, osb); } entry = zis.getNextEntry(); if (entry != null) { throw new IllegalStateException("Zip file has more than 1 entry."); } return osb.build(); } finally { IOHelper.close(zis, osb); } } }
@Override public void writeTo(OutputStream os) throws IOException { // must remember current index so we can reset back to it after the copy int idx = buffer.readerIndex(); try { buffer.resetReaderIndex(); IOHelper.copy(this, os); } finally { buffer.readerIndex(idx); } }
private void marshalStreaming(Exchange exchange, Object graph, OutputStream stream) throws Exception { InputStream decoded = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph); Base64OutputStream base64Output = new Base64OutputStream(stream, true, lineLength, lineSeparator); try { IOHelper.copy(decoded, base64Output); } finally { IOHelper.close(decoded, base64Output); } }
@Override public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception { String filename; String filepath = exchange.getIn().getHeader(FILE_NAME, String.class); if (filepath == null) { // generate the file name as the camel file component would do filename = filepath = StringHelper.sanitize(exchange.getIn().getMessageId()); } else { filename = Paths.get(filepath).getFileName().toString(); // remove any path elements } ZipOutputStream zos = new ZipOutputStream(stream); if (preservePathElements) { createZipEntries(zos, filepath); } else { createZipEntries(zos, filename); } InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph); try { IOHelper.copy(is, zos); } finally { IOHelper.close(is, zos); } String newFilename = filename + ".zip"; exchange.getOut().setHeader(FILE_NAME, newFilename); }
IOHelper.copy(is, tos); } finally { tos.closeArchiveEntry();
private Object transformToStreamCacheOrByteArray(Exchange exchange, InputStream is) throws CryptoCmsException { // the input stream must be completely read, outherwise you will get // errors when your use as next component the file adapter. OutputStreamBuilder output = OutputStreamBuilder.withExchange(exchange); try { // data can be null in the case of explicit Signed Data if (is != null) { try { IOHelper.copy(is, output); } finally { IOHelper.close(is); } } LOG.debug("CMS Enveloped Data decryption successful"); return output.build(); } catch (IOException e) { throw new CryptoCmsException("Error during reading the unencrypted content of the enveloped data object", e); } finally { IOHelper.close(output); } }
@Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { if (usingIterator) { TarIterator tarIterator = new TarIterator(exchange, stream); tarIterator.setAllowEmptyDirectory(allowEmptyDirectory); return tarIterator; } else { BufferedInputStream bis = new BufferedInputStream(stream); TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis); OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange); try { TarArchiveEntry entry = tis.getNextTarEntry(); if (entry != null) { exchange.getOut().setHeader(FILE_NAME, entry.getName()); IOHelper.copy(tis, osb); } entry = tis.getNextTarEntry(); if (entry != null) { throw new IllegalStateException("Tar file has more than 1 entry."); } return osb.build(); } finally { IOHelper.close(osb, tis, bis); } } }
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { InputStream berOut = null; if (usingIterator) { if (clazzName != null) { Class<?> clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(clazzName); encodeGenericTypeObject(exchange, clazz, stream); return; } Object record = exchange.getIn().getBody(); if (record instanceof ASN1Primitive) { ASN1Primitive asn1Primitive = ObjectHelper.cast(ASN1Primitive.class, record); berOut = new ByteArrayInputStream(asn1Primitive.getEncoded()); } else if (record instanceof byte[]) { berOut = new ByteArrayInputStream(ObjectHelper.cast(byte[].class, record)); } } else { byte[] byteInput = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, exchange, graph); berOut = new ByteArrayInputStream(byteInput); } try { IOHelper.copy(berOut, stream); } finally { IOHelper.close(berOut, stream); } }
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 protected int poll() throws Exception { shutdownRunningTask = null; pendingExchanges = 0; Queue<Exchange> queue = new LinkedList<>(); String directory = endpoint.getDirectory(); ListContainerOptions opt = new ListContainerOptions(); if (!Strings.isNullOrEmpty(directory)) { opt = opt.inDirectory(directory); } for (StorageMetadata md : blobStore.list(container, opt.maxResults(maxMessagesPerPoll).recursive())) { String blobName = md.getName(); if (md.getType().equals(StorageType.BLOB)) { if (!Strings.isNullOrEmpty(blobName)) { InputStream body = JcloudsBlobStoreHelper.readBlob(blobStore, container, blobName); if (body != null) { Exchange exchange = endpoint.createExchange(); CachedOutputStream cos = new CachedOutputStream(exchange); IOHelper.copy(body, cos); exchange.getIn().setBody(cos.newStreamCache()); exchange.setProperty(JcloudsConstants.BLOB_NAME, blobName); queue.add(exchange); } } } } return queue.isEmpty() ? 0 : processBatch(CastUtils.cast(queue)); }
protected OutputStreamBuilder getOutputStream(CMSSignedDataParser sp, Exchange exchange) throws Exception { // get the InputStream with the plain data InputStream data; try { data = sp.getSignedContent().getContentStream(); } catch (NullPointerException e) { // nullpointer exception is // thrown when the signed content // is missing throw getContentMissingException(e); } // the input stream must be completely read, otherwise the signer // info is not available! OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange); try { // data can be null in the case of explicit Signed Data if (data != null) { try { IOHelper.copy(data, osb); } finally { IOHelper.close(data); } } } catch (IOException e) { throw new CryptoCmsException("Error during reading the signed content of the signed data object", e); } return osb; }
IOHelper.copy(is, encryptingStream);