Tamas
Tamas

Reputation: 786

CamelFileName vs. message body, file operation

I have implemented a bz2 decompressor by means of the Apache commons-compress library to decompress bz2 files with camel below a certain point in the directory structure on the file system. I have picked up the file name to decompress from the CamelFileName header, opened the file with my decompressor and put the decompressed file back into the same directory. It works fine. The process() method that calls the decompressor I copied here shortened; this processor is invoked for all necessary files by a camel route:

public void process(Exchange exchange) throws Exception {
    LOG.info(" #### BZ2Processor ####");
    BZ2 bz2 = new BZ2();
    String CamelFileName = exchange.getIn().getHeader("CamelFileName", String.class);
    bz2.uncompress(CamelFileName);   
}

I think that it would have been nicer if I take the file from the message body. How would you have implemented it that way?

Upvotes: 1

Views: 1540

Answers (1)

Ramin Arabbagheri
Ramin Arabbagheri

Reputation: 820

The Body would be of type InputStream. You can directly work with this Java type. Camel reads the file on demand. I.e. when you try to access it in the route or in your bean:

String text = exchange.getIn().getBody(String.class);        //or
byte[] bytes = exchange.getIn().getBody(byte[].class);       //or
InputStream is = exchange.getIn().getBody(InputStream.class);

Use one of the above as you see fit. As for closing it, don't worry Camel will take care of it.

Upvotes: 1

Related Questions