Example usage for javax.imageio.stream ImageInputStream close

List of usage examples for javax.imageio.stream ImageInputStream close

Introduction

In this page you can find the example usage for javax.imageio.stream ImageInputStream close.

Prototype

void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ImageInputStream imageStream = ImageIO.createImageInputStream(new URL("").openStream());
    Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
    ImageReader reader = null;/*from w  w w.j av  a2  s .  co  m*/
    if (!readers.hasNext()) {
        imageStream.close();
        return;
    } else {
        reader = readers.next();
    }
    String formatName = reader.getFormatName();
    if (!formatName.equalsIgnoreCase("jpeg") && !formatName.equalsIgnoreCase("png")
            && !formatName.equalsIgnoreCase("gif")) {
        imageStream.close();
        return;
    }
    reader.setInput(imageStream, true, true);
    BufferedImage theImage = reader.read(0);
    reader.dispose();
    imageStream.close();
}

From source file:Main.java

private static String getFormatName(Object o) {
    try {//from w w w  . java 2s  .  c om
        ImageInputStream iis = ImageIO.createImageInputStream(o);
        Iterator iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            return null;
        }
        ImageReader reader = (ImageReader) iter.next();
        iis.close();

        return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
}

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Validate if is holds an image. This method closes is.
 * //from   w w w . j av  a2 s . c o m
 * @param is image to validate
 * @throws IOException on i/o error.
 * @throws IllegalArgumentException if is doesn't represent an image
 */
public static void validateImage(final InputStream is) throws IOException, IllegalArgumentException {
    try {
        final ImageInputStream iis = ImageIO.createImageInputStream(is);
        final Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
        iis.close();
        if (!i.hasNext()) {
            throw new IllegalArgumentException("don't know how to read " + "that type of images");
        }
    } finally {
        is.close();
    }
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static void closeQuietly(ImageInputStream inStream) {
    if (inStream != null) {
        try {/*w w w . ja v  a2s  .  co m*/
            inStream.close();
        } catch (IOException ignore) {

        }
    }
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static void closeQuietly(com.alibaba.simpleimage.io.ImageInputStream stream) {
    if (stream != null) {
        try {/*from   w w w  . j  a  v  a 2  s  . c o  m*/
            stream.close();
        } catch (IOException ignore) {

        }
    }
}

From source file:Main.java

public static BufferedImage readFragment(InputStream stream, Rectangle rect) throws IOException {
    ImageInputStream imageStream = ImageIO.createImageInputStream(stream);
    ImageReader reader = ImageIO.getImageReaders(imageStream).next();
    ImageReadParam param = reader.getDefaultReadParam();

    param.setSourceRegion(rect);/*  ww  w  . j a  v a  2s.c o  m*/
    reader.setInput(imageStream, true, true);
    BufferedImage image = reader.read(0, param);

    reader.dispose();
    imageStream.close();

    return image;
}

From source file:VASSAL.tools.io.IOUtils.java

/**
 * Close an {@link ImageInputStream} unconditionally. Equivalent to
 * calling <code>s.close()</code> when <code>s</code> is nonnull.
 * {@link IOException}s are quietly logged, as there is generally
 * nothing that can be done about exceptions on closing.
 *
 * @param s a (possibly <code>null</code>) <code>ImageInputStream</code>
 *//*from w  w  w.ja  va  2s  .  c  om*/
// Why doesn't ImageInputStream implement Closeable? Argh!
public static void closeQuietly(ImageInputStream s) {
    if (s == null)
        return;

    try {
        s.close();
    } catch (IOException e) {
        // ignore

        // Note that ImageInputStreamImpl.close() rather ridiculously throws
        // an IOException if the stream is already closed. This is always done
        // via ImageInputStreamImpl.checkClosed().
    }
}

From source file:ImageUtilities.java

/**
 * Returns the format of an image (as string).
 * /*from  ww w  .j a v a  2s  . c  o m*/
 * @param stream
 *            image source stream
 * 
 * @return format stream (e.g. "JPEG")
 * 
 * @throws IOException
 *             if an I/O error occured
 */
public static String getFormat(InputStream stream) throws IOException {
    ImageInputStream iis = ImageIO.createImageInputStream(stream);
    Iterator iter = ImageIO.getImageReaders(iis);
    if (!iter.hasNext()) {
        throw new IOException("Unsupported image format!");
    }
    ImageReader reader = (ImageReader) iter.next();
    iis.close();
    return reader.getFormatName();
}

From source file:lucee.runtime.img.ImageUtil.java

public static void closeEL(ImageInputStream iis) {
    try {//from   w  ww. j  av  a  2s.  c  o  m
        if (iis != null)
            iis.close();
    } catch (Throwable t) {
    }

}

From source file:common.utils.ImageUtils.java

/**
 * closes iis/*from   ww  w  .j  ava2s. c o m*/
 * @param iis
 * @return image from given stream or null if no readers found
 * @throws java.io.IOException
 */
public static BufferedImageHolder readImage(ImageInputStream iis) throws IOException {
    ImageReader reader = getImageReader(iis);
    if (reader == null) {
        iis.close();
        return null;
    }
    reader.setInput(iis, true, true);
    ImageReadParam param = reader.getDefaultReadParam();
    String format_name = reader.getFormatName();
    BufferedImage bi;
    try {
        bi = reader.read(0, param);
    } finally {
        reader.dispose();
        iis.close();
    }
    return new BufferedImageHolder(bi, format_name);
}