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.aestasit.markdown.BaseTest.java

protected static InputStream allTestData() throws IOException {
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    for (String fileName : allTestFiles()) {
        IOUtils.write(IOUtils.toString(testData(fileName)), data);
    }/*w  ww.jav  a2  s. com*/
    IOUtils.closeQuietly(data);
    return new ByteArrayInputStream(data.toByteArray());
}

From source file:com.formkiq.core.util.Resources.java

/**
 * Gets a file from classpath as bytes./* w  w w . ja  va 2  s . c  o m*/
 * @param file String
 * @return byte[]
 * @throws IOException IOException
 */
public static byte[] getResourceAsBytes(final String file) throws IOException {
    InputStream is = getResourceAsInputStream(file);
    try {
        byte[] bytes = IOUtils.toByteArray(is);
        return bytes;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:net.orpiske.tcs.utils.compression.Compressor.java

/**
 * Compress a string in GZIP format//from www.j a v a 2  s .  com
 * @param text the string to compress
 * @return An array of compressed bytes
 * @throws IOException if unable to compress it
 */
public static byte[] compress(final String text) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = null;

    try {
        gzipOutputStream = new GZIPOutputStream(outputStream);
        gzipOutputStream.write(text.getBytes());
        gzipOutputStream.close();

        return outputStream.toByteArray();
    } finally {
        IOUtils.closeQuietly(gzipOutputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:fitnesse.FitNesseVersion.java

private static Properties getBuildProperties() {
    Properties buildProperties = new Properties();
    InputStream propertyStream = null;
    Reader propertyReader = null;
    try {//from w  ww.j a  va  2s  .  c o  m
        propertyStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("fitnesse/build.properties");
        propertyReader = new InputStreamReader(propertyStream);
        buildProperties.load(propertyReader);
    } catch (NullPointerException e) {
        throw new RuntimeException(
                "Could not load build.properties from the classpath.  Are you sure the jar is packaged correctly?",
                e);
    } catch (IOException e) {
        throw new RuntimeException(
                "Could not load build.properties from the classpath.  Are you sure the jar is packaged correctly?",
                e);
    } finally {
        IOUtils.closeQuietly(propertyReader);
        IOUtils.closeQuietly(propertyStream);
    }
    return buildProperties;
}

From source file:com.lightboxtechnologies.nsrl.SmallTableLoader.java

protected static void load(FileSystem fs, String filename, LineHandler lh, RecordLoader loader)
        throws IOException {
    InputStream in = null;/*from  w ww . j a v  a 2s. c om*/
    try {
        in = fs.open(new Path(filename));
        loader.load(in, lh);
        in.close();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.adguard.compiler.UrlUtils.java

public static String downloadString(URL url, String encoding) throws IOException {

    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {// www .j  a  v  a  2  s . c o m
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        inputStream = connection.getInputStream();
        return IOUtils.toString(inputStream, encoding);
    } finally {
        IOUtils.closeQuietly(inputStream);
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:iox.refused.util.TextUtil.java

public static String loadfile(ServletContext context, String filename) {
    InputStream is = context.getResourceAsStream(filename);
    if (is == null) {
        logger.log(Level.SEVERE, "<loadfile> unable to load " + filename);
        return "";
    }//from w w  w .  j  a va2s  .c  o  m
    String data;
    try {
        data = IOUtils.toString(is, CS);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "<loadfile> unable to load " + filename, e);
        data = "";
    }
    IOUtils.closeQuietly(is);
    return data;
}

From source file:latexstudio.editor.pdf.PDFService.java

public static void closeDocument() {
    if (inputPDF != null) {
        IOUtils.closeQuietly(inputPDF);
    }
}

From source file:com.l2jfree.security.HexID.java

/**
 * Save hexadecimal ID of the server in the properties file.
 * /*  w w w .  j a  v a2 s.  c om*/
 * @param string (String) : hexadecimal ID of the server to store
 * @param fileName (String) : name of the properties file
 */
public static void saveHexid(String string, String fileName) {
    OutputStream out = null;
    try {
        out = new FileOutputStream(fileName);

        final Properties hexSetting = new Properties();
        hexSetting.setProperty("HexID", string);
        hexSetting.store(out, "the hexID to auth into login");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:cn.vlabs.clb.api.io.FileUtil.java

public static void copy(String from, String to) {
    FileInputStream in = null;//from  w ww  .  java 2  s . c  o m
    FileOutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        copy(in, out);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}