8299333: Unify exceptions used by all variants of ICC_Profile.getInstance(null)

Reviewed-by: prr
This commit is contained in:
Sergey Bylokhov 2023-03-30 16:12:25 +00:00
parent 1d7bb1ffa0
commit d2df36b073
2 changed files with 80 additions and 9 deletions
src/java.desktop/share/classes/java/awt/color
test/jdk/java/awt/color/ICC_Profile

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -50,6 +50,7 @@ import java.io.Serial;
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Objects;
import java.util.StringTokenizer;
import sun.awt.AWTAccessor;
@ -850,10 +851,7 @@ public sealed class ICC_Profile implements Serializable
* {@code java.class.path} property; finally, in a directory used to store
* profiles always available, such as the profile for sRGB. Built-in
* profiles use {@code .pf} as the file name extension for profiles, e.g.
* {@code sRGB.pf}. This method throws an {@code IOException} if the
* specified file cannot be opened or if an I/O error occurs while reading
* the file. It throws an {@code IllegalArgumentException} if the file does
* not contain valid ICC Profile data.
* {@code sRGB.pf}.
*
* @param fileName the file that contains the data for the profile
* @return an {@code ICC_Profile} object corresponding to the data in the
@ -864,6 +862,7 @@ public sealed class ICC_Profile implements Serializable
* Profile data
* @throws SecurityException If a security manager is installed and it does
* not permit read access to the given file
* @throws NullPointerException if {@code fileName} is {@code null}
*/
public static ICC_Profile getInstance(String fileName) throws IOException {
InputStream is;
@ -883,10 +882,7 @@ public sealed class ICC_Profile implements Serializable
/**
* Constructs an {@code ICC_Profile} corresponding to the data in an
* {@code InputStream}. This method throws an
* {@code IllegalArgumentException} if the stream does not contain valid ICC
* Profile data. It throws an {@code IOException} if an I/O error occurs
* while reading the stream.
* {@code InputStream}.
*
* @param s the input stream from which to read the profile data
* @return an {@code ICC_Profile} object corresponding to the data in the
@ -894,8 +890,10 @@ public sealed class ICC_Profile implements Serializable
* @throws IOException If an I/O error occurs while reading the stream
* @throws IllegalArgumentException If the stream does not contain valid ICC
* Profile data
* @throws NullPointerException if {@code s} is {@code null}
*/
public static ICC_Profile getInstance(InputStream s) throws IOException {
Objects.requireNonNull(s);
return getInstance(getProfileDataFromStream(s));
}
@ -1046,6 +1044,7 @@ public sealed class ICC_Profile implements Serializable
* @param fileName the file to write the profile data to
* @throws IOException If the file cannot be opened for writing or an I/O
* error occurs while writing to the file
* @throws NullPointerException if {@code fileName} is {@code null}
*/
public void write(String fileName) throws IOException {
try (OutputStream out = new FileOutputStream(fileName)) {
@ -1058,6 +1057,7 @@ public sealed class ICC_Profile implements Serializable
*
* @param s the stream to write the profile data to
* @throws IOException If an I/O error occurs while writing to the stream
* @throws NullPointerException if {@code s} is {@code null}
*/
public void write(OutputStream s) throws IOException {
s.write(getData());

@ -0,0 +1,71 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @test
* @bug 6211108 6211105 8299333
*/
public final class ExpectedNPEOnNull {
public static void main(String[] args) throws Exception {
// static methods
try {
ICC_Profile.getInstance((String) null);
throw new RuntimeException("NPE is expected");
} catch (NullPointerException ignored) {
// expected
}
try {
ICC_Profile.getInstance((InputStream) null);
throw new RuntimeException("NPE is expected");
} catch (NullPointerException ignored) {
// expected
}
// instance methods
test(ICC_Profile.getInstance(ColorSpace.CS_sRGB));
test(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB));
test(ICC_Profile.getInstance(ColorSpace.CS_CIEXYZ));
test(ICC_Profile.getInstance(ColorSpace.CS_PYCC));
test(ICC_Profile.getInstance(ColorSpace.CS_GRAY));
}
private static void test(ICC_Profile profile) throws Exception {
try {
profile.write((String) null);
throw new RuntimeException("NPE is expected");
} catch (NullPointerException ignored) {
// expected
}
try {
profile.write((OutputStream) null);
throw new RuntimeException("NPE is expected");
} catch (NullPointerException ignored) {
// expected
}
}
}