From d2df36b073943fc85b169f09e32747ebe2ad0bfb Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Thu, 30 Mar 2023 16:12:25 +0000 Subject: [PATCH] 8299333: Unify exceptions used by all variants of ICC_Profile.getInstance(null) Reviewed-by: prr --- .../classes/java/awt/color/ICC_Profile.java | 18 ++--- .../color/ICC_Profile/ExpectedNPEOnNull.java | 71 +++++++++++++++++++ 2 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 test/jdk/java/awt/color/ICC_Profile/ExpectedNPEOnNull.java diff --git a/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java b/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java index 90537ec6fe6..0d31d25b119 100644 --- a/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java +++ b/src/java.desktop/share/classes/java/awt/color/ICC_Profile.java @@ -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()); diff --git a/test/jdk/java/awt/color/ICC_Profile/ExpectedNPEOnNull.java b/test/jdk/java/awt/color/ICC_Profile/ExpectedNPEOnNull.java new file mode 100644 index 00000000000..40188003bff --- /dev/null +++ b/test/jdk/java/awt/color/ICC_Profile/ExpectedNPEOnNull.java @@ -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 + } + } +}