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:edu.scripps.fl.test.pubchem.test.FetchFromDepositionSystemTest.java

private static void displayFile(int aid, InputStream is, String ext) throws IOException {
    File file = File.createTempFile("AID" + aid + "-", "." + ext);
    file.deleteOnExit();/*from w  ww.j  a  v  a2 s.co  m*/
    IOUtils.copy(is, new FileOutputStream(file));
    is.close();
    log.info("Created file " + file);
    //      Desktop.getDesktop().open(file);
}

From source file:gobblin.util.io.MeteredInputStreamTest.java

@Test
public void test() throws Exception {
    InputStream is = new ByteArrayInputStream("aabbccddee".getBytes(Charsets.UTF_8));

    Meter meter = new Meter();
    MeteredInputStream mis = MeteredInputStream.builder().in(is).meter(meter).updateFrequency(1).build();

    InputStream skipped = new MyInputStream(mis);
    DataInputStream dis = new DataInputStream(skipped);

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    IOUtils.copy(dis, os);
    String output = os.toString(Charsets.UTF_8.name());

    Assert.assertEquals(output, "abcde");

    Optional<MeteredInputStream> meteredOpt = MeteredInputStream.findWrappedMeteredInputStream(dis);
    Assert.assertEquals(meteredOpt.get(), mis);
    Assert.assertEquals(meteredOpt.get().getBytesProcessedMeter().getCount(), 10);
}

From source file:com.jayway.restassured.itest.java.FileDownloadITest.java

@Test
public void canDownloadLargeFiles() throws Exception {
    int expectedSize = IOUtils
            .toByteArray(getClass().getResourceAsStream("/powermock-easymock-junit-1.4.12.zip")).length;
    final InputStream inputStream = expect().log().headers().when()
            .get("http://powermock.googlecode.com/files/powermock-easymock-junit-1.4.12.zip").asInputStream();

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, byteArrayOutputStream);
    IOUtils.closeQuietly(byteArrayOutputStream);
    IOUtils.closeQuietly(inputStream);/*from  w  w  w  .j av  a2 s . c  o m*/

    assertThat(byteArrayOutputStream.size(), equalTo(expectedSize));
}

From source file:com.mesosphere.dcos.cassandra.executor.compress.SnappyCompressionDriver.java

@Override
public void compress(final String sourcePath, final String destinationPath) throws IOException {
    BufferedInputStream inputStream = null;
    SnappyOutputStream compressedStream = null;
    try {/*from   w  w  w. j a  va  2  s .com*/
        inputStream = new BufferedInputStream(new FileInputStream(sourcePath));
        compressedStream = new SnappyOutputStream(
                new BufferedOutputStream(new FileOutputStream(destinationPath)));
        IOUtils.copy(inputStream, compressedStream);
    } catch (IOException e) {
        LOGGER.error("Failed to compress {} to {} due to: {}", sourcePath, destinationPath, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(compressedStream);
    }
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a zip entry in a directory/*from   w w w  .  ja va  2 s .  com*/
 * 
 * @param zipfile
 * @param entry
 * @param outputDir
 * @throws IOException
 */
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        ClosingUtils.close(outputStream);
        ClosingUtils.close(inputStream);
    }
}

From source file:com.casker.portfolio.controller.FileController.java

/**
 *  // ww w  . j a v a  2 s.  c o m
 * 
 * @return
 */
@ResponseBody
@RequestMapping("/portfolio/{portfolioNo}/{imageType}")
public void editPassword(HttpServletResponse response, @PathVariable long portfolioNo,
        @PathVariable String imageType) throws Exception {

    Portfolio portfolio = portfolioService.getPortfolioDetail(portfolioNo);

    File file = portfolioService.getImageFile(portfolio, imageType);

    response.setContentLength((int) file.length());

    String fileName = URLEncoder.encode(file.getName(), "utf-8");

    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
    response.setHeader("Content-Transfer-Encoding", "binary");

    InputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, response.getOutputStream());
    IOUtils.closeQuietly(inputStream);

    response.flushBuffer();
}

From source file:com.vigglet.oei.technician.UploadProfilePhoto.java

@Override
protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {/*from   w w  w. ja  va 2s.c om*/
        User user = getUser(req);

        for (Part part : req.getParts()) {
            byte[] b = IOUtils.toByteArray(part.getInputStream());
            String fileName = extractFileName(part);

            File file = new File(Content.FILE_LOCATION + "/" + fileName);
            FileOutputStream fos = new FileOutputStream(file);
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            IOUtils.copy(bais, fos);

            fos.flush();
            fos.close();
            bais.close();

            Content content = new Content();
            content.setCompany(user.getCompany());
            content.setUser(user.getId());
            content.setName(file.getName());
            content.setFilesize((int) file.length());
            content.setLocation(file.getAbsolutePath());

            ContentUtil.getInstance().insertOrUpdate(content);
        }
    } catch (ServletException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
}

From source file:com.adaptris.core.services.Base64EncodeService.java

/**
 * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage)
 *//*from   w w  w . j a v  a  2  s  . c  om*/
public void doService(AdaptrisMessage msg) throws ServiceException {

    OutputStream out = null;
    InputStream in = null;
    try {
        in = msg.getInputStream();
        out = MimeUtility.encode(msg.getOutputStream(), MimeConstants.ENCODING_BASE64);
        IOUtils.copy(in, out);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.ibm.rpe.web.template.ui.utils.Utils.java

public static Response downloadResponse(final InputStream is, String name) {
    if (is == null) {
        return Response.status(Response.Status.NOT_FOUND).entity("File not found").build();
    }//from   ww  w .  ja  v a2  s.c  o  m

    // OR: use a custom StreamingOutput and set to Response
    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream output) throws IOException {
            IOUtils.copy(is, output);
        }
    };

    return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM)
            .header(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + name).build();
}

From source file:com.adaptris.core.services.Base64DecodeService.java

/**
 * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage)
 *///from  w w  w.j ava  2s . c om
public void doService(AdaptrisMessage msg) throws ServiceException {

    OutputStream out = null;
    InputStream in = null;
    try {
        out = msg.getOutputStream();
        in = MimeUtility.decode(msg.getInputStream(), MimeConstants.ENCODING_BASE64);
        IOUtils.copy(in, out);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}