Search in sources :

Example 16 with ImageReader

use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.

the class CrashAfterDispose method main.

public static void main(String[] args) throws IOException {
    InputStream bais = new ByteArrayInputStream(new byte[100]);
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    // find the JPEG reader
    ImageReader reader = null;
    Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
    if (readers.hasNext()) {
        reader = (ImageReader) readers.next();
    } else {
        throw new RuntimeException("Unable to find a reader!");
    }
    // dispose the reader, then poke and prod it... the reader should
    // throw exceptions (which will be caught by this test), but it
    // should not crash the VM
    reader.dispose();
    try {
        reader.setInput(iis);
    } catch (IllegalStateException e) {
    }
    try {
        reader.read(0);
    } catch (IllegalStateException e) {
    }
    try {
        reader.abort();
    } catch (IllegalStateException e) {
    }
    try {
        reader.reset();
    } catch (IllegalStateException e) {
    }
    try {
        reader.dispose();
    } catch (IllegalStateException e) {
    }
    // find the JPEG writer
    ImageWriter writer = null;
    Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
    if (writers.hasNext()) {
        writer = (ImageWriter) writers.next();
    } else {
        throw new RuntimeException("Unable to find a writer!");
    }
    // set up output stream
    OutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    // dispose the writer, then poke and prod it... the writer should
    // throw exceptions (which will be caught by this test), but it
    // should not crash the VM
    writer.dispose();
    try {
        writer.setOutput(ios);
    } catch (IllegalStateException e) {
    }
    try {
        writer.write(bi);
    } catch (IllegalStateException e) {
    }
    try {
        writer.abort();
    } catch (IllegalStateException e) {
    }
    try {
        writer.reset();
    } catch (IllegalStateException e) {
    }
    try {
        writer.dispose();
    } catch (IllegalStateException e) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 17 with ImageReader

use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.

the class TruncatedImageWarningTest method main.

public static void main(String[] args) throws IOException {
    String sep = System.getProperty("file.separator");
    String dir = System.getProperty("test.src", ".");
    String filePath = dir + sep + fileName;
    System.out.println("Test file: " + filePath);
    File f = new File(filePath);
    ImageInputStream in = ImageIO.createImageInputStream(f);
    ImageReader reader = ImageIO.getImageReaders(in).next();
    TruncatedImageWarningTest twt = new TruncatedImageWarningTest();
    reader.addIIOReadWarningListener(twt);
    reader.setInput(in);
    reader.read(0);
    if (!twt.receivedWarning) {
        throw new RuntimeException("No expected warning");
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Example 18 with ImageReader

use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.

the class StreamResetTest method main.

public static void main(String[] args) {
    IOException expectedException = null;
    TestStream iis = new TestStream();
    ImageReader wbmp = ImageIO.getImageReadersByFormatName("WBMP").next();
    if (wbmp == null) {
        System.out.println("No WBMP reader: skip the test");
        return;
    }
    ImageReaderSpi spi = wbmp.getOriginatingProvider();
    iis.checkPosition();
    try {
        spi.canDecodeInput(iis);
    } catch (IOException e) {
        expectedException = e;
    }
    if (expectedException == null) {
        throw new RuntimeException("Test FAILED: stream was not used");
    }
    iis.checkPosition();
    System.out.println("Test PASSED");
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 19 with ImageReader

use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.

the class ValidWbmpTest method main.

public static void main(String[] args) {
    try {
        String[] formats = { "JPEG", "PNG", "BMP" };
        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = img.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.black);
        g.fillRect(10, 10, 80, 80);
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("WBMP").next();
        if (ir == null) {
            throw new RuntimeException("No readers for WBMP format!");
        }
        for (int i = 0; i < formats.length; i++) {
            System.out.println("Test " + formats[i] + " stream...");
            boolean passed = false;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, formats[i], baos);
            baos.close();
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ImageInputStream iis = null;
            iis = ImageIO.createImageInputStream(bais);
            ir.setInput(iis);
            try {
                BufferedImage res = ir.read(0);
            } catch (IIOException e) {
                StackTraceElement[] stack = e.getStackTrace();
                if (ir.getClass().getName().equals(stack[0].getClassName()) && "readHeader".equals(stack[0].getMethodName())) {
                    passed = true;
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
            if (!passed) {
                throw new RuntimeException("Test failed!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IIOException(javax.imageio.IIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOException(javax.imageio.IIOException) Graphics(java.awt.Graphics) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader)

Example 20 with ImageReader

use of javax.imageio.ImageReader in project jdk8u_jdk by JetBrains.

the class EmptyInputWbmpMetadataTest method main.

public static void main(String[] args) {
    boolean isPassed = false;
    ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(fmt).next();
    if (ir == null) {
        throw new RuntimeException("No available reader for " + fmt);
    }
    IIOMetadata meta = null;
    try {
        meta = ir.getImageMetadata(0);
    } catch (IllegalStateException e) {
        System.out.println("Correct exception was thrown. Test passed.");
        isPassed = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (!isPassed) {
        throw new RuntimeException("The IllegalStateException was not thrown." + "Test failed.");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Aggregations

ImageReader (javax.imageio.ImageReader)59 BufferedImage (java.awt.image.BufferedImage)32 ImageInputStream (javax.imageio.stream.ImageInputStream)32 IOException (java.io.IOException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ImageReadParam (javax.imageio.ImageReadParam)14 ImageWriter (javax.imageio.ImageWriter)13 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 InputStream (java.io.InputStream)10 Iterator (java.util.Iterator)10 IIOMetadata (javax.imageio.metadata.IIOMetadata)10 IIOImage (javax.imageio.IIOImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)7 File (java.io.File)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)4 Graphics (java.awt.Graphics)3 Graphics2D (java.awt.Graphics2D)3