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);
}
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);
}
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);
}
}
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);
}
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;
}
Aggregations