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.mgmtp.perfload.core.common.util.PropertiesUtils.java

/**
 * Loads properties as map from the given classpath resource using the current context
 * classloader. This method delegates to {@link #loadProperties(Reader)}.
 * //ww w  .  jav  a2 s .c o m
 * @param resource
 *            the resource
 * @param encoding
 *            the encoding to use
 * @param exceptionIfNotFound
 *            if {@code true}, an {@link IllegalStateException} is thrown if the resource cannot
 *            be loaded; otherwise an empty map is returned
 * @return the map
 */
public static PropertiesMap loadProperties(final String resource, final String encoding,
        final boolean exceptionIfNotFound) throws IOException {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Reader reader = null;
    try {
        InputStream is = cl.getResourceAsStream(resource);
        if (is == null) {
            if (exceptionIfNotFound) {
                throw new IOException("InputStream is null. Properties not found: " + resource);
            }
            return new PropertiesMap();
        }
        reader = new InputStreamReader(is, encoding);
        return loadProperties(reader);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.comcast.dawg.test.base.TestBase.java

/**
 * After class method to remove all the added test STB's.
 *///from www. ja v a2s .  c  o m
@AfterClass
public void deleteStbs() {
    try {
        if (!testStbs.isEmpty()) {
            dawgHouseClient.deleteByIds(testStbs.keySet());
        }
    } finally {
        IOUtils.closeQuietly(dawgHouseClient);
    }
}

From source file:com.oneis.appserver.FileResponse.java

public void writeRangeToOutputStream(OutputStream stream, long offset, long length) throws IOException {
    if (length == 0) {
        return;/*from   w  w w.  j  a v  a  2 s. co m*/
    }
    FileInputStream in = new FileInputStream(file);
    try {
        in.skip(offset);
        byte[] buffer = new byte[4096];
        long count = 0;
        int n = 0;
        while (count < length && (-1 != (n = in.read(buffer, 0,
                (int) (((length - count) > 4096) ? 4096 : (length - count)))))) {
            stream.write(buffer, 0, n);
            count += n;
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.theatlantic.highcharts.export.SVGRenderer.java

@Override
public void render() {
    if (getChartOptions() == null) {
        throw (new RuntimeException("chartOptions must not be null"));
    }/*from   w ww. ja  v  a2s.c o  m*/

    ByteArrayInputStream byteStream = null;
    try {
        final String svg = internal.getSVG(getChartOptions());
        if (svg == null) {
            throw (new RuntimeException("cannot generate svg"));
        }
        byteStream = new ByteArrayInputStream(svg.getBytes());
        InputStreamReader reader = new InputStreamReader(byteStream);
        IOUtils.copy(reader, getOutputStream(), "UTF-8");
    } catch (IOException e) {
        throw (new RuntimeException(e));
    } finally {
        IOUtils.closeQuietly(byteStream);
    }
}

From source file:c3.ops.priam.compress.SnappyCompression.java

private void decompress(InputStream input, OutputStream output) throws IOException {
    SnappyInputStream is = new SnappyInputStream(new BufferedInputStream(input));
    byte data[] = new byte[BUFFER];
    BufferedOutputStream dest1 = new BufferedOutputStream(output, BUFFER);
    try {//from  w w  w  .  ja v a 2  s. com
        int c;
        while ((c = is.read(data, 0, BUFFER)) != -1) {
            dest1.write(data, 0, c);
        }
    } finally {
        IOUtils.closeQuietly(dest1);
        IOUtils.closeQuietly(is);
    }
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive.//from   www  . j  a va2 s. c  o  m
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:com.simpligility.maven.plugins.android.asm.AndroidTestFinder.java

public static boolean containsAndroidTests(File classesBaseDirectory) throws MojoExecutionException {

    if (classesBaseDirectory == null || !classesBaseDirectory.isDirectory()) {
        throw new IllegalArgumentException("classesBaseDirectory must be a valid directory!");
    }//from  w  ww.  ja va  2  s.c  om

    final List<File> classFiles = findEligebleClassFiles(classesBaseDirectory);
    final DescendantFinder descendantFinder = new DescendantFinder(TEST_PACKAGES);
    final AnnotatedFinder annotationFinder = new AnnotatedFinder(TEST_PACKAGES);

    for (File classFile : classFiles) {
        ClassReader classReader;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(classFile);
            classReader = new ClassReader(inputStream);

            classReader.accept(descendantFinder,
                    ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
            classReader.accept(annotationFinder,
                    ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
        } catch (IOException e) {
            throw new MojoExecutionException("Error reading " + classFile + ".\nCould not determine whether it "
                    + "contains tests. Please specify with plugin config parameter "
                    + "<enableIntegrationTest>true|false</enableIntegrationTest>.", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    return descendantFinder.isDescendantFound() || annotationFinder.isDescendantFound();
}

From source file:com.haulmont.cuba.desktop.sys.MainWindowProperties.java

public void save() {
    Properties properties = new Properties();
    saveProperties(properties);//  ww w.java  2s . co m
    try {
        File file = new File(AppBeans.get(Configuration.class).getConfig(GlobalConfig.class).getDataDir(),
                "main-window.properties");
        FileOutputStream stream = FileUtils.openOutputStream(file);
        try {
            properties.store(stream, "Main window properties");
        } finally {
            IOUtils.closeQuietly(stream);
        }
    } catch (IOException e) {
        log.error("Error saving main window location", e);
    }
}

From source file:com.alibaba.otter.shared.common.utils.cmd.StreamAppender.java

public void writeInput(final InputStream err, final InputStream out) {
    errWriter = new Thread() {

        BufferedReader reader = new BufferedReader(new InputStreamReader(err));

        public void run() {
            try {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    output.println(line);
                }//www.j ava2  s .c o m
            } catch (IOException e) {
                //ignore
            } finally {
                output.flush();// ?flush
                IOUtils.closeQuietly(reader);
            }
        }
    };
    outWriter = new Thread() {

        BufferedReader reader = new BufferedReader(new InputStreamReader(out));

        public void run() {
            try {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    output.println(line);
                }
            } catch (IOException e) {
                //ignore
            } finally {
                output.flush();// ?flush
                IOUtils.closeQuietly(reader);
            }
        }
    };
    errWriter.setDaemon(true);
    outWriter.setDaemon(true);
    errWriter.start();
    outWriter.start();
}

From source file:com.px100systems.util.serialization.DataStream.java

public void close() {
    if (dos != null)
        IOUtils.closeQuietly(dos);
    if (dis != null)
        IOUtils.closeQuietly(dis);
}