|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 5043343 |
| 27 | + * @summary Verify that FIIS and FIOS constructors set streamPos to RAF position |
| 28 | + * @library /test/lib |
| 29 | + * @build jdk.test.lib.RandomFactory |
| 30 | + * @run main FileImageStreamPos |
| 31 | + * @key randomness |
| 32 | + */ |
| 33 | + |
| 34 | +import java.awt.image.BufferedImage; |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.RandomAccessFile; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Random; |
| 40 | +import javax.imageio.ImageIO; |
| 41 | +import javax.imageio.stream.FileImageInputStream; |
| 42 | +import javax.imageio.stream.FileImageOutputStream; |
| 43 | +import jdk.test.lib.RandomFactory; |
| 44 | + |
| 45 | +public class FileImageStreamPos { |
| 46 | + // Maximum size of random prefix |
| 47 | + private static final int BOUND = 1024; |
| 48 | + |
| 49 | + // Maximum image dimensions |
| 50 | + public static final int SIZE_MIN = 512; |
| 51 | + public static final int SIZE_MAX = 1024; |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException { |
| 54 | + |
| 55 | + // Create source image with random content |
| 56 | + Random random = RandomFactory.getRandom(); |
| 57 | + final int w = random.nextInt(SIZE_MIN, SIZE_MAX); |
| 58 | + final int h = random.nextInt(SIZE_MIN, SIZE_MAX); |
| 59 | + BufferedImage srcImage = |
| 60 | + new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); |
| 61 | + int[] rgbArray = random.ints(w*h).toArray(); |
| 62 | + srcImage.setRGB(0, 0, w, h, rgbArray, 0, w); |
| 63 | + |
| 64 | + // Write some random bytes followed by the image |
| 65 | + File dstFile = File.createTempFile("before", "after", new File(".")); |
| 66 | + dstFile.deleteOnExit(); |
| 67 | + final int offset = random.nextInt(BOUND); |
| 68 | + System.out.println("offset: " + offset); |
| 69 | + try(RandomAccessFile raf = new RandomAccessFile(dstFile, "rw");) { |
| 70 | + byte[] b = new byte[offset]; |
| 71 | + random.nextBytes(b); |
| 72 | + raf.write(b); |
| 73 | + FileImageOutputStream fios = new FileImageOutputStream(raf); |
| 74 | + if (fios.getStreamPosition() != offset) |
| 75 | + throw new RuntimeException(fios.getStreamPosition() + " != " + |
| 76 | + offset); |
| 77 | + ImageIO.write(srcImage, "PNG", fios); |
| 78 | + } |
| 79 | + |
| 80 | + // Read the image just written and compare with the original |
| 81 | + try(RandomAccessFile raf = new RandomAccessFile(dstFile, "r")) { |
| 82 | + // Read the image from after then random prefix |
| 83 | + raf.skipBytes(offset); |
| 84 | + FileImageInputStream fiis = new FileImageInputStream(raf); |
| 85 | + if (fiis.getStreamPosition() != offset) |
| 86 | + throw new RuntimeException(fiis.getStreamPosition() + " != " + |
| 87 | + offset); |
| 88 | + BufferedImage dstImage = ImageIO.read(fiis); |
| 89 | + |
| 90 | + // Compare the image dimensions |
| 91 | + if (dstImage.getWidth() != w) |
| 92 | + throw new RuntimeException(dstImage.getWidth() + " != " + w); |
| 93 | + if (dstImage.getHeight() != h) |
| 94 | + throw new RuntimeException(dstImage.getHeight() + " != " + h); |
| 95 | + |
| 96 | + // Compare RGB pixels |
| 97 | + if (!Arrays.equals(srcImage.getRGB(0, 0, w, h, null, 0, w), |
| 98 | + dstImage.getRGB(0, 0, w, h, null, 0, w))) |
| 99 | + throw new RuntimeException("Pixels are not equal"); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments