1 /* 2 * Copyright (c) 2020, 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 8243674 27 * @summary Test verifies that PNGImageReader is able to read iTXt chunk 28 * with language tag having length more than 80 29 * @run main ReadLongLanguageTagTest 30 */ 31 32 import java.awt.Graphics2D; 33 import java.awt.image.BufferedImage; 34 import java.awt.Color; 35 import java.io.ByteArrayInputStream; 36 import java.io.ByteArrayOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.util.Iterator; 40 import javax.imageio.*; 41 import javax.imageio.metadata.IIOInvalidTreeException; 42 import javax.imageio.metadata.IIOMetadata; 43 import javax.imageio.metadata.IIOMetadataNode; 44 import javax.imageio.stream.ImageInputStream; 45 import javax.imageio.stream.ImageOutputStream; 46 47 public class ReadLongLanguageTagTest { 48 49 private static BufferedImage img; 50 private static ImageWriter writer; 51 private static ImageWriteParam param; 52 private static IIOMetadata metadata; 53 private static byte[] imageByteArray; 54 55 private static void initialize(int type) { 56 int width = 1; 57 int height = 1; 58 img = new BufferedImage(width, height, type); 59 Graphics2D g2D = img.createGraphics(); 60 g2D.setColor(new Color(255, 255, 255)); 61 g2D.fillRect(0, 0, width, width); 62 g2D.dispose(); 63 64 Iterator<ImageWriter> iterWriter = 65 ImageIO.getImageWritersBySuffix("png"); 66 writer = iterWriter.next(); 67 68 param = writer.getDefaultWriteParam(); 69 ImageTypeSpecifier specifier = 70 ImageTypeSpecifier. 71 createFromBufferedImageType(type); 72 metadata = writer.getDefaultImageMetadata(specifier, param); 73 } 74 75 private static void createITXTNode() 76 throws IIOInvalidTreeException { 77 IIOMetadataNode iTXt_Entry = new IIOMetadataNode("iTXtEntry"); 78 iTXt_Entry.setAttribute("keyword", "ImageIO"); 79 iTXt_Entry.setAttribute("compressionFlag", "FALSE"); 80 iTXt_Entry.setAttribute("compressionMethod", "0"); 81 iTXt_Entry.setAttribute("languageTag", "en-Java" + 82 "JavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJava" + 83 "JavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJavaJava"); 84 iTXt_Entry.setAttribute("translatedKeyword", ""); 85 iTXt_Entry.setAttribute("text", ""); 86 87 IIOMetadataNode iTXt = new IIOMetadataNode("iTXt"); 88 iTXt.appendChild(iTXt_Entry); 89 IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0"); 90 root.appendChild(iTXt); 91 metadata.mergeTree("javax_imageio_png_1.0", root); 92 } 93 94 private static void writeImage() throws IOException { 95 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 96 ImageOutputStream ios = ImageIO.createImageOutputStream(baos); 97 writer.setOutput(ios); 98 writer.write(metadata, new IIOImage(img, null, metadata), param); 99 writer.dispose(); 100 101 baos.flush(); 102 imageByteArray = baos.toByteArray(); 103 baos.close(); 104 } 105 106 private static void readITXTChunk() throws IOException { 107 initialize(BufferedImage.TYPE_BYTE_GRAY); 108 // Create iTXt node and merge it with default metadata 109 createITXTNode(); 110 111 writeImage(); 112 113 InputStream input= new ByteArrayInputStream(imageByteArray); 114 try { 115 Iterator<ImageReader> iterReader = 116 ImageIO.getImageReadersBySuffix("PNG"); 117 if (iterReader.hasNext()) { 118 ImageReader pngImageReader = iterReader.next(); 119 ImageReadParam param = pngImageReader.getDefaultReadParam(); 120 ImageInputStream imageStream = 121 ImageIO.createImageInputStream(input); 122 pngImageReader.setInput(imageStream, false, false); 123 pngImageReader.read(0, param); 124 } else { 125 throw new RuntimeException("Requested PNGImageReader" + 126 " not available"); 127 } 128 } finally { 129 input.close(); 130 } 131 } 132 133 public static void main(String[] args) throws IOException { 134 // read PNG image having long language tag in iTXt chunk 135 readITXTChunk(); 136 } 137 }