Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/stream/NullStreamCheckTest.java
41152 views
1
/*
2
* Copyright (c) 2016, 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 8044289
27
* @summary Test verifies that when some of the read() and write() methods
28
* are not able to get stream from createImageInputStream() and
29
* createImageOutputStream() are we doing null check for stream
30
* and throwing IOException as per specification.
31
* @run main NullStreamCheckTest
32
*/
33
34
import java.awt.image.BufferedImage;
35
import java.io.File;
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39
import java.net.MalformedURLException;
40
import java.net.URL;
41
import javax.imageio.ImageIO;
42
import javax.imageio.spi.IIORegistry;
43
import javax.imageio.spi.ImageInputStreamSpi;
44
import javax.imageio.spi.ImageOutputStreamSpi;
45
46
public class NullStreamCheckTest {
47
48
// get ImageIORegistry default instance.
49
private static final IIORegistry localRegistry = IIORegistry.
50
getDefaultInstance();
51
// stream variables needed for input and output.
52
static LocalOutputStream outputStream = new LocalOutputStream();
53
static LocalInputStream inputStream = new LocalInputStream();
54
55
static final int width = 50, height = 50;
56
57
// input and output BufferedImage needed while read and write.
58
static BufferedImage inputImage = new BufferedImage(width, height,
59
BufferedImage.TYPE_INT_ARGB);
60
61
/* if we catch expected IOException message return
62
* false otherwise return true.
63
*/
64
private static boolean verifyOutputExceptionMessage(IOException ex) {
65
String message = ex.getMessage();
66
return (!message.equals("Can't create an ImageOutputStream!"));
67
}
68
69
/* if we catch expected IOException message return
70
* false otherwise return true.
71
*/
72
private static boolean verifyInputExceptionMessage(IOException ex) {
73
String message = ex.getMessage();
74
return (!message.equals("Can't create an ImageInputStream!"));
75
}
76
77
private static void verifyFileWrite() throws IOException {
78
File outputTestFile = File.createTempFile("outputTestFile", ".png");
79
try {
80
ImageIO.write(inputImage, "png", outputTestFile);
81
} catch (IOException ex) {
82
if (verifyOutputExceptionMessage(ex))
83
throw ex;
84
} finally {
85
outputTestFile.delete();
86
}
87
}
88
89
private static void verifyStreamWrite() throws IOException {
90
try {
91
ImageIO.write(inputImage, "png", outputStream);
92
} catch (IOException ex) {
93
if (verifyOutputExceptionMessage(ex))
94
throw ex;
95
} finally {
96
try {
97
outputStream.close();
98
} catch (IOException ex) {
99
throw ex;
100
}
101
}
102
}
103
104
private static void verifyFileRead() throws IOException {
105
File inputTestFile = File.createTempFile("inputTestFile", ".png");
106
try {
107
ImageIO.read(inputTestFile);
108
} catch (IOException ex) {
109
if (verifyInputExceptionMessage(ex))
110
throw ex;
111
} finally {
112
inputTestFile.delete();
113
}
114
}
115
116
private static void verifyStreamRead() throws IOException {
117
try {
118
ImageIO.read(inputStream);
119
} catch (IOException ex) {
120
if (verifyInputExceptionMessage(ex))
121
throw ex;
122
} finally {
123
try {
124
inputStream.close();
125
} catch (IOException ex) {
126
throw ex;
127
}
128
}
129
}
130
131
private static void verifyUrlRead() throws IOException {
132
URL url;
133
File inputTestUrlFile = File.createTempFile("inputTestFile", ".png");
134
try {
135
try {
136
url = inputTestUrlFile.toURI().toURL();
137
} catch (MalformedURLException ex) {
138
throw ex;
139
}
140
141
try {
142
ImageIO.read(url);
143
} catch (IOException ex) {
144
if (verifyInputExceptionMessage(ex))
145
throw ex;
146
}
147
} finally {
148
inputTestUrlFile.delete();
149
}
150
}
151
152
public static void main(String[] args) throws IOException,
153
MalformedURLException {
154
155
/* deregister ImageOutputStreamSpi so that we creatImageOutputStream
156
* returns null while writing.
157
*/
158
localRegistry.deregisterAll(ImageOutputStreamSpi.class);
159
/* verify possible ImageIO.write() scenario's for null stream output
160
* from createImageOutputStream() API in ImageIO class.
161
*/
162
verifyFileWrite();
163
verifyStreamWrite();
164
165
/* deregister ImageInputStreamSpi so that we creatImageInputStream
166
* returns null while reading.
167
*/
168
localRegistry.deregisterAll(ImageInputStreamSpi.class);
169
/* verify possible ImageIO.read() scenario's for null stream output
170
* from createImageInputStream API in ImageIO class.
171
*/
172
verifyFileRead();
173
verifyStreamRead();
174
verifyUrlRead();
175
}
176
177
static class LocalOutputStream extends OutputStream {
178
179
@Override
180
public void write(int i) throws IOException {
181
}
182
}
183
184
static class LocalInputStream extends InputStream {
185
186
@Override
187
public int read() throws IOException {
188
return 0;
189
}
190
}
191
}
192
193