use of java.io.IOException in project camel by apache.
the class HBaseHelper method toBytes.
public static byte[] toBytes(Object obj) {
if (obj instanceof byte[]) {
return (byte[]) obj;
} else if (obj instanceof Byte) {
return Bytes.toBytes((Byte) obj);
} else if (obj instanceof Short) {
return Bytes.toBytes((Short) obj);
} else if (obj instanceof Integer) {
return Bytes.toBytes((Integer) obj);
} else if (obj instanceof Long) {
return Bytes.toBytes((Long) obj);
} else if (obj instanceof Double) {
return Bytes.toBytes((Double) obj);
} else if (obj instanceof String) {
return Bytes.toBytes((String) obj);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
} catch (IOException e) {
LOG.warn("Error while serializing object. Null will be used.", e);
return null;
} finally {
IOHelper.close(oos);
IOHelper.close(baos);
}
}
}
use of java.io.IOException in project camel by apache.
the class HdfsProducer method doProcess.
void doProcess(Exchange exchange) throws Exception {
Object body = exchange.getIn().getBody();
Object key = exchange.getIn().getHeader(HdfsHeader.KEY.name());
// if an explicit filename is specified, close any existing stream and append the filename to the hdfsPath
if (exchange.getIn().getHeader(Exchange.FILE_NAME) != null) {
if (ostream != null) {
IOHelper.close(ostream, "output stream", log);
}
StringBuilder actualPath = getHdfsPathUsingFileNameHeader(exchange);
ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
} else if (ostream == null) {
// must have ostream
ostream = setupHdfs(false);
}
boolean split = false;
List<SplitStrategy> strategies = config.getSplitStrategies();
for (SplitStrategy splitStrategy : strategies) {
split |= splitStrategy.getType().split(ostream, splitStrategy.value, this);
}
if (split) {
if (ostream != null) {
IOHelper.close(ostream, "output stream", log);
}
StringBuilder actualPath = newFileName();
ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
}
String path = ostream.getActualPath();
log.trace("Writing body to hdfs-file {}", path);
ostream.append(key, body, exchange.getContext().getTypeConverter());
idle.set(false);
// close if we do not have idle checker task to do this for us
boolean close = scheduler == null;
// but user may have a header to explict control the close
Boolean closeHeader = exchange.getIn().getHeader(HdfsConstants.HDFS_CLOSE, Boolean.class);
if (closeHeader != null) {
close = closeHeader;
}
// if no idle checker then we need to explicit close the stream after usage
if (close) {
try {
HdfsProducer.this.log.trace("Closing stream");
ostream.close();
ostream = null;
} catch (IOException e) {
// ignore
}
}
log.debug("Wrote body to hdfs-file {}", path);
}
use of java.io.IOException in project camel by apache.
the class HdfsProducer method doProcess.
void doProcess(Exchange exchange) throws Exception {
Object body = exchange.getIn().getBody();
Object key = exchange.getIn().getHeader(HdfsHeader.KEY.name());
// if an explicit filename is specified, close any existing stream and append the filename to the hdfsPath
if (exchange.getIn().getHeader(Exchange.FILE_NAME) != null) {
if (ostream != null) {
IOHelper.close(ostream, "output stream", log);
}
StringBuilder actualPath = getHdfsPathUsingFileNameHeader(exchange);
ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
} else if (ostream == null) {
// must have ostream
ostream = setupHdfs(false);
}
boolean split = false;
List<SplitStrategy> strategies = config.getSplitStrategies();
for (SplitStrategy splitStrategy : strategies) {
split |= splitStrategy.getType().split(ostream, splitStrategy.value, this);
}
if (split) {
if (ostream != null) {
IOHelper.close(ostream, "output stream", log);
}
StringBuilder actualPath = newFileName();
ostream = HdfsOutputStream.createOutputStream(actualPath.toString(), config);
}
String path = ostream.getActualPath();
log.trace("Writing body to hdfs-file {}", path);
ostream.append(key, body, exchange.getContext().getTypeConverter());
idle.set(false);
// close if we do not have idle checker task to do this for us
boolean close = scheduler == null;
// but user may have a header to explict control the close
Boolean closeHeader = exchange.getIn().getHeader(HdfsConstants.HDFS_CLOSE, Boolean.class);
if (closeHeader != null) {
close = closeHeader;
}
// if no idle checker then we need to explicit close the stream after usage
if (close) {
try {
HdfsProducer.this.log.trace("Closing stream");
ostream.close();
ostream = null;
} catch (IOException e) {
// ignore
}
}
log.debug("Wrote body to hdfs-file {}", path);
}
use of java.io.IOException in project camel by apache.
the class HessianDataFormat method unmarshal.
@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
final Hessian2Input in = new Hessian2Input(inputStream);
try {
in.startMessage();
final Object obj = in.readObject();
in.completeMessage();
return obj;
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
use of java.io.IOException in project camel by apache.
the class HttpProducer method doExtractResponseBodyAsStream.
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);
}
}
Aggregations