All Downloads are FREE. Search and download functionalities are using the official Maven repository.

metridoc.camel.utils.IOUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010 Trustees of the University of Pennsylvania Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 * http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package metridoc.camel.utils;

import org.apache.camel.Exchange;
import org.apache.camel.component.file.GenericFile;

import java.io.*;
import java.util.zip.GZIPInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: tbarker
 * Date: 7/4/11
 * Time: 10:00 PM
 */
public class IOUtils {
    public static InputStream convertGenericFileToInputStream(Exchange exchange) {

        File file = getFile(exchange);

        if (file != null) {
            return convertFileToInputStream(file);
        }
        return exchange.getIn().getBody(InputStream.class);
    }

    public static InputStream convertFileToInputStream(File file) {
        String fileName = file.getName();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            if (fileName != null && fileName.endsWith(".gz")) {
                return new GZIPInputStream(inputStream);
            }
        } catch (IOException e) {
            throw new RuntimeException("could not find the file", e);
        }

        return inputStream;
    }

    public static InputStream convertGenericFileToInputStream(GenericFile genericFile) {
        File file = getFile(genericFile);
        return convertFileToInputStream(file);
    }

    public static File getFile(GenericFile file) {
        return (File) file.getBody();
    }

    public static File getFile(Exchange exchange) {
        GenericFile genericFile = exchange.getIn().getBody(GenericFile.class);
        File file;

        if (genericFile == null) {
            file = exchange.getIn().getBody(File.class);
        } else {
            file = getFile(genericFile);
        }

        return file;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy