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.woonoz.proxy.servlet.BufferOnCreateInputStream.java

public static BufferOnCreateInputStream create(final InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, baos);
    return new BufferOnCreateInputStream(baos.toByteArray());
}

From source file:edu.purdue.cybercenter.dm.util.Helper.java

public static int fileToStream(String path, OutputStream os) throws FileNotFoundException, IOException {
    int size;/*from  w  ww . j  av a2  s .c  om*/
    try (InputStream is = new FileInputStream(path)) {
        size = IOUtils.copy(is, os);
    }
    return size;
}

From source file:com.tek271.reverseProxy.servlet.ContentUtils.java

public static void copyBinary(HttpEntity entity, ServletResponse response) {
    try {/*from   ww w  . j  a v  a2s  . c om*/
        InputStream content = entity.getContent();
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(content, outputStream);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
}

From source file:net.bpelunit.util.ZipUtil.java

public static void unzipFile(File zip, File dir) throws IOException {
    InputStream in = null;//ww w . j a  v a  2s  .  c  o  m
    OutputStream out = null;
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.getName().endsWith("/")) {
            File unzippedFile = new File(dir, entry.getName());
            try {
                in = zipFile.getInputStream(entry);
                unzippedFile.getParentFile().mkdirs();

                out = new FileOutputStream(unzippedFile);

                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    }
}

From source file:com.vamonossoftware.core.ZipUtil.java

public static int unzip(File zip, File dest) {
    try {//from   w  ww. ja  v  a2  s  .  c om
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File destfile = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                destfile.mkdirs();
            } else {
                IOUtils.copy(zf.getInputStream(entry), new FileOutputStream(destfile));
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:com.betfair.cougar.codegen.FileUtil.java

/**
 * Copy the given resource to the given file.
 *
 * @param resourceName name of resource to copy
 * @param destination file/* w  w  w . j a v a  2  s  .co m*/
 */
public static void resourceToFile(String resourceName, File dest, Class src) {

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.getClassLoader().getResourceAsStream(resourceName);
        if (is == null) {
            throw new RuntimeException("Could not load resource: " + resourceName);
        }
        dest.getParentFile().mkdirs();
        os = new FileOutputStream(dest);

        IOUtils.copy(is, os);

    } catch (Exception e) {
        throw new RuntimeException(
                "Error copying resource '" + resourceName + "' to file '" + dest.getPath() + "': " + e, e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:net.pickapack.net.IOHelper.java

/**
 * Extract the resource in the specified path.
 *
 * @param path the path to the resource/*from   ww w .  j  av a  2s  . c  o  m*/
 */
public static void extractResource(String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    try {
        IOUtils.copy(IOHelper.class.getResourceAsStream("/" + path), new FileOutputStream(path));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:es.caib.sgtsic.util.Zips.java

public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(baos);

    for (String key : documents.keySet()) {

        InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key)));
        ZipEntry zipEntry = new ZipEntry(key);
        zip.putNextEntry(zipEntry);//w w w  .j av a  2 s.  c om
        IOUtils.copy(is, zip);
        zip.closeEntry();
        is.close();
    }

    zip.close();
    baos.close();

    return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip");

}

From source file:com.iyonger.apm.web.util.FileUtils.java

/**
 * Copy the given resource to the given file.
 *
 * @param resourcePath resource path//from  www.j av a  2 s. c om
 * @param file         file to write
 * @since 3.2
 */
public static void copyResourceToFile(String resourcePath, File file) {
    InputStream io = null;
    FileOutputStream fos = null;
    try {
        io = new ClassPathResource(resourcePath).getInputStream();
        fos = new FileOutputStream(file);
        IOUtils.copy(io, fos);
    } catch (IOException e) {
        LOGGER.error("error while writing {}", resourcePath, e);
    } finally {
        IOUtils.closeQuietly(io);
        IOUtils.closeQuietly(fos);
    }

}

From source file:com.carrotgarden.eclipse.fileinstall.util.ProjectUtil.java

/**
 * Extract class path resource to the file system.
 *//*  w w  w  .jav a 2  s . com*/
public static void copyFromClasspathIntoProject(
        //
        final Class<?> klaz, //
        final String sourcePath, //
        final IProject project, //
        final String targetPath //
) throws Exception {

    final InputStream input = klaz.getResourceAsStream(sourcePath);

    final File target = file(project, targetPath);

    if (!target.exists()) {
        target.getParentFile().mkdirs();
    }

    final OutputStream output = new FileOutputStream(target);

    IOUtils.copy(input, output);

    input.close();
    output.close();

}