Search in sources :

Example 81 with IOException

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);
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 82 with IOException

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);
}
Also used : IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 83 with IOException

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);
}
Also used : IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 84 with IOException

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
        }
    }
}
Also used : Hessian2Input(com.caucho.hessian.io.Hessian2Input) IOException(java.io.IOException)

Example 85 with IOException

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);
    }
}
Also used : IOException(java.io.IOException) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916