Search in sources :

Example 16 with ImageInputStream

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

the class ReadUnsignedIntTest method main.

public static void main(String[] args) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeInt(1);
    dos.writeInt(0x7fffffff);
    dos.writeInt(0x8fffffff);
    dos.writeInt(0xffffffff);
    dos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    for (int i = 0; i < 4; i++) {
        long res = iis.readUnsignedInt();
        if (res <= 0) {
            throw new RuntimeException("Negative number was read: " + Long.toString(res, 16));
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with ImageInputStream

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

the class Main method launch.

private static void launch(URL[] urls, final String className, final String methodName) {
    final String uniqClassName = "testapp/Uniq" + counter;
    final boolean saveStrongRef = forgetSomeStreams ? (counter % 5 == 4) : false;
    System.out.printf("%s: launch the app\n", uniqClassName);
    Runnable launchIt = new Runnable() {

        public void run() {
            AppContext ctx = SunToolkit.createNewAppContext();
            try {
                Class appMain = ctx.getContextClassLoader().loadClass(className);
                Method launch = appMain.getDeclaredMethod(methodName, strongRefs.getClass());
                Constructor c = appMain.getConstructor(String.class, problems.getClass());
                Object o = c.newInstance(uniqClassName, problems);
                if (saveStrongRef) {
                    System.out.printf("%s: force strong ref\n", uniqClassName);
                    launch.invoke(o, strongRefs);
                } else {
                    HashMap<String, ImageInputStream> empty = null;
                    launch.invoke(o, empty);
                }
                ctx = null;
            } catch (Throwable e) {
                problems.add(e);
            } finally {
                doneSignal.countDown();
            }
        }
    };
    MyClassLoader appClassLoader = new MyClassLoader(urls, uniqClassName);
    refs.put(appClassLoader, uniqClassName);
    Thread appThread = new Thread(appsThreadGroup, launchIt, "AppThread" + counter++);
    appThread.setContextClassLoader(appClassLoader);
    appThread.start();
    launchIt = null;
    appThread = null;
    appClassLoader = null;
}
Also used : Constructor(java.lang.reflect.Constructor) AppContext(sun.awt.AppContext) ImageInputStream(javax.imageio.stream.ImageInputStream) Method(java.lang.reflect.Method)

Example 18 with ImageInputStream

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

the class ReadBytesIIOByteBuffer method main.

public static void main(String[] argv) {
    byte[] bar = { 1, 1, 1 };
    InputStream is = new ByteArrayInputStream(bar);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    byte[] b = new byte[10];
    IIOByteBuffer iiob = new IIOByteBuffer(b, 0, b.length);
    try {
        iis.readBytes(iiob, -1);
    } catch (IndexOutOfBoundsException e) {
        return;
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception: " + e);
    }
    throw new RuntimeException("No exception thrown for len < 0!");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IIOByteBuffer(javax.imageio.stream.IIOByteBuffer)

Example 19 with ImageInputStream

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

the class ReadFullyTest method main.

public static void main(String[] args) {
    try {
        byte[] b = { // low low
        (byte) 0x11, // low low
        (byte) 0x22, // low high
        (byte) 0x44, // low high
        (byte) 0x99, // high low
        (byte) 0xAA, // high low
        (byte) 0x33, // high high
        (byte) 0xBB, // high high
        (byte) 0xCC };
        InputStream in = new ByteArrayInputStream(b);
        ImageInputStream iin = new MemoryCacheImageInputStream(in);
        short[] s = new short[b.length / 2];
        char[] c = new char[b.length / 2];
        int[] i = new int[b.length / 4];
        long[] l = new long[b.length / 8];
        float[] f = new float[b.length / 4];
        double[] d = new double[b.length / 8];
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(s, 0, s.length);
        expect(s[0] & 0xffff, 0x1122);
        expect(s[1] & 0xffff, 0x4499);
        expect(s[2] & 0xffff, 0xAA33);
        expect(s[3] & 0xffff, 0xBBCC);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(s, 0, s.length);
        expect(s[0] & 0xffff, 0x2211);
        expect(s[1] & 0xffff, 0x9944);
        expect(s[2] & 0xffff, 0x33AA);
        expect(s[3] & 0xffff, 0xCCBB);
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(c, 0, c.length);
        expect(c[0], 0x1122);
        expect(c[1], 0x4499);
        expect(c[2], 0xAA33);
        expect(c[3], 0xBBCC);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(c, 0, c.length);
        expect(c[0], 0x2211);
        expect(c[1], 0x9944);
        expect(c[2], 0x33AA);
        expect(c[3], 0xCCBB);
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(i, 0, i.length);
        expect(i[0] & 0xffffffff, 0x11224499);
        expect(i[1] & 0xffffffff, 0xAA33BBCC);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(i, 0, i.length);
        expect(i[0] & 0xffffffff, 0x99442211);
        expect(i[1] & 0xffffffff, 0xCCBB33AA);
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(f, 0, f.length);
        expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x11224499);
        expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xAA33BBCC);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(f, 0, f.length);
        expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x99442211);
        expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xCCBB33AA);
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(l, 0, l.length);
        expect(l[0], 0x11224499AA33BBCCL);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(l, 0, l.length);
        expect(l[0], 0xCCBB33AA99442211L);
        iin.seek(0L);
        iin.setByteOrder(bigEndian);
        iin.readFully(d, 0, d.length);
        expect(Double.doubleToLongBits(d[0]), 0x11224499AA33BBCCL);
        iin.seek(0L);
        iin.setByteOrder(littleEndian);
        iin.readFully(d, 0, d.length);
        expect(Double.doubleToLongBits(d[0]), 0xCCBB33AA99442211L);
    } catch (Exception ex) {
        throw new RuntimeException("Got exception " + ex);
    }
}
Also used : MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 20 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project minecolonies by Minecolonies.

the class Image method getImageDimensions.

/**
     * Load and image from a {@link ResourceLocation} and return a {@link Tuple} containing its width and height.
     *
     * @param resourceLocation The {@link ResourceLocation} pointing to the image.
     * @return Width and height.
     */
public static Tuple<Integer, Integer> getImageDimensions(final ResourceLocation resourceLocation) {
    int width = 0;
    int height = 0;
    final Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix("png");
    if (it.hasNext()) {
        final ImageReader reader = it.next();
        try (ImageInputStream stream = ImageIO.createImageInputStream(Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation).getInputStream())) {
            reader.setInput(stream);
            width = reader.getWidth(reader.getMinIndex());
            height = reader.getHeight(reader.getMinIndex());
        } catch (final IOException e) {
            getLogger().warn(e);
        } finally {
            reader.dispose();
        }
    }
    return new Tuple<>(width, height);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) Tuple(net.minecraft.util.Tuple)

Aggregations

ImageInputStream (javax.imageio.stream.ImageInputStream)60 ImageReader (javax.imageio.ImageReader)32 BufferedImage (java.awt.image.BufferedImage)23 IOException (java.io.IOException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageOutputStream (javax.imageio.stream.ImageOutputStream)9 File (java.io.File)8 InputStream (java.io.InputStream)7 IIOImage (javax.imageio.IIOImage)7 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)7 ImageReadParam (javax.imageio.ImageReadParam)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageWriter (javax.imageio.ImageWriter)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 Iterator (java.util.Iterator)4 IIOException (javax.imageio.IIOException)4 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2