Skip to content

Commit d2df36b

Browse files
committed
8299333: Unify exceptions used by all variants of ICC_Profile.getInstance(null)
Reviewed-by: prr
1 parent 1d7bb1f commit d2df36b

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

src/java.desktop/share/classes/java/awt/color/ICC_Profile.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,6 +50,7 @@
5050
import java.io.Serializable;
5151
import java.security.AccessController;
5252
import java.security.PrivilegedAction;
53+
import java.util.Objects;
5354
import java.util.StringTokenizer;
5455

5556
import sun.awt.AWTAccessor;
@@ -850,10 +851,7 @@ public static ICC_Profile getInstance(int cspace) {
850851
* {@code java.class.path} property; finally, in a directory used to store
851852
* profiles always available, such as the profile for sRGB. Built-in
852853
* profiles use {@code .pf} as the file name extension for profiles, e.g.
853-
* {@code sRGB.pf}. This method throws an {@code IOException} if the
854-
* specified file cannot be opened or if an I/O error occurs while reading
855-
* the file. It throws an {@code IllegalArgumentException} if the file does
856-
* not contain valid ICC Profile data.
854+
* {@code sRGB.pf}.
857855
*
858856
* @param fileName the file that contains the data for the profile
859857
* @return an {@code ICC_Profile} object corresponding to the data in the
@@ -864,6 +862,7 @@ public static ICC_Profile getInstance(int cspace) {
864862
* Profile data
865863
* @throws SecurityException If a security manager is installed and it does
866864
* not permit read access to the given file
865+
* @throws NullPointerException if {@code fileName} is {@code null}
867866
*/
868867
public static ICC_Profile getInstance(String fileName) throws IOException {
869868
InputStream is;
@@ -883,19 +882,18 @@ public static ICC_Profile getInstance(String fileName) throws IOException {
883882

884883
/**
885884
* Constructs an {@code ICC_Profile} corresponding to the data in an
886-
* {@code InputStream}. This method throws an
887-
* {@code IllegalArgumentException} if the stream does not contain valid ICC
888-
* Profile data. It throws an {@code IOException} if an I/O error occurs
889-
* while reading the stream.
885+
* {@code InputStream}.
890886
*
891887
* @param s the input stream from which to read the profile data
892888
* @return an {@code ICC_Profile} object corresponding to the data in the
893889
* specified {@code InputStream}
894890
* @throws IOException If an I/O error occurs while reading the stream
895891
* @throws IllegalArgumentException If the stream does not contain valid ICC
896892
* Profile data
893+
* @throws NullPointerException if {@code s} is {@code null}
897894
*/
898895
public static ICC_Profile getInstance(InputStream s) throws IOException {
896+
Objects.requireNonNull(s);
899897
return getInstance(getProfileDataFromStream(s));
900898
}
901899

@@ -1046,6 +1044,7 @@ public int getPCSType() {
10461044
* @param fileName the file to write the profile data to
10471045
* @throws IOException If the file cannot be opened for writing or an I/O
10481046
* error occurs while writing to the file
1047+
* @throws NullPointerException if {@code fileName} is {@code null}
10491048
*/
10501049
public void write(String fileName) throws IOException {
10511050
try (OutputStream out = new FileOutputStream(fileName)) {
@@ -1058,6 +1057,7 @@ public void write(String fileName) throws IOException {
10581057
*
10591058
* @param s the stream to write the profile data to
10601059
* @throws IOException If an I/O error occurs while writing to the stream
1060+
* @throws NullPointerException if {@code s} is {@code null}
10611061
*/
10621062
public void write(OutputStream s) throws IOException {
10631063
s.write(getData());
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright Amazon.com Inc. 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+
import java.awt.color.ColorSpace;
25+
import java.awt.color.ICC_Profile;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
29+
/**
30+
* @test
31+
* @bug 6211108 6211105 8299333
32+
*/
33+
public final class ExpectedNPEOnNull {
34+
35+
public static void main(String[] args) throws Exception {
36+
// static methods
37+
try {
38+
ICC_Profile.getInstance((String) null);
39+
throw new RuntimeException("NPE is expected");
40+
} catch (NullPointerException ignored) {
41+
// expected
42+
}
43+
try {
44+
ICC_Profile.getInstance((InputStream) null);
45+
throw new RuntimeException("NPE is expected");
46+
} catch (NullPointerException ignored) {
47+
// expected
48+
}
49+
// instance methods
50+
test(ICC_Profile.getInstance(ColorSpace.CS_sRGB));
51+
test(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB));
52+
test(ICC_Profile.getInstance(ColorSpace.CS_CIEXYZ));
53+
test(ICC_Profile.getInstance(ColorSpace.CS_PYCC));
54+
test(ICC_Profile.getInstance(ColorSpace.CS_GRAY));
55+
}
56+
57+
private static void test(ICC_Profile profile) throws Exception {
58+
try {
59+
profile.write((String) null);
60+
throw new RuntimeException("NPE is expected");
61+
} catch (NullPointerException ignored) {
62+
// expected
63+
}
64+
try {
65+
profile.write((OutputStream) null);
66+
throw new RuntimeException("NPE is expected");
67+
} catch (NullPointerException ignored) {
68+
// expected
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)