Example usage for org.apache.commons.io IOUtils copy

List of usage examples for org.apache.commons.io IOUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils copy.

Prototype

public static void copy(Reader input, OutputStream output) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

Usage

From source file:com.wenzani.maven.mongodb.TarUtils.java

public String untargz(File archive, File outputDir) {
    String absolutePath = archive.getAbsolutePath();
    String root = null;//from www.  j  a va 2s.  c o  m
    boolean first = true;

    while (absolutePath.contains("tar") || absolutePath.contains("gz") || absolutePath.contains("tgz")) {
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("."));
    }

    absolutePath = absolutePath + ".tar";

    try {
        GZIPInputStream input = new GZIPInputStream(new FileInputStream(archive));
        FileOutputStream fos = new FileOutputStream(new File(absolutePath));

        IOUtils.copy(input, fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fos);

        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                new FileInputStream(absolutePath));

        for (TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry(); entry != null;) {
            unpackEntries(tarArchiveInputStream, entry, outputDir);

            if (first && entry.isDirectory()) {
                root = outputDir + File.separator + entry.getName();
            }

            entry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return root;
}

From source file:net.orpiske.ssps.common.archive.CompressedArchiveUtils.java

/**
 * Decompress a file// www . j av a 2  s.  co m
 * @param source the source file to be uncompressed
 * @param destination the destination directory
 * @return the number of bytes read
 * @throws IOException for lower level I/O errors
 */
public static long gzDecompress(File source, File destination) throws IOException {
    FileOutputStream out;

    prepareDestination(destination);
    out = new FileOutputStream(destination);

    FileInputStream fin = null;
    BufferedInputStream bin = null;
    GzipCompressorInputStream gzIn = null;

    try {
        fin = new FileInputStream(source);
        bin = new BufferedInputStream(fin);
        gzIn = new GzipCompressorInputStream(bin);

        IOUtils.copy(gzIn, out);

        gzIn.close();

        fin.close();
        bin.close();
        out.close();
    } catch (IOException e) {
        IOUtils.closeQuietly(out);

        IOUtils.closeQuietly(fin);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(gzIn);

        throw e;
    }

    return gzIn.getBytesRead();
}

From source file:edu.scripps.fl.pubchem.app.summary.ESummaryStage.java

@Override
public void innerProcess(Object obj) throws StageException {
    try {//from  ww  w. j ava  2  s.c o  m
        Thread.sleep(333);
        List<Long> aids = (List<Long>) obj;
        InputStream in = EUtilsWebSession.getInstance().getSummaries(aids, "pcassay");
        File file = File.createTempFile("pubchem", "summary.xml");
        log.info(String.format("Downloaded %s summaries to %s", aids.size(), file));
        IOUtils.copy(in, new FileOutputStream(file));
        emit(file);
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}

From source file:com.music.service.LocalFileStorageService.java

@Override
public void storeFile(String path, InputStream inputStream, long size) throws IOException {
    OutputStream os = new BufferedOutputStream(new FileOutputStream(path));
    IOUtils.copy(inputStream, os);
    os.close();//from   w  w w . ja va  2s. c  o m
}

From source file:ch.cyberduck.core.io.BufferSegmentingOutputStream.java

@Override
public void flush() throws IOException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Copy buffer %s to output %s", buffer, proxy));
    }//from w  w  w  .  j a  va 2s.  c  om
    IOUtils.copy(new BufferInputStream(buffer), proxy);
    // Re-use buffer
    buffer.truncate(0L);
}

From source file:eu.unifiedviews.plugins.transformer.gzipper.GzipperTest.java

@Test
public void execute() throws Exception {
    GzipperConfig_V1 config = new GzipperConfig_V1();

    // Prepare DPU.
    Gzipper dpu = new Gzipper();
    dpu.configure((new ConfigurationBuilder()).setDpuConfiguration(config).toString());

    // Prepare test environment.
    TestEnvironment environment = new TestEnvironment();

    // Prepare data unit.
    WritableFilesDataUnit filesOutput = environment.createFilesOutput("filesOutput");
    WritableFilesDataUnit filesInput = environment.createFilesInput("filesInput");

    File inputFile = new File(URI.create(filesInput.addNewFile("LICENSE")));
    try (FileOutputStream fout = new FileOutputStream(inputFile)) {
        IOUtils.copy(Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE"), fout);
    }/*from   w w  w  .  j a v a  2  s  . com*/
    try {
        // Run.
        environment.run(dpu);

        // Get file iterator.
        Set<FilesDataUnit.Entry> outputFiles = FilesHelper.getFiles(filesOutput);
        Assert.assertEquals(1, outputFiles.size());

        FilesDataUnit.Entry entry = outputFiles.iterator().next();

        byte[] outputContent = IOUtils.toByteArray(
                new GZIPInputStream(new FileInputStream(new File(new URI(entry.getFileURIString())))));
        byte[] expectedContent = IOUtils
                .toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE"));

        Assert.assertArrayEquals(expectedContent, outputContent);

        Assert.assertEquals("LICENSE.gz", VirtualPathHelpers.getVirtualPath(filesOutput, "LICENSE"));
    } finally {
        // Release resources.
        environment.release();
    }
}

From source file:gov.nih.nci.caarray.plugins.agilent.EndOfLineCorrectingReaderTest.java

private File createTestFile() throws IOException {
    File file = File.createTempFile("EndOfLineCorrectingReaderTest", null);
    file.deleteOnExit();/*from  w ww  .  j  a  v  a2 s .com*/

    final FileWriter fileWriter = new FileWriter(file);
    try {
        IOUtils.copy(new StringReader(originalData), fileWriter);
    } finally {
        fileWriter.close();
    }

    return file;
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();/*from   w ww  .ja v  a  2s . c  o  m*/
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.splout.db.common.SploutClient.java

public static String asString(InputStream inputStream) throws IOException {
    StringWriter writer = new StringWriter();
    try {// w w w  .  jav a  2 s .  co m
        IOUtils.copy(inputStream, writer);
        return writer.toString();
    } finally {
        inputStream.close();
        writer.close();
    }
}

From source file:com.cazcade.billabong.store.impl.FileBasedBinaryStore.java

@Override
protected void addToMap(String storeKey, InputStream data) {
    //Create file
    File storeFile = new File(storeDirectory, storeKey);
    try {/*  w w w.  j  a v a  2  s.  com*/
        if (data != null) {
            FileOutputStream outputStream = new FileOutputStream(storeFile, false);
            try {
                IOUtils.copy(data, outputStream);
            } finally {
                outputStream.close();
            }
            storeFile.setLastModified(dateHelper.current().getTime());

            //add entry to map.
            map.put(storeKey, new FileBinaryStoreEntry(storeFile));
        } else {
            map.remove(storeKey);
            storeFile.delete();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}