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:com.izforge.izpack.installer.unpacker.CompressedFileUnpacker.java

/**
 * Unpacks a pack file.//from www .  j  a  v a2s.  c om
 *
 * @param file            the pack file meta-data
 * @param packInputStream the pack input stream
 * @param target          the target
 * @throws IOException        for any I/O error
 * @throws InstallerException for any installer exception
 */
@Override
public void unpack(PackFile file, InputStream packInputStream, File target)
        throws IOException, InstallerException {
    File tmpfile = File.createTempFile("izpack-uncompress", null, FileUtils.getTempDirectory());
    OutputStream fo = null;
    InputStream finalStream = null;

    try {
        fo = IOUtils.buffer(FileUtils.openOutputStream(tmpfile));
        IOUtils.copyLarge(packInputStream, fo, 0, file.size());
        fo.flush();
        fo.close();

        InputStream in = IOUtils.buffer(FileUtils.openInputStream(tmpfile));

        if (compressionFormat == PackCompression.DEFLATE) {
            DeflateParameters deflateParameters = new DeflateParameters();
            deflateParameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
            finalStream = new DeflateCompressorInputStream(in, deflateParameters);
        } else {
            finalStream = new CompressorStreamFactory().createCompressorInputStream(compressionFormat.toName(),
                    in);
        }

        copy(file, finalStream, target);
    } catch (CompressorException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(fo);
        IOUtils.closeQuietly(finalStream);
        FileUtils.deleteQuietly(tmpfile);
    }
}

From source file:com.base2.kagura.core.storage.FileReportsProvider.java

/**
 * Custom separate dload report component, so it can be called elsewhere, or overwritten by child Providers. Checks
 * the "report" to ensure it is a directory then looks for reportconf.yaml or reportconf.json inside the file. If it
 * exists loads it./*from w  ww. j  a  v a2  s.  co  m*/
 * @param result The collection of reports to load the contents into.
 * @param report The directory that contains the report files.
 * @param reportId The report id
 * @throws IOException
 */
protected void loadReport(ReportsConfig result, File report, String reportId) throws IOException {
    if (report.isDirectory()) {
        FilenameFilter configYamlFilter = new PatternFilenameFilter("^reportconf.(yaml|json)$");
        File[] selectYaml = report.listFiles(configYamlFilter);
        if (selectYaml != null && selectYaml.length == 1) {
            File selectedYaml = selectYaml[0];
            loadReport(result, FileUtils.openInputStream(selectedYaml), reportId);
        }
    }
}

From source file:com.haulmont.cuba.desktop.sys.MainWindowProperties.java

public void load() {
    Properties properties = new Properties();
    try {/*  w  ww. ja  va  2s . com*/
        Configuration configuration = AppBeans.get(Configuration.NAME);
        File file = new File(configuration.getConfig(GlobalConfig.class).getDataDir(),
                "main-window.properties");
        if (file.exists()) {
            FileInputStream stream = FileUtils.openInputStream(file);
            try {
                properties.load(stream);
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }
    } catch (IOException e) {
        log.error("Error loading main window location", e);
    }
    loadProperties(properties);
}

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

/**
 * Initialize the reader.//  w ww .  j  av  a 2 s .co m
 *
 * @param file The input file
 * @throws IOException If an error occurs reading the input
 */
public ReadsReader(final File file) throws IOException {
    this(FileUtils.openInputStream(file));
}

From source file:com.hotmart.hot.uploader.rest.UploadRESTTest.java

/**
 * Test of upload method, of class UploadREST.
 *///from w w w . j a  v a  2s.c  o m
@Test
@InSequence(1)
public void testUpload(@ArquillianResteasyResource UploadREST uploadREST) throws IOException {

    ChunkMetaInfo chunk = new ChunkMetaInfo();
    chunk.setFileMD5(MD5);
    chunk.setContentRange("bytes 0-" + part1Size + "/" + fileSize);
    chunk.setFileName(part1.getName());
    chunk.setUser(USER);
    FileUtils.getUserDirectoryPath();

    //part1
    Response response = uploadREST.upload(chunk, FileUtils.openInputStream(part1));
    assertThat(response.getStatus()).isEqualTo(204);
    //part2
    chunk = new ChunkMetaInfo();
    chunk.setFileMD5(MD5);
    chunk.setUser(USER);
    chunk.setContentRange("bytes " + part1Size + "-" + (part1Size + part2Size) + "/" + fileSize);
    response = uploadREST.upload(chunk, FileUtils.openInputStream(part2));
    assertThat(response.getStatus()).isEqualTo(200);
}

From source file:fr.letroll.ttorrentandroid.common.Torrent.java

@Nonnull
private static Map<String, BEValue> load(@Nonnull File file) throws IOException {
    InputStream in = FileUtils.openInputStream(file);
    try {/*w  w  w .j a  v  a 2  s .co  m*/
        return new StreamBDecoder(in).bdecodeMap().getMap();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.aionlightning.slf4j.conversion.TruncateToZipFileAppender.java

/**
 * This method creates archive with file instead of deleting it.
 * /*from w w  w .  j  a  v a  2 s .c  o m*/
 * @param file
 *            file to truncate
 */
protected void truncate(File file) {
    File backupRoot = new File(backupDir);
    if (!backupRoot.exists() && !backupRoot.mkdirs()) {
        log.warn("Can't create backup dir for backup storage");
        return;
    }

    String date = "";
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        date = reader.readLine().split("\f")[1];
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip");
    ZipOutputStream zos = null;
    FileInputStream fis = null;
    try {
        zos = new ZipOutputStream(new FileOutputStream(zipFile));
        ZipEntry entry = new ZipEntry(file.getName());
        entry.setMethod(ZipEntry.DEFLATED);
        entry.setCrc(FileUtils.checksumCRC32(file));
        zos.putNextEntry(entry);
        fis = FileUtils.openInputStream(file);

        byte[] buffer = new byte[1024];
        int readed;
        while ((readed = fis.read(buffer)) != -1) {
            zos.write(buffer, 0, readed);
        }

    } catch (Exception e) {
        log.warn("Can't create zip file", e);
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
                log.warn("Can't close zip file", e);
            }
        }

        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.warn("Can't close zipped file", e);
            }
        }
    }

    if (!file.delete()) {
        log.warn("Can't delete old log file " + file.getAbsolutePath());
    }
}

From source file:com.izforge.izpack.installer.unpacker.Pack200FileUnpackerTest.java

@Override
protected InputStream createPackStream(File source) throws IOException {
    File tmpfile = null;//  w ww.  j a  v  a 2 s . c  o  m
    JarFile jar = null;

    try {
        tmpfile = File.createTempFile("izpack-compress", ".pack200", FileUtils.getTempDirectory());
        CountingOutputStream proxyOutputStream = new CountingOutputStream(FileUtils.openOutputStream(tmpfile));
        OutputStream bufferedStream = IOUtils.buffer(proxyOutputStream);

        Pack200.Packer packer = createPack200Packer(this.packFile);
        jar = new JarFile(this.packFile.getFile());
        packer.pack(jar, bufferedStream);

        bufferedStream.flush();
        this.packFile.setSize(proxyOutputStream.getByteCount());

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(FileUtils.openInputStream(tmpfile), out);
        out.close();
        return new ByteArrayInputStream(out.toByteArray());
    } finally {
        if (jar != null) {
            jar.close();
        }
        FileUtils.deleteQuietly(tmpfile);
    }
}

From source file:eu.apenet.dpt.standalone.gui.eaccpf.EacCpfFrame.java

public EacCpfFrame(File eacFile, boolean isEac, Dimension dimension, ProfileListModel model,
        ResourceBundle labels) throws Exception {
    String namespace = ReadXml.getXmlNamespace(eacFile);
    if (isEac && !namespace.equals(EacCpfNamespaceMapper.EAC_CPF_URI)) {
        throw new Exception("eaccpf.error.fileNotInEacCpfNamespace");
    } else if (!isEac && namespace.equals(EacCpfNamespaceMapper.EAC_CPF_URI)) {
        throw new Exception("eaccpf.error.notAnEacCpfFile");
    }/*from w  ww  .j  a v  a 2  s  .c  o m*/
    if (!isEac) {
        try {
            InputStream is = FileUtils.openInputStream(eacFile);
            File tempEacCpfFile = new File(Utilities.TEMP_DIR + "tmp_" + eacFile.getName());
            File xslFile = new File(Utilities.SYSTEM_DIR + "default-apeEAC-CPF.xsl");
            TransformationTool.createTransformation(is, tempEacCpfFile, xslFile, null, true, true, "", true,
                    null);
            eacFile = tempEacCpfFile;
        } catch (Exception e) {
            LOG.error("Something went wrong...", e);
        }
    }
    this.dimension = dimension;
    this.model = model;
    this.labels = labels;
    createFrame(eacFile, false);
}

From source file:com.bandstand.web.SongsController.java

@Get
public InputStream getSongFile(Song song) throws IOException {
    File content = new File(song.getFileName());
    return FileUtils.openInputStream(content);
}