Java InputStream Create getInputStream(String fileType, String fileName)

Here you can find the source of getInputStream(String fileType, String fileName)

Description

get Input Stream

License

Apache License

Declaration

public static InputStream getInputStream(String fileType,
            String fileName) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*/*w  ww . j  av  a  2 s  .c  o m*/
 * Copyright 2004-2010 Information & Software Engineering Group (188/1)
 *                     Institute of Software Technology and Interactive Systems
 *                     Vienna University of Technology, Austria
 *
 * Licensed under the Apache 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.ifs.tuwien.ac.at/dm/somtoolbox/license.html
 *
 * 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.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;

public class Main {
    public static InputStream getInputStream(String fileType,
            String fileName) throws FileNotFoundException {
        return getInputStream(fileType, new File(fileName));
    }

    public static InputStream getInputStream(String fileType, File file)
            throws FileNotFoundException {
        InputStream is;
        File gzFile = new File(file.getParentFile(), file.getName() + ".gz");

        if (file == null || !file.exists()) { // we don't find a file with the original file name
            if (gzFile.exists()) { // we check if a '.gz' file exists
                file = gzFile; // if yes, we use this file name
            } else {
                throw new FileNotFoundException("File " + file + " or "
                        + gzFile + " not found (trying file "
                        + file.getAbsolutePath() + ".");
            }
        }

        try {
            is = new GZIPInputStream(new FileInputStream(file));
            Logger.getLogger("at.tuwien.ifs.somtoolbox").info(
                    file + " is gzip compressed. Trying compressed read.");
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException(fileType + " " + file
                    + " not found.");
        } catch (IOException e) {
            Logger.getLogger("at.tuwien.ifs.somtoolbox")
                    .info(file
                            + " is not gzip compressed. Trying uncompressed read.");
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e2) {
                throw new FileNotFoundException("File " + " " + file
                        + " not found.");
            }
        }
        return is;
    }
}

Related

  1. getInputStream(String fileName)
  2. getInputStream(String filename)
  3. getInputStream(String filename)
  4. getInputStream(String filename)
  5. getInputStream(String filePath)
  6. getInputStream(String fname, String enc)
  7. getInputStream(String inputFile)
  8. getInputStream(String location)
  9. getInputStream(String parentFolder, String fileName)