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.alibaba.simpleimage.codec.jpeg.JPEGDecodePerformanceTest.java

protected void run(DecodeFacade decoder, File rootDir, String filename, int times, int frequency)
        throws Exception {
    long start, end, total = 0L;
    if (rootDir == null) {
        rootDir = imgDir;/*from ww  w .  j  a v a2s  .  com*/
    }

    FileInputStream inputStream = new FileInputStream(new File(rootDir, filename));

    ByteArrayOutputStream temp = new ByteArrayOutputStream();
    IOUtils.copy(inputStream, temp);
    IOUtils.closeQuietly(inputStream);

    InputStream img = temp.toInputStream();
    temp = null;

    System.out.println("***********Performance Test**************");

    for (int i = 0; i < frequency; i++) {
        // System.gc();
        // Thread.sleep(5 * 1000L);

        start = System.currentTimeMillis();

        for (int t = 0; t < times; t++) {
            // File img = imgs[t % imgs.length];
            img.reset();
            BufferedImage bi = decoder.decode(img);
            bi.getHeight();
            bi = null;
        }

        end = System.currentTimeMillis();

        total += (end - start);
    }

    System.out.printf("Decoder : %s \n", decoder.getName());
    System.out.println("Image : " + filename);
    System.out.printf("Times : %d\n", times);
    System.out.printf("Frequency : %d\n", frequency);
    System.out.printf("Total time : %d ms\n", total);
    System.out.printf("Average time : %.2f ms\n", ((double) total / (times * frequency)));
}

From source file:fr.cph.chicago.parser.JsonParser.java

@NonNull
public List<BikeStation> parseStations(@NonNull final InputStream stream) throws ParserException {
    try {//from   w w w .java2s  . com
        final DivvyDTO divvyJson = MAPPER.readValue(stream, new TypeReference<DivvyDTO>() {
        });
        return divvyJson.getStations();
    } catch (final IOException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:io.wcm.caravan.io.http.response.InputStreamBody.java

@Override
public String asString() throws IOException {
    try {//from  w  ww. j  a  v a2s .  co m
        return IOUtils.toString(inputStream, Charsets.UTF_8);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.lithium.flow.filer.hash.ClientHashFiler.java

@Override
public void close() throws IOException {
    IOUtils.closeQuietly(client);
    super.close();
}

From source file:io.apiman.test.common.mock.EchoResponse.java

/**
 * Create an echo response from the inbound information in the http server
 * request./*  ww w.ja  v a2  s .  c o m*/
 * @param request
 * @param withBody 
 * @return a new echo response
 */
public static EchoResponse from(HttpServletRequest request, boolean withBody) {
    EchoResponse response = new EchoResponse();
    response.setMethod(request.getMethod());
    response.setResource(request.getRequestURI());
    response.setUri(request.getRequestURI());
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String value = request.getHeader(name);
        response.getHeaders().put(name, value);
    }
    if (withBody) {
        long totalBytes = 0;
        InputStream is = null;
        try {
            is = request.getInputStream();
            MessageDigest sha1 = MessageDigest.getInstance("SHA1"); //$NON-NLS-1$
            byte[] data = new byte[1024];
            int read = 0;
            while ((read = is.read(data)) != -1) {
                sha1.update(data, 0, read);
                totalBytes += read;
            }
            ;

            byte[] hashBytes = sha1.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            String fileHash = sb.toString();

            response.setBodyLength(totalBytes);
            response.setBodySha1(fileHash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return response;
}

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

/**
 * Creates ODS Spreadsheet//from w  w  w  .j a v  a 2 s.c o m
 * @param sIn Input String
 * @param sOut Output String
 * @throws GDEMException If an error occurs.
 */
public void makeSpreadsheet(String sIn, String sOut) throws GDEMException {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(sOut);
        makeSpreadsheet(sIn, out);
    } catch (Exception e) {
        throw new GDEMException(
                "ErrorConversionHandler - couldn't save the OpenDocumentSpreadheet file: " + e.toString(), e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.noorq.casser.test.integration.build.BuildProperties.java

private void loadFromClasspath(String resourceName) {
    InputStream in = getClass().getResourceAsStream("/" + resourceName);
    if (in == null) {
        throw new RuntimeException("resource is not found in classpath: " + resourceName);
    }/*from www.  j a va 2 s.c om*/
    try {
        props.load(in);
    } catch (Exception x) {
        throw new RuntimeException(x);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:lineage2.commons.configuration.ExProperties.java

/**
 * Method load.//from  www.j a v  a 2 s  . c  o m
 * @param file File
 * @throws IOException
 */
public void load(File file) throws IOException {
    InputStream is = null;
    try {
        load(is = new FileInputStream(file));
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.blogspot.jabelarminecraft.movinglightsource.VersionChecker.java

@Override
public void run() {
    InputStream in = null;//from  w w  w  .  j a  v  a 2  s . c  o m
    try {
        in = new URL(
                "https://raw.githubusercontent.com/jabelar/MovingLightSource-1.8/master/src/main/java/com/blogspot/jabelarminecraft/movinglightsource/version_file")
                        .openStream();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        latestVersion = IOUtils.readLines(in).get(0); // toString(in);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(in);
    }
    // DEBUG
    System.out.println(
            "Current version is " + MovingLightSource.MODVERSION + " and latest version is " + latestVersion);
    isLatestVersion = MovingLightSource.MODVERSION.compareTo(latestVersion) >= 0;
    System.out.println("Are you running latest version = " + isLatestVersion);
}

From source file:com.jwrapper.maven.jwrapper.JWrapperDownloadMojo.java

@Override
public void execute() throws MojoExecutionException {
    try {/*from  ww w.  ja  v  a2s  . c  o m*/

        final String wrapperRemoteURL = wrapperRemoteURL();
        final String wrapperLocalURL = wrapperLocalURL();

        logger().info("wrapperRemoteURL: {}", wrapperRemoteURL);
        logger().info("wrapperLocalURL : {}", wrapperLocalURL);

        final File file = new File(wrapperLocalURL());

        if (!wrapperEveryTime() && file.exists()) {
            logger().info("JWrapper artifact is present, skip download.");
            return;
        } else {
            logger().info("JWrapper artifact is missing, make download.");
        }

        file.getParentFile().mkdirs();

        final InputStream input = new URL(wrapperRemoteURL()).openStream();
        final OutputStream output = new FileOutputStream(file);

        IOUtils.copy(input, output);

        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);

        if (file.length() < 1000 * 1000) {
            throw new IllegalStateException("Download failure.");
        }

        logger().info("JWrapper artifact downloaded: {} bytes.", file.length());

    } catch (final Throwable e) {
        logger().error("", e);
        throw new MojoExecutionException("", e);
    }
}