Skip to content

Commit f98e519

Browse files
author
duke
committed
Backport 0e98ec36623d5d83172209058574a97bab1d6038
1 parent 9df689f commit f98e519

25 files changed

+191
-32
lines changed

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.FileOutputStream;
4242
import java.io.IOException;
4343
import java.io.InputStream;
44+
import java.io.InvalidObjectException;
4445
import java.io.ObjectInputStream;
4546
import java.io.ObjectOutputStream;
4647
import java.io.ObjectStreamException;
@@ -1567,33 +1568,19 @@ private void writeObject(ObjectOutputStream s) throws IOException {
15671568
private void readObject(ObjectInputStream s)
15681569
throws IOException, ClassNotFoundException {
15691570
s.defaultReadObject();
1570-
1571-
String csName = (String) s.readObject();
1572-
byte[] data = (byte[]) s.readObject();
1573-
1574-
int cspace = 0; // ColorSpace.CS_* constant if known
1575-
boolean isKnownPredefinedCS = false;
1576-
if (csName != null) {
1577-
isKnownPredefinedCS = true;
1578-
if (csName.equals("CS_sRGB")) {
1579-
cspace = ColorSpace.CS_sRGB;
1580-
} else if (csName.equals("CS_CIEXYZ")) {
1581-
cspace = ColorSpace.CS_CIEXYZ;
1582-
} else if (csName.equals("CS_PYCC")) {
1583-
cspace = ColorSpace.CS_PYCC;
1584-
} else if (csName.equals("CS_GRAY")) {
1585-
cspace = ColorSpace.CS_GRAY;
1586-
} else if (csName.equals("CS_LINEAR_RGB")) {
1587-
cspace = ColorSpace.CS_LINEAR_RGB;
1588-
} else {
1589-
isKnownPredefinedCS = false;
1590-
}
1591-
}
1592-
1593-
if (isKnownPredefinedCS) {
1594-
resolvedDeserializedProfile = getInstance(cspace);
1595-
} else {
1596-
resolvedDeserializedProfile = getInstance(data);
1571+
try {
1572+
String csName = (String) s.readObject();
1573+
byte[] data = (byte[]) s.readObject();
1574+
resolvedDeserializedProfile = switch (csName) {
1575+
case "CS_sRGB" -> getInstance(ColorSpace.CS_sRGB);
1576+
case "CS_CIEXYZ" -> getInstance(ColorSpace.CS_CIEXYZ);
1577+
case "CS_PYCC" -> getInstance(ColorSpace.CS_PYCC);
1578+
case "CS_GRAY" -> getInstance(ColorSpace.CS_GRAY);
1579+
case "CS_LINEAR_RGB" -> getInstance(ColorSpace.CS_LINEAR_RGB);
1580+
case null, default -> getInstance(data);
1581+
};
1582+
} catch (ClassCastException | IllegalArgumentException e) {
1583+
throw new InvalidObjectException("Invalid ICC Profile Data", e);
15971584
}
15981585
}
15991586

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.io.File;
25+
import java.io.FileInputStream;
26+
import java.io.InvalidObjectException;
27+
import java.io.ObjectInputStream;
28+
import java.io.OptionalDataException;
29+
30+
/**
31+
* @test
32+
* @bug 8367384
33+
* @summary Verify ICC_Profile serialization per spec, all name/data cases
34+
*/
35+
public final class SerializationSpecTest {
36+
37+
public static void main(String[] args) throws Exception {
38+
// Serialization form for ICC_Profile includes version, profile name,
39+
// and profile data. If the name is invalid or does not match a standard
40+
// profile, the data is used. An exception is thrown only if both the
41+
// name and the data are invalid, or if one of them is missing or is of
42+
// the wrong type.
43+
44+
// Naming conventions used in test file names:
45+
// null : null reference
46+
// valid : valid standard profile name or valid profile data (byte[])
47+
// invalid : unrecognized name or data with incorrect ICC header
48+
// wrongType: incorrect type, e.g., int[] instead of String or byte[]
49+
50+
// No name or data
51+
test("empty", OptionalDataException.class);
52+
53+
// Cases where only the profile name is present (no profile data)
54+
test("null", OptionalDataException.class);
55+
test("valid", OptionalDataException.class);
56+
test("invalid", OptionalDataException.class);
57+
test("wrongType", InvalidObjectException.class);
58+
59+
// The test files are named as <name>_<data>.ser
60+
test("null_null", InvalidObjectException.class);
61+
test("null_valid", null); // valid data is enough if name is null
62+
test("null_invalid", InvalidObjectException.class);
63+
test("null_wrongType", InvalidObjectException.class);
64+
65+
test("invalid_null", InvalidObjectException.class);
66+
test("invalid_valid", null); // valid data is enough if name is invalid
67+
test("invalid_invalid", InvalidObjectException.class);
68+
test("invalid_wrongType", InvalidObjectException.class);
69+
70+
test("wrongType_null", InvalidObjectException.class);
71+
test("wrongType_valid", InvalidObjectException.class);
72+
test("wrongType_invalid", InvalidObjectException.class);
73+
test("wrongType_wrongType", InvalidObjectException.class);
74+
75+
test("valid_null", null); // the valid name is enough
76+
test("valid_valid", null); // the valid name is enough
77+
test("valid_invalid", null); // the valid name is enough
78+
test("valid_wrongType", InvalidObjectException.class);
79+
}
80+
81+
private static void test(String test, Class<?> expected) {
82+
String fileName = test + ".ser";
83+
File file = new File(System.getProperty("test.src", "."), fileName);
84+
Class<?> actual = null;
85+
try (var fis = new FileInputStream(file);
86+
var ois = new ObjectInputStream(fis))
87+
{
88+
ois.readObject();
89+
} catch (Exception e) {
90+
actual = e.getClass();
91+
}
92+
if (actual != expected) {
93+
System.err.println("Test: " + test);
94+
System.err.println("Expected: " + expected);
95+
System.err.println("Actual: " + actual);
96+
throw new RuntimeException("Test failed");
97+
}
98+
}
99+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)