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:end2endtests.runner.ProcessRunner.java

private static void redirectStream(final InputStream in, final OutputStream out) {
    Thread t = new Thread(new Runnable() {
        public void run() {
            try {
                IOUtils.copy(in, out);
            } catch (IOException e) {
                e.printStackTrace();//from w ww . j a  va 2s .c  om
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
    });
    t.setDaemon(true);
    t.start();
}

From source file:jp.co.opentone.bsol.framework.core.filestore.FileStoreClient.java

/**
 * ?????ID?./* w w w  .j  a v a  2s .c om*/
 * @param sourcePath ?
 * @return ID
 */
public String createFile(String sourcePath) {
    String id = generateId();
    try (FileInputStream in = new FileInputStream(sourcePath);
            FileOutputStream out = new FileOutputStream(getFile(id))) {
        IOUtils.copy(in, out);
        return id;
    } catch (IOException e) {
        throw new FileStoreException(e);
    }
}

From source file:echopoint.tucana.InputStreamDownloadProvider.java

public void writeFile(final OutputStream out) throws IOException {
    status = Status.inprogress;//from  w ww .j  a  v  a2s  .c  o m

    try {
        IOUtils.copy(stream, out);
        out.flush();
    } catch (IOException e) {
        status = Status.failed;
        throw e;
    }

    status = Status.completed;
}

From source file:net.orpiske.ssps.common.archive.zip.ZipArchive.java

private long extractFile(ZipFile zipFile, ZipArchiveEntry entry, File outFile)
        throws IOException, ZipException, FileNotFoundException {
    InputStream fin = null;// w  ww .  j  a  v a  2 s .co  m
    BufferedInputStream bin = null;
    FileOutputStream out = null;

    long ret = 0;

    try {
        fin = zipFile.getInputStream(entry);
        bin = new BufferedInputStream(fin);
        out = new FileOutputStream(outFile);

        ret += IOUtils.copy(fin, out);

        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(fin);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(fin);
    }

    return ret;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.api.sparqlquery.SparqlQueryApiResultSetProducer.java

@Override
public void executeAndFormat(OutputStream out) throws RDFServiceException, IOException {
    InputStream rawResult = getRawResultStream();
    if (mediaType.isNativeFormat()) {
        IOUtils.copy(rawResult, out);
    } else if (mediaType == TSV) {
        // ARQ doesn't support TSV, so we will do the translation.
        pipeWithReplacement(rawResult, out);
    } else {//  w  ww.  ja  v a 2 s. c o m
        ResultSet rs = ResultSetFactory.fromJSON(rawResult);
        ResultsFormat format = ResultsFormat.lookup(mediaType.getJenaResponseFormat());
        ResultSetFormatter.output(out, rs, format);
    }
}

From source file:fm.last.commons.io.LastFileUtils.java

/**
 * Appends the passed files (in list order) to the destination.
 * /*from   w  w w. j  av a 2  s . c o  m*/
 * @param destination Destination file.
 * @param files List of files to be appended to the destination file.
 * @throws IOException If an error occurs appending the files.
 */
public static void appendFiles(File destination, List<File> files) throws IOException {
    FileUtils.copyFile(files.get(0), destination);
    FileOutputStream fos = new FileOutputStream(destination, true);
    for (int i = 1; i < files.size(); i++) {
        FileInputStream fis = new FileInputStream(files.get(i));
        IOUtils.copy(fis, fos);
        IOUtils.closeQuietly(fis);
    }
    IOUtils.closeQuietly(fos);
}

From source file:edu.vt.vbi.patric.mashup.PSICQUICInterface.java

public String getCounts(String db, String term) throws java.rmi.RemoteException {
    String result = "-1";
    try {//from  w w w  . j a  va  2 s .c o m
        String url = baseURL + db + baseURLQuery + term + "?format=count";
        LOGGER.trace(url);

        StringWriter writer = new StringWriter();
        IOUtils.copy((new URL(url)).openStream(), writer);
        result = writer.toString();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return result;
}

From source file:edu.brandeis.ggen.GGenCommand.java

public String generateGraphviz() throws GGenException {
    ProcessBuilder pb = new ProcessBuilder();
    List<String> command = new LinkedList<>();
    command.add(GGEN_PATH);// w ww  .  j  ava2 s  .co  m
    Arrays.stream(args.split(" ")).forEach(command::add);

    try {
        Process p = pb.command(command).start();

        if (this.input != null)
            p.getOutputStream().write(this.input.generateGraphviz().getBytes());
        p.getOutputStream().close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(p.getInputStream(), bos);
        return new String(bos.toByteArray());
    } catch (IOException e) {
        throw new GGenException(
                "Could not launch the ggen command. Check that the GGenCommand.GGEN_PATH static variable is correctly set for your system. Message: "
                        + e.getMessage());
    }
}

From source file:com.cloudant.sync.datastore.encryption.EncryptedAttachmentOutputStreamTest.java

@Test
public void testWritingValidFile() throws IOException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
    File expectedCipherText = TestUtils.loadFixture("fixture/EncryptedAttachmentTest_cipherText_aes128");
    File plainText = TestUtils.loadFixture("fixture/EncryptedAttachmentTest_plainText");

    ByteArrayOutputStream actualEncryptedOutput = new ByteArrayOutputStream();

    OutputStream encryptedOutputStream = new EncryptedAttachmentOutputStream(actualEncryptedOutput,
            EncryptionTestConstants.key16Byte, EncryptionTestConstants.iv);
    IOUtils.copy(new FileInputStream(plainText), encryptedOutputStream);
    encryptedOutputStream.close();//from  ww w .j  a v  a2  s.co  m
    actualEncryptedOutput.close();

    Assert.assertTrue("Writing to encrypted stream didn't give expected cipher text",
            IOUtils.contentEquals(new ByteArrayInputStream(actualEncryptedOutput.toByteArray()),
                    new FileInputStream(expectedCipherText)));
}

From source file:com.yahoo.rdl.maven.RdlExecutableFileProviderImpl.java

@Override
public Path getRdlExecutableFile() throws MojoExecutionException {
    if (configuredExecutableFile != null) {
        return configuredExecutableFile;
    }//from   w  w  w .  j a  va  2s. co m
    if (actualExecutableFile != null) {
        return actualExecutableFile;
    }
    File binFolder = new File(scratchSpace.toFile(), "bin");
    if (!binFolder.mkdirs() && !binFolder.exists()) {
        throw new MojoExecutionException("Unable to create folder for rdl executable: " + binFolder);
    }

    Path rdlBinaryPath = scratchSpace.resolve(Paths.get("bin", "rdl"));
    try (OutputStream os = Files.newOutputStream(rdlBinaryPath, StandardOpenOption.CREATE)) {
        try (InputStream is = getClass().getClassLoader().getResourceAsStream("bin/" + osName + "/rdl")) {
            IOUtils.copy(is, os);
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write rdl binary to " + rdlBinaryPath, e);
    }
    if (!rdlBinaryPath.toFile().setExecutable(true) && !rdlBinaryPath.toFile().canExecute()) {
        throw new MojoExecutionException("Unable to chmod +x executable: " + rdlBinaryPath.toAbsolutePath());
    }

    Path rdlGenSwaggerBinaryPath = scratchSpace.resolve(Paths.get("bin", "rdl-gen-swagger"));
    try (OutputStream os = Files.newOutputStream(rdlGenSwaggerBinaryPath, StandardOpenOption.CREATE)) {
        try (InputStream is = getClass().getClassLoader()
                .getResourceAsStream("bin/" + osName + "/rdl-gen-swagger")) {
            IOUtils.copy(is, os);
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write rdl-gen-swagger binary to " + rdlBinaryPath, e);
    }
    if (!rdlGenSwaggerBinaryPath.toFile().setExecutable(true)
            && !rdlGenSwaggerBinaryPath.toFile().canExecute()) {
        throw new MojoExecutionException("Unable to chmod +x executable: " + rdlBinaryPath.toAbsolutePath());
    }

    actualExecutableFile = rdlBinaryPath;
    return rdlBinaryPath;
}