Search in sources :

Example 46 with InputStream

use of java.io.InputStream in project camel by apache.

the class CachedOutputStreamTest method testCacheStreamToFileAndCloseStream.

public void testCacheStreamToFileAndCloseStream() throws Exception {
    context.start();
    CachedOutputStream cos = new CachedOutputStream(exchange);
    cos.write(TEST_STRING.getBytes("UTF-8"));
    File file = new File("target/cachedir");
    String[] files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertTrue("The file name should start with cos", files[0].startsWith("cos"));
    StreamCache cache = cos.newStreamCache();
    assertTrue("Should get the FileInputStreamCache", cache instanceof FileInputStreamCache);
    String temp = toString((InputStream) cache);
    ((InputStream) cache).close();
    files = file.list();
    assertEquals("we should have a temp file", 1, files.length);
    assertEquals("Cached a wrong file", temp, TEST_STRING);
    exchange.getUnitOfWork().done(exchange);
    try {
        cache.reset();
        // The stream is closed, so the temp file is gone.
        fail("we expect the exception here");
    } catch (Exception exception) {
    // do nothing
    }
    files = file.list();
    assertEquals("we should have no temp file", 0, files.length);
    IOHelper.close(cos);
}
Also used : StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) File(java.io.File) IOException(java.io.IOException)

Example 47 with InputStream

use of java.io.InputStream in project camel by apache.

the class CacheInputStreamInDeadLetterIssue520Test method testSendingInputStream.

public void testSendingInputStream() throws Exception {
    InputStream message = new ByteArrayInputStream("<hello>Willem</hello>".getBytes());
    sendingMessage(message);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 48 with InputStream

use of java.io.InputStream in project camel by apache.

the class BlobServiceProducer method updateAppendBlob.

private void updateAppendBlob(Exchange exchange) throws Exception {
    CloudAppendBlob client = BlobServiceUtil.createAppendBlobClient(getConfiguration());
    configureCloudBlobForWrite(client);
    BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange);
    if (opts.getAccessCond() == null) {
        // Default: do not reset the blob content if the blob already exists
        opts.setAccessCond(AccessCondition.generateIfNotExistsCondition());
    }
    Boolean appendBlobCreated = exchange.getIn().getHeader(BlobServiceConstants.APPEND_BLOCK_CREATED, Boolean.class);
    if (Boolean.TRUE != appendBlobCreated) {
        doCreateAppendBlob(client, opts, exchange);
    }
    InputStream inputStream = getInputStreamFromExchange(exchange);
    try {
        client.appendBlock(inputStream, -1, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
    } finally {
        closeInputStreamIfNeeded(inputStream);
    }
}
Also used : CloudAppendBlob(com.microsoft.azure.storage.blob.CloudAppendBlob) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Example 49 with InputStream

use of java.io.InputStream in project camel by apache.

the class BlobServiceProducer method uploadPageBlob.

private void uploadPageBlob(Exchange exchange) throws Exception {
    LOG.trace("Updating a page blob [{}] from exchange [{}]...", getConfiguration().getBlobName(), exchange);
    CloudPageBlob client = BlobServiceUtil.createPageBlobClient(getConfiguration());
    configureCloudBlobForWrite(client);
    BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange);
    if (opts.getAccessCond() == null) {
        // Default: do not reset the blob content if the blob already exists
        opts.setAccessCond(AccessCondition.generateIfNotExistsCondition());
    }
    Boolean pageBlobCreated = exchange.getIn().getHeader(BlobServiceConstants.PAGE_BLOCK_CREATED, Boolean.class);
    if (Boolean.TRUE != pageBlobCreated) {
        doCreatePageBlob(client, opts, exchange);
    }
    InputStream inputStream = getInputStreamFromExchange(exchange);
    doUpdatePageBlob(client, inputStream, opts, exchange);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CloudPageBlob(com.microsoft.azure.storage.blob.CloudPageBlob)

Example 50 with InputStream

use of java.io.InputStream in project camel by apache.

the class BlobServiceProducer method getInputStreamFromExchange.

private InputStream getInputStreamFromExchange(Exchange exchange) throws Exception {
    Object blobObject = exchange.getIn().getMandatoryBody();
    InputStream inputStream = null;
    if (blobObject instanceof String) {
        String charset = getCharsetName(exchange);
        inputStream = new ByteArrayInputStream(((String) blobObject).getBytes(charset));
    } else if (blobObject instanceof InputStream) {
        inputStream = (InputStream) blobObject;
    } else if (blobObject instanceof File) {
        inputStream = new FileInputStream((File) blobObject);
    } else {
        throw new IllegalArgumentException("Unsupported blob type:" + blobObject.getClass().getName());
    }
    return inputStream;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InputStream (java.io.InputStream)12635 IOException (java.io.IOException)4626 ByteArrayInputStream (java.io.ByteArrayInputStream)2818 Test (org.junit.Test)2722 FileInputStream (java.io.FileInputStream)2443 File (java.io.File)1750 OutputStream (java.io.OutputStream)1173 InputStreamReader (java.io.InputStreamReader)1091 BufferedInputStream (java.io.BufferedInputStream)1075 URL (java.net.URL)1032 FileOutputStream (java.io.FileOutputStream)821 ByteArrayOutputStream (java.io.ByteArrayOutputStream)782 BufferedReader (java.io.BufferedReader)742 FileNotFoundException (java.io.FileNotFoundException)587 Properties (java.util.Properties)580 ArrayList (java.util.ArrayList)562 HttpURLConnection (java.net.HttpURLConnection)424 HashMap (java.util.HashMap)397 GZIPInputStream (java.util.zip.GZIPInputStream)342 Metadata (org.apache.tika.metadata.Metadata)319