Search in sources :

Example 56 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class EndWriteSequenceTest method main.

public static void main(String[] args) throws IOException {
    ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
    boolean gotCorrectException = false;
    /**
         * check statement: "Throws: IllegalStateException -
         * if the output has not been set ...."
         */
    try {
        // to be shure that output is null
        w.reset();
        w.endWriteSequence();
    } catch (IllegalStateException e) {
        gotCorrectException = true;
    } catch (Throwable e) {
        throw new RuntimeException("Test failed.", e);
    }
    if (!gotCorrectException) {
        throw new RuntimeException("Test failed.");
    }
    /**
         * set up output stream
         */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    w.setOutput(ios);
    /**
         * check statement: "Throws: IllegalStateException -
         * if .... prepareWriteSequence has not been called.
         */
    gotCorrectException = false;
    try {
        w.endWriteSequence();
    } catch (IllegalStateException e) {
        gotCorrectException = true;
    } catch (Throwable e) {
        throw new RuntimeException("Test failed.", e);
    }
    if (!gotCorrectException) {
        throw new RuntimeException("Test failed.");
    }
    System.out.println("Test passed.");
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 57 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class OddPaletteTest method doTest.

private static void doTest(BufferedImage src) {
    ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
    if (w == null) {
        throw new RuntimeException("No writer available!");
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        w.setOutput(ios);
        w.write(src);
    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Test failed.", e);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 58 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class CreateMemoryCacheOutputStream method main.

public static void main(String[] args) {
    ImageIO.setUseCache(false);
    OutputStream os = new ByteArrayOutputStream();
    ImageOutputStream stream = null;
    try {
        stream = ImageIO.createImageOutputStream(os);
    } catch (Exception e) {
        throw new RuntimeException("Got exception " + e);
    }
    if (stream == null) {
        throw new RuntimeException("Got null stream!");
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 59 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class DeleteOnExitTest method main.

public static void main(String[] args) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(new byte[100]);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    String tmp = System.getProperty("java.io.tmpdir", ".");
    System.out.println("tmp: " + tmp);
    // count number of files before test
    ImageIO.setUseCache(true);
    ImageIO.setCacheDirectory(new File(tmp));
    File tmpDir = ImageIO.getCacheDirectory();
    System.out.println("tmpDir is " + tmpDir);
    int fnum_before = tmpDir.list().length;
    System.out.println("Files before test: " + fnum_before);
    ImageInputStream iis = ImageIO.createImageInputStream(is);
    System.out.println("iis = " + iis);
    ImageInputStream iis2 = ImageIO.createImageInputStream(is);
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    System.out.println("ios = " + ios);
    ImageOutputStream ios2 = ImageIO.createImageOutputStream(os);
    iis2.close();
    ios2.close();
    int fnum_after = tmpDir.list().length;
    System.out.println("Files after test: " + fnum_after);
    if (fnum_before == fnum_after) {
        throw new RuntimeException("Test failed: cache was not used.");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 60 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class MemoryCacheImageOutputStreamTest method main.

public static void main(String[] args) throws IOException {
    try {
        MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
        // or write anything, for that matter
        stream.write(0);
        stream.flush();
    } catch (Exception e) {
        throw new RuntimeException("Error flushing stream: " + e);
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    byte[] b = new byte[30 * 256];
    byte byteVal = (byte) 0;
    for (int i = 0; i < b.length; i++) {
        b[i] = byteVal++;
    }
    // Write 261,120 bytes
    for (int i = 0; i < 34; i++) {
        ios.write(b);
    }
    // Scatter 256 values at positions 1000, 2000, ...
    // Using both write(int) and write(byte[])
    byte[] buf = new byte[1];
    for (int i = 0; i < 256; i += 2) {
        ios.seek(1000 * i);
        ios.write(i);
        ios.seek(1000 * (i + 1));
        buf[0] = (byte) (i + 1);
        ios.write(buf);
    }
    // Re-read scattered values
    for (int i = 0; i < 256; i++) {
        ios.seek(1000 * i);
        int val = ios.read();
        if (val != i) {
            System.out.println("Got bad value (1) at pos = " + (1000 * i));
        }
    }
    // Discard two buffers and re-read scattered values
    ios.flushBefore(2 * 8192);
    for (int i = 0; i < 256; i++) {
        long pos = 1000 * i;
        if (pos >= 2 * 8192) {
            ios.seek(pos);
            int val = ios.read();
            if (val != i) {
                System.out.println("Got bad value (2) at pos = " + (1000 * i));
            }
        }
    }
    ios.close();
    byte[] data = os.toByteArray();
    for (int i = 0; i < data.length; i++) {
        byte val = data[i];
        if ((i < 256000) && (i % 1000) == 0) {
            if (val != (byte) (i / 1000)) {
                System.out.println("Got bad value (3) at pos = " + i);
            }
        } else {
            byte gval = (byte) ((i % (30 * 256)) % 256);
            if (val != gval) {
                System.out.println("Got bad value (4) at pos = " + i + "(got " + (val & 0xff) + " wanted " + (gval & 0xff) + ")");
            }
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)64 ImageWriter (javax.imageio.ImageWriter)41 BufferedImage (java.awt.image.BufferedImage)37 ByteArrayOutputStream (java.io.ByteArrayOutputStream)36 IIOImage (javax.imageio.IIOImage)33 IOException (java.io.IOException)26 ImageWriteParam (javax.imageio.ImageWriteParam)26 File (java.io.File)24 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)12 Iterator (java.util.Iterator)9 ImageInputStream (javax.imageio.stream.ImageInputStream)9 ImageReader (javax.imageio.ImageReader)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)7 Graphics2D (java.awt.Graphics2D)5 FileOutputStream (java.io.FileOutputStream)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 OutputStream (java.io.OutputStream)4