Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/png/ReadPNGWithNoTextInTEXTChunk.java
41155 views
1
/*
2
* Copyright (c) 2018, 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 8212116
27
* @summary Test verifies that PNGImageReader doesn't throw IIOException
28
* when reading a tEXt chunk having no text.
29
* @run main ReadPNGWithNoTextInTEXTChunk
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.util.Iterator;
39
import javax.imageio.ImageTypeSpecifier;
40
import javax.imageio.ImageWriter;
41
import javax.imageio.ImageIO;
42
import javax.imageio.ImageReader;
43
import javax.imageio.ImageWriteParam;
44
import javax.imageio.metadata.IIOInvalidTreeException;
45
import javax.imageio.metadata.IIOMetadata;
46
import javax.imageio.metadata.IIOMetadataNode;
47
import javax.imageio.stream.ImageOutputStream;
48
import javax.imageio.stream.ImageInputStream;
49
import javax.imageio.IIOImage;
50
51
public class ReadPNGWithNoTextInTEXTChunk {
52
53
private static BufferedImage img;
54
private static ImageWriter writer;
55
private static ImageWriteParam param;
56
private static IIOMetadata metadata;
57
private static byte[] imageByteArray;
58
59
private static void initialize(int type) {
60
int width = 1;
61
int height = 1;
62
img = new BufferedImage(width, height, type);
63
Graphics2D g2D = img.createGraphics();
64
g2D.setColor(new Color(255, 255, 255));
65
g2D.fillRect(0, 0, width, width);
66
g2D.dispose();
67
68
Iterator<ImageWriter> iterWriter =
69
ImageIO.getImageWritersBySuffix("png");
70
writer = iterWriter.next();
71
72
param = writer.getDefaultWriteParam();
73
ImageTypeSpecifier specifier =
74
ImageTypeSpecifier.
75
createFromBufferedImageType(type);
76
metadata = writer.getDefaultImageMetadata(specifier, param);
77
}
78
79
private static void createTEXTNode()
80
throws IIOInvalidTreeException {
81
IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");
82
tEXt_Entry.setAttribute("keyword", "Author");
83
tEXt_Entry.setAttribute("value", "");
84
85
IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");
86
tEXt.appendChild(tEXt_Entry);
87
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
88
root.appendChild(tEXt);
89
metadata.mergeTree("javax_imageio_png_1.0", root);
90
}
91
92
private static void writeImage() throws IOException {
93
ByteArrayOutputStream baos = new ByteArrayOutputStream();
94
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
95
writer.setOutput(ios);
96
writer.write(metadata, new IIOImage(img, null, metadata), param);
97
writer.dispose();
98
99
baos.flush();
100
imageByteArray = baos.toByteArray();
101
baos.close();
102
}
103
104
private static void readPNGTEXTChunk() throws IOException {
105
initialize(BufferedImage.TYPE_BYTE_GRAY);
106
// Create tEXt node with text length 0
107
createTEXTNode();
108
109
writeImage();
110
111
ByteArrayInputStream bais = new ByteArrayInputStream( imageByteArray );
112
ImageInputStream input= ImageIO.createImageInputStream(bais);
113
Iterator iter = ImageIO.getImageReaders(input);
114
ImageReader reader = (ImageReader) iter.next();
115
reader.setInput(input, false, false);
116
BufferedImage image = reader.read(0, reader.getDefaultReadParam());
117
input.close();
118
bais.close();
119
}
120
121
public static void main(String[] args) throws IOException {
122
123
// read PNG image where tEXt chunk's text length is 0
124
readPNGTEXTChunk();
125
}
126
}
127
128