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.ibm.jaggr.core.util.CopyUtil.java

public static void copy(Reader reader, OutputStream out) throws IOException {
    InputStream in = new ReaderInputStream(reader, "UTF-8"); //$NON-NLS-1$
    try {/*from  w  w  w.  j a va2s. co  m*/
        IOUtils.copy(in, out);
    } finally {
        try {
            in.close();
        } catch (Exception ignore) {
        }
        try {
            out.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:fr.itinerennes.bundler.cli.GtfsUtilsTest.java

@BeforeClass
public static void prepare() throws IOException {

    directory = File.createTempFile("junit-", "-itr.tmp");
    directory.delete();/* w  w  w .  j a v a2s .c o  m*/
    directory.mkdir();

    invalidFile = File.createTempFile("junit-", "-itr.tmp");

    gtfsFile = File.createTempFile("junit-", "-itr.tmp.zip");
    final InputStream gtfsIn = GtfsUtilsTest.class.getResourceAsStream("gtfs.zip");
    IOUtils.copy(gtfsIn, new FileOutputStream(gtfsFile));
}

From source file:gov.nih.nci.cacis.xds.StaticMetadataSupplier.java

@Override
public String createSubmissionSet() throws IOException {
    final InputStream staticSubSet = getClass().getClassLoader().getResourceAsStream("submissionSet.xml");
    final StringWriter writer = new StringWriter();
    IOUtils.copy(staticSubSet, writer);
    return writer.toString();
}

From source file:com.asual.summer.onejar.OneJarServer.java

private static String getCurrentWarFile() throws IOException {
    JarFile jarFile = new JarFile(System.getProperty("java.class.path"));
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        String name = entries.nextElement().getName();
        if (name.endsWith(".war")) {
            File war = new File(new File(System.getProperty("java.io.tmpdir")),
                    "summer-onejar-" + System.currentTimeMillis() + ".war");
            InputStream input = jarFile.getInputStream(new ZipEntry(name));
            FileOutputStream output = new FileOutputStream(war);
            IOUtils.copy(input, output);
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
            war.deleteOnExit();//from w w w  . ja v  a2s  . c  o m
            return war.getAbsolutePath();
        }
    }
    return null;
}

From source file:com.oneis.appserver.FileResponse.java

public void writeToOutputStream(OutputStream stream) throws IOException {
    InputStream in = new FileInputStream(file);
    try {//from   w ww  . jav  a 2  s . com
        IOUtils.copy(in, stream);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.sap.prd.mobile.ios.mios.ScriptRunner.java

private static File copyScript(String script, File workingDirectory) throws IOException {
    if (!workingDirectory.exists())
        if (!workingDirectory.mkdirs())
            throw new IOException("Cannot create directory '" + workingDirectory + "'.");

    final File scriptFile = new File(workingDirectory, getScriptFileName(script));
    scriptFile.deleteOnExit();/*from w  w w . j  av  a2  s  .  c  o m*/

    OutputStream os = null;
    InputStream is = null;

    try {
        is = ScriptRunner.class.getResourceAsStream(script);

        if (is == null)
            throw new FileNotFoundException(script + " not found.");

        os = FileUtils.openOutputStream(scriptFile);

        IOUtils.copy(is, os);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }

    Forker.forkProcess(System.out, null, "chmod", "755", scriptFile.getCanonicalPath());
    return scriptFile;
}

From source file:com.ning.billing.util.config.TestXMLSchemaGenerator.java

@Test
public void test() throws IOException, TransformerException, JAXBException {
    InputStream stream = XMLSchemaGenerator.xmlSchema(XmlTestClass.class);
    StringWriter writer = new StringWriter();
    IOUtils.copy(stream, writer);
    String result = writer.toString();

    System.out.println(result);//  ww  w  .  j  a  v  a 2  s.c o m
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

public static void unzipFile(File destFolder, File zipFile) throws IOException {
    ZipInputStream zipStream = null;
    try {/*  w w w. java 2  s  .  c  o  m*/
        if (!destFolder.exists()) {
            destFolder.mkdirs();
        }

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile));
        zipStream = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zipStream.getNextEntry()) != null) {
            // get output file
            String name = entry.getName();
            if (name.startsWith("/") || name.startsWith("\\"))
                name = name.substring(1);
            File file = new File(destFolder, name);
            // ensure directory exists
            File dir = file.getParentFile();
            if (!dir.exists())
                dir.mkdirs();
            IOUtils.copy(zipStream, new FileOutputStream(file));
        }
    } finally {
        if (zipStream != null)
            zipStream.close();
    }
}

From source file:com.griddynamics.deming.ecommerce.cms.file.service.MultipartFileAdapter.java

public MultipartFileAdapter(InputStream inputStream, String path) throws IOException {
    this.name = path;
    this.originalFileName = path;

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, byteArrayOutputStream);

    this.bytes = byteArrayOutputStream.toByteArray();
}

From source file:brut.util.OS.java

public static void cpdir(File src, File dest) throws BrutException {
    dest.mkdirs();/*from   w  ww .  j av a2  s.  c o m*/
    File[] files = src.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        File destFile = new File(dest.getPath() + File.separatorChar + file.getName());
        if (file.isDirectory()) {
            cpdir(file, destFile);
            continue;
        }
        try {
            InputStream in = new FileInputStream(file);
            OutputStream out = new FileOutputStream(destFile);
            IOUtils.copy(in, out);
            in.close();
            out.close();
        } catch (IOException ex) {
            throw new BrutException("Could not copy file: " + file, ex);
        }
    }
}