Example usage for java.io Closeable close

List of usage examples for java.io Closeable close

Introduction

In this page you can find the example usage for java.io Closeable close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java

void closeQuietly(Closeable closable) {
    if (closable == null)
        return;//from   ww  w.j a  v a2  s.com
    try {
        closable.close();
    } catch (Throwable ignored) {
    }
}

From source file:io.selendroid.android.impl.AbstractDevice.java

public byte[] takeScreenshot() throws AndroidDeviceException {
    if (device == null) {
        throw new AndroidDeviceException("Device not accessible via ddmlib.");
    }//ww  w. j  a  va  2s . co m
    RawImage rawImage;
    try {
        rawImage = device.getScreenshot();
    } catch (IOException ioe) {
        throw new AndroidDeviceException("Unable to get frame buffer: " + ioe.getMessage());
    } catch (TimeoutException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    } catch (AdbCommandRejectedException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    }

    // device/adb not available?
    if (rawImage == null)
        return null;

    BufferedImage image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);

    int index = 0;
    int IndexInc = rawImage.bpp >> 3;
    for (int y = 0; y < rawImage.height; y++) {
        for (int x = 0; x < rawImage.width; x++) {
            int value = rawImage.getARGB(index);
            index += IndexInc;
            image.setRGB(x, y, value);
        }
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    try {
        if (!ImageIO.write(image, "png", stream)) {
            throw new IOException("Failed to find png writer");
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new AndroidDeviceException(e.getMessage());
    }
    byte[] raw = null;
    try {
        stream.flush();
        raw = stream.toByteArray();
        stream.close();
    } catch (IOException e) {
        throw new RuntimeException("I/O Error while capturing screenshot: " + e.getMessage());
    } finally {
        Closeable closeable = (Closeable) stream;
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    return raw;
}

From source file:org.eclipse.skalli.core.search.LuceneIndex.java

private void closeQuietly(Closeable closable) {
    try {//from  w  w w.ja  v  a  2s  . c o m
        if (closable != null) {
            closable.close();
        }
    } catch (IOException e) {
        LOG.error(MessageFormat.format("Failed to close {0}", closable.getClass().getName()), e);
    }
}

From source file:com.dsi.ant.antplus.pluginsampler.bloodpressure.Activity_BloodPressureSampler.java

private void clearLayoutList() {
    if (!layoutControllerList.isEmpty()) {
        for (Closeable controller : layoutControllerList)
            try {
                controller.close();
            } catch (IOException e) {
                //Never happens
            }/*from  ww  w  . j  a  v  a 2  s . co m*/

        layoutControllerList.clear();
    }
}

From source file:org.opencms.lock.CmsLockUtil.java

/**
 * Utility method for locking and unlocking a set of resources conveniently with the try-with syntax
 * from Java 1.7.<p>// www . j a va 2 s . c o  m
 *
 * This method locks a set of resources and returns a Closeable instance that will unlock the locked resources
 * when its close() method is called.
 *
 * @param cms the CMS context
 * @param resources the resources to lock
 *
 * @return the Closeable used to unlock the resources
 * @throws Exception if something goes wrong
 */
public static Closeable withLockedResources(final CmsObject cms, CmsResource... resources) throws Exception {

    final Map<CmsResource, CmsLockActionRecord> lockMap = Maps.newHashMap();
    Closeable result = new Closeable() {

        @SuppressWarnings("synthetic-access")
        public void close() {

            for (Map.Entry<CmsResource, CmsLockActionRecord> entry : lockMap.entrySet()) {
                if (entry.getValue().getChange() == LockChange.locked) {
                    CmsResource resourceToUnlock = entry.getKey();
                    // the resource may have been moved, so we read it again to get the correct path
                    try {
                        resourceToUnlock = cms.readResource(entry.getKey().getStructureId(),
                                CmsResourceFilter.ALL);
                    } catch (CmsException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                    }
                    try {
                        cms.unlockResource(resourceToUnlock);
                    } catch (CmsException e) {
                        LOG.warn(e.getLocalizedMessage(), e);
                    }
                }

            }
        }
    };
    try {
        for (CmsResource resource : resources) {
            CmsLockActionRecord record = ensureLock(cms, resource);
            lockMap.put(resource, record);
        }
    } catch (CmsException e) {
        result.close();
        throw e;
    }
    return result;
}

From source file:org.buildboost.hudson.plugins.boostscm.BuildBoostSCM.java

private void closeStream(Closeable closable) {
    if (closable != null) {
        try {//from  www .j ava 2s  .c o m
            closable.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:CloudManagerAPI.java

private void closeStream(Closeable closeable) {
    try {/*from w w w.j a  va  2s .c  om*/
        if (closeable != null) {
            closeable.close();
        }
    } catch (Exception e) {
        // intentionally left blank
    }
}

From source file:org.opengeoportal.proxy.controllers.OldDynamicOgcController.java

protected void closeQuietly(Closeable closeable) {
    try {//w  w  w .j  a  v  a2 s . co  m
        closeable.close();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:org.dhatim.io.AbstractOutputStreamResource.java

private void close(final Closeable closeable) {
    if (closeable == null) {
        return;/*from ww  w .j a  va 2 s .c  o m*/
    }

    if (closeable instanceof Flushable) {
        try {
            ((Flushable) closeable).flush();
        } catch (IOException e) {
            log.debug("IOException while trying to flush output resource '" + resourceName + "': ", e);
        }
    }

    try {
        closeable.close();
    } catch (IOException e) {
        log.debug("IOException while trying to close output resource '" + resourceName + "': ", e);
    }
}