Example usage for org.apache.commons.io FileUtils openInputStream

List of usage examples for org.apache.commons.io FileUtils openInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openInputStream.

Prototype

public static FileInputStream openInputStream(File file) throws IOException 

Source Link

Document

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

Usage

From source file:eu.seaclouds.platform.dashboard.model.SeaCloudsApplicationDataStorageTest.java

@Test
public void testAddSeaCloudsApplicationData() throws Exception {
    Yaml yamlParser = new Yaml();
    URL resource = Resources.getResource(TOSCA_DAM_FILE_PATH);
    Map toscaDamMap = (Map) yamlParser.load(FileUtils.openInputStream(new File(resource.getFile())));

    int oldSize = dataStore.listSeaCloudsApplicationData().size();
    SeaCloudsApplicationData seaCloudsApplicationData = new SeaCloudsApplicationData(toscaDamMap);
    dataStore.addSeaCloudsApplicationData(seaCloudsApplicationData);
    int newSize = dataStore.listSeaCloudsApplicationData().size();
    assertEquals(oldSize, newSize - 1);//from  w  ww  . j a v  a 2  s  . c  o  m

    SeaCloudsApplicationData seaCloudsApplicationDataById = dataStore
            .getSeaCloudsApplicationDataById(seaCloudsApplicationData.getSeaCloudsApplicationId());
    assertEquals(seaCloudsApplicationData, seaCloudsApplicationDataById);
}

From source file:de.pawlidi.openaletheia.license.LicenseLoader.java

public Properties load(File licenseFile) throws LicenseException {
    if (licenseFile == null) {
        throw new LicenseException("Invalid license file");
    }//from   ww w  . j  av a 2s. co  m
    try {
        return load(FileUtils.openInputStream(licenseFile));
    } catch (IOException e) {
        throw new LicenseException("Could not load license file " + licenseFile.getName());
    }
}

From source file:com.dv.util.DataViewerZipUtil.java

/**
 * ZipOutputStream/*  w  w  w  . java  2 s.co m*/
 * @param srcFile 
 * @param zipOut ZipOutputStream?
 * @param ns ZIP
 * @throws IOException
 */
private static void doZipFile(File srcFile, ZipOutputStream zipOut, String ns) throws IOException {
    if (srcFile.isFile()) {
        zipOut.putNextEntry(new ZipEntry(ns + srcFile.getName()));
        InputStream is = FileUtils.openInputStream(srcFile);
        try {
            IOUtils.copy(is, zipOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
        return;
    }
    for (File file : srcFile.listFiles()) {
        String entryName = ns + file.getName();

        if (file.isDirectory()) {
            entryName += File.separator;
            zipOut.putNextEntry(new ZipEntry(entryName));
        }
        doZipFile(file, zipOut, entryName);
    }
}

From source file:com.book.identification.task.BookSearch.java

@Override
public void run() {

    List<Volume> volumes = DAOFactory.getInstance().getVolumeDAO().findAll();

    for (Volume volume : volumes) {
        indexedFiles.add(new File(volume.getPath()));
    }/*w w w  .java2  s  .  c  om*/
    Collection<File> entries = FileUtils.listFiles(root, new SuffixFileFilter(new String[] { ".pdf", ".chm" }),
            TrueFileFilter.INSTANCE);
    for (File fileToProcces : entries) {

        String fileSHA1 = null;
        try {
            fileSHA1 = DigestUtils.shaHex(FileUtils.openInputStream(fileToProcces));
        } catch (IOException e1) {
            continue;
        }
        logger.info("Accepted file -> " + fileToProcces.getName() + " Hash -> " + fileSHA1);
        FileType fileType = FileType
                .valueOf(StringUtils.upperCase(FilenameUtils.getExtension(fileToProcces.getName())));
        try {
            fileQueue.put(new BookFile(fileToProcces, fileSHA1, fileType));
        } catch (InterruptedException e) {
            logger.info(e);
        }
    }
    nextWorker.notifyEndProducers();
}

From source file:com.github.tddts.jet.util.Util.java

/**
 * Load property with given key from given property file.
 *
 * @param fileName property file path// w w  w .j  a  v a 2s .c o m
 * @param key      property key
 * @return property value
 */
public static String loadProperty(String fileName, String key) {
    try {
        File file = new File(fileName);

        if (!file.exists())
            return null;

        Properties properties = new Properties();

        try (InputStream in = FileUtils.openInputStream(file)) {
            properties.load(in);
        }

        return properties.getProperty(key);

    } catch (IOException e) {
        throw new ApplicationException(e);
    }
}

From source file:com.blockwithme.longdb.tools.Utils.java

/** Reads first 2 bytes, checks if its equal to GZIPInputStream.GZIP_MAGIC
 * //from   w  w  w  .  j  a va 2 s  .co  m
 * @param theFile
 *        the file
 * @return true, if compressed
 * @throws Exception */
public static boolean compressed(final File theFile) throws Exception {
    // TODO: I the file name ends with .gz, then I think that we
    // don't need to check
    FileInputStream fis = null;
    try {
        fis = FileUtils.openInputStream(theFile);
        while (true) {
            final byte[] bytes = new byte[2];
            fis.read(bytes);
            final int head = (bytes[0] & BYTE_MASK) | ((bytes[1] << BYTE_BITS) & SECOND_BYTE_MASK);
            return head == GZIPInputStream.GZIP_MAGIC;
        }
    } finally {
        if (fis != null)
            fis.close();
    }
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static void loadFromDirectory(File directory) {
    for (File file : FileUtils.listFiles(directory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)) {
        for (ExtensionFilter filter : ExtensionFilter.values()) {
            if (filter.accept(file.getName())) {
                InputStream inputStream = null;
                try {
                    inputStream = FileUtils.openInputStream(file);
                    Serializable item = fromInputStream(inputStream);
                    if (item != null) {
                        //noinspection unchecked
                        sCache[filter.ordinal()].add(item);
                        L.d("loaded item %s from file", item);
                    }/* ww  w.j  a va  2s.  com*/
                } catch (IOException e) {
                    L.e(e, "Could not merge in Bindables from %s", file.getAbsolutePath());
                } catch (ClassNotFoundException e) {
                    L.e(e, "Could not read Binding properties intermediate file. %s", file.getAbsolutePath());
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
            }
        }
    }
}

From source file:edu.cornell.med.icb.goby.reads.ReadsReader.java

/**
 * Initialize the reader.//from   w  w  w  . j  av  a2 s  .c  o m
 *
 * @param path Path to the input file
 * @throws IOException If an error occurs reading the input
 */
public ReadsReader(final String path) throws IOException {
    this(FileUtils.openInputStream(new File(path)));
}

From source file:com.iisigroup.cap.sample.handler.SampleHandler.java

@HandlerType(HandlerTypeEnum.FileDownload)
public IResult dwnload(IRequest request) throws CapException {
    // String outputName = request.get("fileName", "CapLog.log");
    File file = new File("logs/CapLog.log");
    FileInputStream is = null;//from   www. j  a v a2s .  c  o  m
    try {
        is = FileUtils.openInputStream(file);
        return new ByteArrayDownloadResult(request, IOUtils.toByteArray(is), "text/plain");
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return null;
    // return new FileDownloadResult(request, "logs/CapLog.log", outputName,
    // "text/plain");
}

From source file:com.cloudera.csd.tools.codahale.CodahaleMetricAdapter.java

@Override
public void init(String fixtureFile, @Nullable String conventionsFile) throws Exception {
    Preconditions.checkNotNull(fixtureFile);

    FileInputStream in = null;//from  w w w  .  j a v a  2 s . co  m
    try {
        in = FileUtils.openInputStream(new File(fixtureFile));
        fixture = JsonUtil.valueFromStream(CodahaleMetricDefinitionFixture.class, in);
    } catch (JsonRuntimeException ex) {
        LOG.error("Could not parse file at: " + fixtureFile, ex);
        throw ex;
    } finally {
        IOUtils.closeQuietly(in);
        in = null;
    }

    if (conventionsFile == null) {
        conventions = createDefaultConventions();
    } else {
        try {
            in = FileUtils.openInputStream(new File(conventionsFile));
            conventions = JsonUtil.valueFromStream(CodahaleMetricConventions.class, in);
        } catch (JsonRuntimeException ex) {
            LOG.error("Could not parse file at: " + conventionsFile, ex);
            throw ex;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}