Path: blob/master/test/jdk/javax/imageio/plugins/png/ReadPNGWithNoTextInTEXTChunk.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 821211626* @summary Test verifies that PNGImageReader doesn't throw IIOException27* when reading a tEXt chunk having no text.28* @run main ReadPNGWithNoTextInTEXTChunk29*/3031import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.awt.Color;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.util.Iterator;38import javax.imageio.ImageTypeSpecifier;39import javax.imageio.ImageWriter;40import javax.imageio.ImageIO;41import javax.imageio.ImageReader;42import javax.imageio.ImageWriteParam;43import javax.imageio.metadata.IIOInvalidTreeException;44import javax.imageio.metadata.IIOMetadata;45import javax.imageio.metadata.IIOMetadataNode;46import javax.imageio.stream.ImageOutputStream;47import javax.imageio.stream.ImageInputStream;48import javax.imageio.IIOImage;4950public class ReadPNGWithNoTextInTEXTChunk {5152private static BufferedImage img;53private static ImageWriter writer;54private static ImageWriteParam param;55private static IIOMetadata metadata;56private static byte[] imageByteArray;5758private static void initialize(int type) {59int width = 1;60int height = 1;61img = new BufferedImage(width, height, type);62Graphics2D g2D = img.createGraphics();63g2D.setColor(new Color(255, 255, 255));64g2D.fillRect(0, 0, width, width);65g2D.dispose();6667Iterator<ImageWriter> iterWriter =68ImageIO.getImageWritersBySuffix("png");69writer = iterWriter.next();7071param = writer.getDefaultWriteParam();72ImageTypeSpecifier specifier =73ImageTypeSpecifier.74createFromBufferedImageType(type);75metadata = writer.getDefaultImageMetadata(specifier, param);76}7778private static void createTEXTNode()79throws IIOInvalidTreeException {80IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");81tEXt_Entry.setAttribute("keyword", "Author");82tEXt_Entry.setAttribute("value", "");8384IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");85tEXt.appendChild(tEXt_Entry);86IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");87root.appendChild(tEXt);88metadata.mergeTree("javax_imageio_png_1.0", root);89}9091private static void writeImage() throws IOException {92ByteArrayOutputStream baos = new ByteArrayOutputStream();93ImageOutputStream ios = ImageIO.createImageOutputStream(baos);94writer.setOutput(ios);95writer.write(metadata, new IIOImage(img, null, metadata), param);96writer.dispose();9798baos.flush();99imageByteArray = baos.toByteArray();100baos.close();101}102103private static void readPNGTEXTChunk() throws IOException {104initialize(BufferedImage.TYPE_BYTE_GRAY);105// Create tEXt node with text length 0106createTEXTNode();107108writeImage();109110ByteArrayInputStream bais = new ByteArrayInputStream( imageByteArray );111ImageInputStream input= ImageIO.createImageInputStream(bais);112Iterator iter = ImageIO.getImageReaders(input);113ImageReader reader = (ImageReader) iter.next();114reader.setInput(input, false, false);115BufferedImage image = reader.read(0, reader.getDefaultReadParam());116input.close();117bais.close();118}119120public static void main(String[] args) throws IOException {121122// read PNG image where tEXt chunk's text length is 0123readPNGTEXTChunk();124}125}126127128