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:net.gbmb.collector.impl.TestFinalStorage.java

@Override
public String store(String cid, InputStream content) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(content, bos);//  ww w  .  j  a va  2  s  . c o  m
    IOUtils.closeQuietly(content);
    IOUtils.closeQuietly(bos);
    contents.put(cid, bos.toByteArray());
    return cid;
}

From source file:eionet.gdem.conversion.odf.OpenDocumentUtils.java

/**
 * Returns true, if inputstream is zip file
 * @param input InputStream//from   ww w. ja  va 2  s  . com
 * @return True if InputStream is a zip file.
 */
public static boolean isSpreadsheetFile(InputStream input) {

    ZipInputStream zipStream = null;
    ZipEntry zipEntry = null;
    try {
        zipStream = new ZipInputStream(input);
        while (zipStream.available() == 1 && (zipEntry = zipStream.getNextEntry()) != null) {
            if (zipEntry != null) {
                if ("content.xml".equals(zipEntry.getName())) {
                    // content file found, it is OpenDocument.
                    return true;
                }
            }
        }
    } catch (IOException ioe) {
        return false;
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return false;

}

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

static String convertScanToString(Scan scan) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    DataOutputStream dos = null;//w  ww . jav a 2 s .c o  m
    try {
        dos = new DataOutputStream(out);
        scan.write(dos);
        dos.close();
    } finally {
        IOUtils.closeQuietly(dos);
    }

    return Base64.encodeBytes(out.toByteArray());
}

From source file:b2s.idea.mavenize.TestJarFilenamePool.java

public TestJarFilenamePool() {
    InputStream input = null;// w w w . j  av a2 s  .  c  o m
    try {
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream("intellij-jars.txt");
        filenamePool = new ArrayList(Arrays.asList(IOUtils.toString(input).split("\r\n|\n")));
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:com.smash.revolance.ui.materials.TemplateHelper.java

public static String processTemplate(String templateName, Map<String, Object> variables)
        throws TemplateException, IOException {
    // load template
    Template template = null;/*from w w w. ja  va  2  s .c o  m*/
    InputStreamReader stream = null;
    try {
        InputStream content = TemplateHelper.class.getClassLoader().getResourceAsStream(templateName);
        if (content == null) {
            System.err.println("Unable to find template: " + templateName);
            return "Unable to find template: " + templateName;
        } else {
            stream = new InputStreamReader(content);
            template = new Template("template", stream, null);
        }
    } catch (IOException e) {
        System.err.println("Unable to find template: " + templateName);
        return "Unable to find template: " + templateName;
    } finally {
        IOUtils.closeQuietly(stream);
    }
    // process template
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (template != null) {
        // Fill the template with parameter values
        Writer out = new OutputStreamWriter(baos);
        template.process(variables, out);
        out.flush();
    }
    return baos.toString();
}

From source file:com.github.jrh3k5.mojo.flume.io.ArchiveUtils.java

/**
 * Un-GZIP a file./*from w ww. j av a  2 s . c o  m*/
 * 
 * @param toUnzip
 *            A {@link URL} representing the GZIP file to be unzipped.
 * @param toFile
 *            A {@link File} representing the location to which the unzipped file should be placed.
 * @throws IOException
 *             If any errors occur during the unzipping.
 * @see #gzipFile(File, File)
 */
public static void gunzipFile(URL toUnzip, File toFile) throws IOException {
    if (toFile.exists() && !toFile.isFile()) {
        throw new IllegalArgumentException("Destination file " + toFile
                + " exists, but is not a file and, as such, cannot be written to.");
    }

    GZIPInputStream zipIn = null;
    FileOutputStream fileOut = null;
    try {
        zipIn = new GZIPInputStream(toUnzip.openStream());
        fileOut = new FileOutputStream(toFile);
        IOUtils.copy(zipIn, fileOut);
    } finally {
        IOUtils.closeQuietly(fileOut);
        IOUtils.closeQuietly(zipIn);
    }
}

From source file:com.silverpeas.jcrutil.model.SilverpeasRegister.java

/**
 * Register Silverpeas node types in Jackrabbit
 * @param cndFileName the file containing the nodes definitions.
 * @throws NamespaceException// w w w  .j  ava  2  s . co  m
 * @throws UnsupportedRepositoryOperationException
 * @throws RepositoryException
 * @throws ParseException
 * @throws IOException
 */
public static void registerNodeTypes(String cndFileName)
        throws RepositoryException, ParseException, IOException {
    FileReader fileReader = new FileReader(cndFileName);
    try {
        registerNodeTypes(fileReader);
    } finally {
        IOUtils.closeQuietly(fileReader);
    }
}

From source file:it.geosolutions.geoserver.jms.impl.handlers.DocumentFileHandler.java

@Override
public boolean synchronize(DocumentFile event) throws Exception {
    FileOutputStream fout = null;
    try {// w w w  .  jav a 2s . com
        fout = new FileOutputStream(event.getPath());
        xstream.toXML(event.getBody(), fout);
        return true;
    } catch (IOException e) {
        if (LOGGER.isLoggable(java.util.logging.Level.SEVERE))
            LOGGER.severe(e.getLocalizedMessage());
        throw e;
    } finally {
        IOUtils.closeQuietly(fout);
    }
}

From source file:com.navercorp.volleyextensions.cache.universalimageloader.disc.CountingInputStreamTest.java

@Test
public void bytesReadShouldBeSameAsContentLength() throws IOException {
    // Given// w  ww  .j  ava2s .  c  o m
    InputStream is = openFromClassPath("test.txt");
    CountingInputStream cis = new CountingInputStream(is);
    // When
    String content = IOUtils.toString(cis);
    IOUtils.closeQuietly(cis);
    //Then
    assertThat(content, is("Cached Data"));
    assertThat(cis.bytesRead, is(11));
}

From source file:de.weltraumschaf.registermachine.bytecode.ByteCodeReader.java

/**
 * Close input stream quietly.
 */
public void close() {
    IOUtils.closeQuietly(input);
}