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.alibaba.simpleimage.codec.jpeg.JPEGDecodePerformanceTest.java

protected void run(DecodeFacade decoder, File rootDir, String filename, int times, int frequency)
        throws Exception {
    long start, end, total = 0L;
    if (rootDir == null) {
        rootDir = imgDir;/*  www .ja  v  a2  s  .  c  o  m*/
    }

    FileInputStream inputStream = new FileInputStream(new File(rootDir, filename));

    ByteArrayOutputStream temp = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, temp);
    IOUtils.closeQuietly(inputStream);

    InputStream img = temp.toInputStream();
    temp = null;

    System.out.println("***********Performance Test**************");

    for (int i = 0; i < frequency; i++) {
        // System.gc();
        // Thread.sleep(5 * 1000L);

        start = System.currentTimeMillis();

        for (int t = 0; t < times; t++) {
            // File img = imgs[t % imgs.length];
            img.reset();
            BufferedImage bi = decoder.decode(img);
            bi.getHeight();
            bi = null;
        }

        end = System.currentTimeMillis();

        total += (end - start);
    }

    System.out.printf("Decoder : %s \n", decoder.getName());
    System.out.println("Image : " + filename);
    System.out.printf("Times : %d\n", times);
    System.out.printf("Frequency : %d\n", frequency);
    System.out.printf("Total time : %d ms\n", total);
    System.out.printf("Average time : %.2f ms\n", ((double) total / (times * frequency)));
}

From source file:com.lightboxtechnologies.spectrum.MRCoffeeClient.java

protected static void extract_lib(String src, File dst) throws IOException {
    InputStream in = null;//from w w w.  j  av a  2  s .  c  om
    try {
        in = MRCoffeeJob.class.getResourceAsStream(src);
        if (in == null) {
            throw new IOException(src + " not found!");
        }

        OutputStream out = null;
        try {
            out = new FileOutputStream(dst);
            IOUtils.copy(in, out);
            out.close();
        } finally {
            IOUtils.closeQuietly(out);
        }

        in.close();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.sap.hana.cloud.samples.jenkins.storage.FileStorage.java

@Override
public void save(final InputStream stream) {
    FileOutputStream fileOutput;//  w  ww  .j a va2s. co  m
    try {
        fileOutput = new FileOutputStream(configFile);
        try {
            IOUtils.copy(stream, fileOutput);
        } finally {
            fileOutput.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:controllers.PdfGenerator.java

public void addTemporaryFonts(List<String> fontsToLoad) {
    if (defaultFonts == null)
        defaultFonts = new ArrayList<String>();
    for (String font : fontsToLoad) {
        try {//  ww  w.  j ava2  s.c  o  m
            InputStream fin = Play.application().resourceAsStream(font);
            final File tempFile = File.createTempFile("tmp_" + FilenameUtils.getBaseName(font),
                    "." + FilenameUtils.getExtension(font));
            tempFile.deleteOnExit();
            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copy(fin, out);
            defaultFonts.add(tempFile.getAbsolutePath());
        } catch (Exception e) {
            Logger.error("Loading fonts", e);
        }
    }
}

From source file:de.thischwa.pmcms.tool.compression.Zip.java

/**
 * Static method to compress all files based on the ImputStream in 'entries' into 'zip'. 
 * Each entry has a InputStream and its String representation in the zip.
 * /*from w  w  w.j a  v  a  2 s . com*/
 * @param zip The zip file. It will be deleted if exists. 
 * @param entries Map<File, String>
 * @param monitor Must be initialized correctly by the caller.
 * @throws IOException
 */
public static void compress(final File zip, final Map<InputStream, String> entries,
        final IProgressMonitor monitor) throws IOException {
    if (zip == null || entries == null || CollectionUtils.isEmpty(entries.keySet()))
        throw new IllegalArgumentException("One ore more parameters are empty!");
    if (zip.exists())
        zip.delete();
    else if (!zip.getParentFile().exists())
        zip.getParentFile().mkdirs();

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip)));
    out.setLevel(Deflater.BEST_COMPRESSION);
    try {
        for (InputStream inputStream : entries.keySet()) {
            // skip beginning slash, because can cause errors in other zip apps
            ZipEntry zipEntry = new ZipEntry(skipBeginningSlash(entries.get(inputStream)));
            out.putNextEntry(zipEntry);
            IOUtils.copy(inputStream, out);
            out.closeEntry();
            inputStream.close();
            if (monitor != null)
                monitor.worked(1);
        }
    } finally { // cleanup
        IOUtils.closeQuietly(out);
    }
}

From source file:edu.umn.msi.tropix.common.test.ZipFileCollectionTest.java

public void copyResourceToFile(final File file, final String resource) throws IOException {
    final InputStream inputStream = this.getClass().getResourceAsStream(resource);
    final FileOutputStream tempOutputStream = new FileOutputStream(file, false);
    IOUtils.copy(inputStream, tempOutputStream);
    tempOutputStream.close();/*from  w  w  w  .j a va2  s .com*/
    inputStream.close();
}

From source file:com.jwrapper.maven.jwrapper.JWrapperDownloadMojo.java

@Override
public void execute() throws MojoExecutionException {
    try {//from ww  w .  j a v a2  s  .  c om

        final String wrapperRemoteURL = wrapperRemoteURL();
        final String wrapperLocalURL = wrapperLocalURL();

        logger().info("wrapperRemoteURL: {}", wrapperRemoteURL);
        logger().info("wrapperLocalURL : {}", wrapperLocalURL);

        final File file = new File(wrapperLocalURL());

        if (!wrapperEveryTime() && file.exists()) {
            logger().info("JWrapper artifact is present, skip download.");
            return;
        } else {
            logger().info("JWrapper artifact is missing, make download.");
        }

        file.getParentFile().mkdirs();

        final InputStream input = new URL(wrapperRemoteURL()).openStream();
        final OutputStream output = new FileOutputStream(file);

        IOUtils.copy(input, output);

        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);

        if (file.length() < 1000 * 1000) {
            throw new IllegalStateException("Download failure.");
        }

        logger().info("JWrapper artifact downloaded: {} bytes.", file.length());

    } catch (final Throwable e) {
        logger().error("", e);
        throw new MojoExecutionException("", e);
    }
}

From source file:cz.cvut.fel.integracniportal.extension.SftpChannel.java

public InputStream getFile(String filename) throws SftpException, IOException {
    InputStream inputStream = sftpChannel.get(filename);
    ByteArrayOutputStream clonedStream = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, clonedStream);
    clonedStream.flush();/*from   w w w  . j  av  a  2 s  . com*/
    InputStream result = new ByteArrayInputStream(clonedStream.toByteArray());
    sftpChannel.disconnect();
    return result;
}

From source file:com.yoncabt.ebr.logger.fs.FileSystemReportLogger.java

@Override
public void logReport(ReportRequest request, ReportOutputFormat outputFormat, InputStream reportData)
        throws IOException {
    String uuid = request.getUuid();
    Map<String, Object> reportParams = request.getReportParams();
    File saveDir = new File(EBRConf.INSTANCE.getValue(EBRParams.REPORT_LOGGER_FSLOGGER_PATH, "/tmp"));
    saveDir.mkdirs();/*  ww  w.jav  a  2s  .  c  o  m*/
    boolean compress = EBRConf.INSTANCE.getValue(EBRParams.REPORT_LOGGER_FSLOGGER_COMPRESS, true);
    OutputStream osReport;
    OutputStream osParams;
    if (compress) {
        osReport = new GZIPOutputStream(new FileOutputStream(new File(saveDir, uuid + ".gz")));
        osParams = new GZIPOutputStream(new FileOutputStream(new File(saveDir, uuid + ".json.gz")));
    } else {
        osReport = new FileOutputStream(new File(saveDir, uuid));
        osParams = new FileOutputStream(new File(saveDir, uuid + ".json"));
    }
    IOUtils.copy(reportData, osReport);
    JSONObject jo = new JSONObject(reportParams);
    try (OutputStreamWriter osw = new OutputStreamWriter(osParams, "utf-8")) {
        jo.write(osw);
    }
    osReport.close();
    osParams.close();
}

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

@Test
public void testWriteXMLToFile() throws Exception {
    Document doc = XMLUtil.parseXML(getClass().getResourceAsStream("simple.xml"));
    File f = File.createTempFile("bpelunit", ".xml");

    try {/*from   w  w  w  .j  a  va  2 s .co m*/
        XMLUtil.writeXML(doc, f);
        ByteArrayOutputStream reference = new ByteArrayOutputStream();
        IOUtils.copy(getClass().getResourceAsStream("simple.xml"), reference);

        byte[] actual = FileUtil.readFile(f);

        String referenceString = normalize(reference.toString("UTF-8"));
        String actualString = normalize(new String(actual));

        assertEquals(referenceString, actualString);

    } finally {
        f.delete();
    }
}