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.boundlessgeo.geoserver.api.converters.ResourceMessageConverter.java

@Override
protected void writeInternal(Resource resource, HttpOutputMessage msg)
        throws IOException, HttpMessageNotWritableException {
    InputStream in = resource.in();
    try {/*from   w w w  .ja v  a 2 s.  c  o  m*/
        IOUtils.copy(in, msg.getBody());
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.hs.mail.container.server.socket.SocketConnection.java

public void run() {
    try {/*from   w  ww  . j a  v  a2  s  .c o m*/
        handler.handleConnection(socket);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (socket != null) {
            try {
                IOUtils.closeQuietly(socket.getInputStream());
                IOUtils.closeQuietly(socket.getOutputStream());
                socket.close();
            } catch (IOException e) {
            }
            socket = null;
        }
    }

}

From source file:edu.stanford.hakan.aim4api.utility.EPADConfig.java

private EPADConfig() {
    try {/* www.ja  v  a  2 s  .  com*/
        properties = new Properties();
        File configFile = getConfigurationFile();
        if (!configFile.exists())
            throw new IllegalStateException(
                    "Could not find ePAD configuration file " + configFile.getAbsolutePath());

        FileInputStream fis = new FileInputStream(configFile);
        try {
            properties.load(fis);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    } catch (IOException | IllegalStateException e) {
    }
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Lower level pack operation/*from w w w .  ja v a  2 s. com*/
 * 
 * @param source
 *            source file
 * @param destination
 *            destination file
 * @return the number of bytes processed
 * @throws ArchiveException
 * @throws IOException
 */
public static long pack(String source, File destination) throws ArchiveException, IOException {

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    OutputStream outStream = new FileOutputStream(destination);

    ArchiveOutputStream outputStream;
    try {
        outputStream = factory.createArchiveOutputStream(ArchiveStreamFactory.TAR, outStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(outStream);

        throw e;
    }

    File startDirectory = new File(source);

    RecursiveArchiver archiver = new RecursiveArchiver(outputStream);

    try {
        archiver.archive(startDirectory);
        outputStream.flush();
        outputStream.close();

        outStream.flush();
        outStream.close();

    } catch (IOException e) {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(outputStream);

        throw e;
    }

    long ret = outputStream.getBytesWritten();

    if (logger.isDebugEnabled()) {
        logger.debug("Packed " + ret + " bytes");
    }

    return ret;
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.MiscIcons.java

@Nullable
private static Image loadImage(@Nonnull final String name) {
    if ("empty".equals(name)) {
        final BufferedImage result = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        return result;
    }/*from  ww  w.j  a va2 s .  c  o m*/
    final InputStream in = MiscIcons.class
            .getResourceAsStream("/com/igormaznitsa/mindmap/swing/miscicons/" + name + ".png");
    if (in == null) {
        return null;
    }
    try {
        return ImageIO.read(in);
    } catch (IOException ex) {
        LOGGER.error("IO exception for icon '" + name + '\'');
        return null;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.thoughtworks.go.util.FileDigester.java

public static String md5DigestOfFile(File file) throws IOException {
    FileInputStream stream = null;
    try {/*  ww  w.  ja va2s .  co  m*/
        stream = new FileInputStream(file);
        return md5DigestOfStream(stream);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:com.hyperaware.conference.android.plugin.AssetsSessionFeedbackUrlMapFactory.java

@Override
public SessionFeedbackUrlMap newInstance() throws Exception {
    InputStream is = null;//w  ww.j  a  v a2s.  co  m
    final SessionFeedbackUrlMap map;
    try {
        is = context.getAssets().open(asset);
        final Gson gson = new Gson();
        map = gson.fromJson(new InputStreamReader(is), SessionFeedbackUrlMap.class);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return map;
}

From source file:de.cosmocode.palava.core.inject.PropertiesConverter.java

@Override
public Properties convert(String value, TypeLiteral<?> toType) {
    final URL url = converter.convert(value, URLConverter.LITERAL);
    final Properties properties = new Properties();
    final InputStream stream = openStream(url);

    try {/*from w ww .  j a v  a 2 s.c  o m*/
        properties.load(stream);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IOUtils.closeQuietly(stream);
    }

    return properties;
}

From source file:ch.cyberduck.core.io.CRC32ChecksumCompute.java

@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
    final CRC32 crc32 = new CRC32();
    try {//from   www.j  a  v  a  2  s . c o m
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
            crc32.update(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(),
                e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return new Checksum(HashAlgorithm.crc32, Long.toHexString(crc32.getValue()));
}

From source file:mitm.common.util.MiscIOUtils.java

/**
 * Creates an InputStream of the content of {@link DeferredFileOutputStream}. The output is closed before reading the
 * content. This is required when the {@link DeferredFileOutputStream} uses a file to make sure the file is
 * completely written to disk.//from  w ww  . j a v  a  2s.c  o  m
 *
 * Note: If {@link DeferredFileOutputStream} uses a file, the file will be automatically deleted when #close is
 * called for the InputStream. The caller is responsible for calling close.
 */
@SuppressWarnings("resource")
public static InputStream toInputStream(DeferredFileOutputStream output) throws IOException {
    Check.notNull(output, "output");

    IOUtils.closeQuietly(output);

    return output.isInMemory() ? new ByteArrayInputStream(output.getData())
            : new BufferedInputStream(new DeleteFileOnCloseFileInputStream(output.getFile()));
}