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:eu.unifiedviews.plugins.transformer.gunzipper.GunzipperTest.java

@Test
public void testSmallFile() throws Exception {
    GunzipperConfig_V1 config = new GunzipperConfig_V1();

    // Prepare DPU.
    Gunzipper dpu = new Gunzipper();
    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.gz")));
    try (FileOutputStream fout = new FileOutputStream(inputFile)) {
        IOUtils.copy(Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE.gz"), fout);
    }/* w  w w.j  a v  a2  s  .c  o m*/
    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();
        String outputContent = IOUtils.toString(new URI(entry.getFileURIString()), "US-ASCII");
        String expectedContent = IOUtils.toString(
                Thread.currentThread().getContextClassLoader().getResourceAsStream("LICENSE"), "US-ASCII");

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

From source file:net.hillsdon.reviki.web.handlers.StreamView.java

public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    response.setContentType(_mimeType);//from w ww  .  ja  v  a 2 s .co m
    IOUtils.copy(_contents, response.getOutputStream());
}

From source file:com.ettrema.http.caldav.demo.TMessageResource.java

public TMessageResource(TFolderResource parent, String name, StandardMessage sm) {
    super(parent, name);
    this.msg = sm;
    for (Attachment attachment : sm.getAttachments()) {
        try {/*from  w w w. ja  va2  s . c  o m*/
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            IOUtils.copy(attachment.getInputStream(), bout);
            TBinaryResource bRes = new TBinaryResource(this, attachment.getName(), bout.toByteArray(),
                    attachment.getContentType());
            System.out.println("created attachment file: " + bRes.getName());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:com.pdftron.pdf.utils.BasicHTTPDownloadTask.java

@Override
protected Boolean doInBackground(String... params) {
    URL url;//from   w  w  w. j a  v a2s  . c  o m
    OutputStream filestream = null;
    try {
        url = new URL(mURL);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        filestream = new BufferedOutputStream(new FileOutputStream(mSaveFile));

        IOUtils.copy(urlConnection.getInputStream(), filestream);

        filestream.close();
    } catch (MalformedURLException e) {
        return false;
    } catch (IOException e) {
        try {
            if (filestream != null)
                filestream.close();
        } catch (IOException e2) {
        }
        return false;
    }
    return true;
}

From source file:ar.com.zauber.commons.web.proxy.transformers.NullContentTransformer.java

/** @see OutputTransformer#transform(InputStream, OutputStream) */
public final void transform(final InputStream is, final OutputStream os, final ContentMetadata metadata) {
    try {/*from   ww  w  .  ja v a 2s .  c  o m*/
        IOUtils.copy(is, os);
    } catch (final IOException e) {
        throw new UnhandledException(e);
    }
}

From source file:com.ewcms.publication.filter.render.ResourceRender.java

/**
 * ?// ww w  .  j  a  va  2  s. c  o  m
 * 
 * @param response
 * @param uri   ?
 * @return
 * @throws IOException
 */
@Override
protected boolean output(HttpServletResponse response, Long siteId, String uri) throws IOException {
    Resource resource = resourcePublishDao.findByUri(siteId, uri);
    if (resource == null) {
        logger.debug("Resource is not exist,uri is {}", uri);
        return false;
    }
    String realPath = resource.getPath();
    try {
        IOUtils.copy(new FileInputStream(realPath), response.getOutputStream());
    } catch (Exception e) {
        logger.warn("Resource is not exit,real path is{}", realPath);
    }
    response.flushBuffer();

    return true;
}

From source file:brut.util.BrutIO.java

public static void copy(File inputFile, ZipOutputStream outputFile) throws IOException {
    try (FileInputStream fis = new FileInputStream(inputFile)) {
        IOUtils.copy(fis, outputFile);
    }//from w  ww .java 2s .  c  om
}

From source file:com.twitter.heron.common.config.SystemConfigTest.java

@Test
public void testReadConfig() throws IOException {
    InputStream inputStream = getClass().getResourceAsStream(RESOURCE_LOC);
    if (inputStream == null) {
        throw new RuntimeException("Sample output file not found");
    }//w  w w .j a  v  a2 s  .  c  o  m
    File file = File.createTempFile("system_temp", "yaml");
    file.deleteOnExit();
    OutputStream outputStream = new FileOutputStream(file);
    IOUtils.copy(inputStream, outputStream);
    outputStream.close();
    SystemConfig systemConfig = SystemConfig.newBuilder(true).putAll(file.getAbsolutePath(), true).build();
    Assert.assertEquals("log-files", systemConfig.getHeronLoggingDirectory());
    Assert.assertEquals(ByteAmount.fromMegabytes(100), systemConfig.getHeronLoggingMaximumSize());
    Assert.assertEquals(5, systemConfig.getHeronLoggingMaximumFiles());
    Assert.assertEquals(Duration.ofSeconds(60), systemConfig.getHeronMetricsExportInterval());
}

From source file:com.guye.baffle.util.OS.java

public static void cpdir(File src, File dest) throws BaffleException {
    dest.mkdirs();/*from  w  w  w.  j a va  2 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 BaffleException("Could not copy file: " + file, ex);
        }
    }
}

From source file:edu.dfci.cccb.mev.limma.domain.simple.FileBackedLimma.java

@SneakyThrows
public static FileBackedLimma from(InputStream results) {
    FileBackedLimma result = new FileBackedLimma(new TemporaryFolder());
    try (OutputStream full = new BufferedOutputStream(new FileOutputStream(result.full));
            BufferedInputStream in = new BufferedInputStream(results)) {
        IOUtils.copy(in, full);
    }//from w  w  w  .  ja v a 2  s .c o  m
    return result;
}