Example usage for org.apache.commons.io IOUtils closeQuietly

List of usage examples for org.apache.commons.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(OutputStream output) 

Source Link

Document

Unconditionally close an OutputStream.

Usage

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Reads a file into a String./*from   www.  j a v  a 2s  .  c o m*/
 *
 * @param file
 * @param step
 * @return the resulting String
 */
public static String readFileToString(final File file, final Step step) {
    String canonicalPath = null;
    String result = null;
    InputStream inputStream = null;
    try {
        canonicalPath = file.getCanonicalPath();
        inputStream = new FileInputStream(file);
        result = IOUtils.toString(inputStream);
    } catch (IOException e) {
        throw new StepExecutionException("Could not find/read \"" + canonicalPath + "\".", step);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return result;
}

From source file:com.hw.util.CompressUtils.java

public static final void zip(String compressPath, String[] needCompressPaths) {
    File compressFile = new File(compressPath);

    List<File> files = new ArrayList<File>();
    for (String needCompressPath : needCompressPaths) {
        File needCompressFile = new File(needCompressPath);
        if (!needCompressFile.exists()) {
            continue;
        }//from   w  w w.j  a  va 2  s . c  om
        files.add(needCompressFile);
    }
    try {
        ZipArchiveOutputStream zaos = null;
        try {
            zaos = new ZipArchiveOutputStream(compressFile);
            zaos.setUseZip64(Zip64Mode.AsNeeded);
            zaos.setEncoding("GBK");

            for (File file : files) {
                addFilesToCompression(zaos, file, "");
            }

        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(zaos);
        }
    } catch (Exception e) {
        FileUtils.deleteQuietly(compressFile);
        throw new RuntimeException("", e);
    }
}

From source file:io.jenkins.lib.findbugs.samples.CloseQuietlyInsteadOfLoggingDetector.ApacheIOUtils.java

static public void foo() throws FileNotFoundException {
    InputStream istream = new FileInputStream(Jenkins.getActiveInstance().root);
    IOUtils.closeQuietly(istream);
}

From source file:com.adaptris.core.stubs.MessageHelper.java

public static AdaptrisMessage createMessage(AdaptrisMessageFactory factory, String filename)
        throws IOException {
    AdaptrisMessage m = factory.newMessage();
    if (m instanceof FileBackedMessage) {
        ((FileBackedMessage) m).initialiseFrom(new File(filename));
    } else {//from ww w . j a v a 2 s . c o  m
        OutputStream out = null;
        InputStream in = null;
        try {
            in = new FileInputStream(new File(filename));
            out = m.getOutputStream();
            IOUtils.copy(in, out);
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }
    return m;
}

From source file:com.moz.fiji.hive.utils.ByteWritable.java

public static byte[] serialize(Writable writable) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataOutputStream dataOut = null;
    try {/*  w  w w. jav a2s .  co  m*/
        dataOut = new DataOutputStream(out);
        writable.write(dataOut);
        return out.toByteArray();
    } finally {
        IOUtils.closeQuietly(dataOut);
    }
}

From source file:com.daphne.es.maintain.editor.web.controller.utils.CompressUtils.java

public static final void zip(String compressPath, String[] needCompressPaths) {
    File compressFile = new File(compressPath);

    List<File> files = Lists.newArrayList();
    for (String needCompressPath : needCompressPaths) {
        File needCompressFile = new File(needCompressPath);
        if (!needCompressFile.exists()) {
            continue;
        }//w w w  .jav  a  2  s .  co m
        files.add(needCompressFile);
    }
    try {
        ZipArchiveOutputStream zaos = null;
        try {
            zaos = new ZipArchiveOutputStream(compressFile);
            zaos.setUseZip64(Zip64Mode.AsNeeded);
            zaos.setEncoding("GBK");

            for (File file : files) {
                addFilesToCompression(zaos, file, "");
            }

        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(zaos);
        }
    } catch (Exception e) {
        FileUtils.deleteQuietly(compressFile);
        throw new RuntimeException("", e);
    }
}

From source file:com.iyonger.apm.web.util.FileUtils.java

/**
 * Copy the given resource to the given file.
 *
 * @param resourcePath resource path/*  w  w  w . j a v  a2s. c  o  m*/
 * @param file         file to write
 * @since 3.2
 */
public static void copyResourceToFile(String resourcePath, File file) {
    InputStream io = null;
    FileOutputStream fos = null;
    try {
        io = new ClassPathResource(resourcePath).getInputStream();
        fos = new FileOutputStream(file);
        IOUtils.copy(io, fos);
    } catch (IOException e) {
        LOGGER.error("error while writing {}", resourcePath, e);
    } finally {
        IOUtils.closeQuietly(io);
        IOUtils.closeQuietly(fos);
    }

}

From source file:de.ocarthon.core.utility.GzipCompression.java

public static void compress(String input, OutputStream os) throws IOException {
    GZIPOutputStream gos = null;// w w  w  . j  a v a2  s . c  o  m
    PrintWriter pw = null;

    try {
        gos = new GZIPOutputStream(os);
        pw = new PrintWriter(gos);
        pw.write(input);
    } finally {
        IOUtils.closeQuietly(pw);
        IOUtils.closeQuietly(gos);
    }
}

From source file:com.betfair.cougar.codegen.FileUtil.java

/**
 * Copy the given resource to the given file.
 *
 * @param resourceName name of resource to copy
 * @param destination file/*from  ww w . j a v a 2  s. c  o  m*/
 */
public static void resourceToFile(String resourceName, File dest, Class src) {

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.getClassLoader().getResourceAsStream(resourceName);
        if (is == null) {
            throw new RuntimeException("Could not load resource: " + resourceName);
        }
        dest.getParentFile().mkdirs();
        os = new FileOutputStream(dest);

        IOUtils.copy(is, os);

    } catch (Exception e) {
        throw new RuntimeException(
                "Error copying resource '" + resourceName + "' to file '" + dest.getPath() + "': " + e, e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:de.shadowhunt.subversion.internal.AbstractRepositoryDownloadIT.java

public static void assertEquals(final String message, final InputStream expected, final InputStream actual)
        throws Exception {
    try {//from   w  w w. j ava  2 s.  co  m
        Assert.assertEquals(message, IOUtils.toString(expected).trim(), IOUtils.toString(actual).trim());
    } finally {
        IOUtils.closeQuietly(expected);
        IOUtils.closeQuietly(actual);
    }
}