Skip to content

Commit b397472

Browse files
committed
8262915: java.awt.color.ColorSpace.getName() is not thread-safe
Reviewed-by: azvegint, aivanov
1 parent 268d9b7 commit b397472

File tree

1 file changed

+29
-48
lines changed

1 file changed

+29
-48
lines changed

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

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public abstract class ColorSpace implements Serializable {
108108
* The number of components in the color space.
109109
*/
110110
private final int numComponents;
111-
private transient String [] compName = null;
111+
112+
/**
113+
* Lazy-initialized names of components in the color space.
114+
*/
115+
private transient volatile String [] compName;
112116

113117
/**
114118
* The lazy cache of singletons for the predefined built-in color spaces.
@@ -447,60 +451,37 @@ public int getNumComponents() {
447451
/**
448452
* Returns the name of the component given the component index.
449453
*
450-
* @param idx the component index
454+
* @param component the component index
451455
* @return the name of the component at the specified index
452-
* @throws IllegalArgumentException if {@code idx} is less than 0 or greater
453-
* than {@code numComponents - 1}
456+
* @throws IllegalArgumentException if {@code component} is less than 0 or
457+
* greater than {@code numComponents - 1}
454458
*/
455-
public String getName (int idx) {
456-
/* REMIND - handle common cases here */
457-
rangeCheck(idx);
459+
public String getName(int component) {
460+
rangeCheck(component);
458461
if (compName == null) {
459-
switch (type) {
460-
case ColorSpace.TYPE_XYZ:
461-
compName = new String[] {"X", "Y", "Z"};
462-
break;
463-
case ColorSpace.TYPE_Lab:
464-
compName = new String[] {"L", "a", "b"};
465-
break;
466-
case ColorSpace.TYPE_Luv:
467-
compName = new String[] {"L", "u", "v"};
468-
break;
469-
case ColorSpace.TYPE_YCbCr:
470-
compName = new String[] {"Y", "Cb", "Cr"};
471-
break;
472-
case ColorSpace.TYPE_Yxy:
473-
compName = new String[] {"Y", "x", "y"};
474-
break;
475-
case ColorSpace.TYPE_RGB:
476-
compName = new String[] {"Red", "Green", "Blue"};
477-
break;
478-
case ColorSpace.TYPE_GRAY:
479-
compName = new String[] {"Gray"};
480-
break;
481-
case ColorSpace.TYPE_HSV:
482-
compName = new String[] {"Hue", "Saturation", "Value"};
483-
break;
484-
case ColorSpace.TYPE_HLS:
485-
compName = new String[] {"Hue", "Lightness",
486-
"Saturation"};
487-
break;
488-
case ColorSpace.TYPE_CMYK:
489-
compName = new String[] {"Cyan", "Magenta", "Yellow",
490-
"Black"};
491-
break;
492-
case ColorSpace.TYPE_CMY:
493-
compName = new String[] {"Cyan", "Magenta", "Yellow"};
494-
break;
495-
default:
496-
String [] tmp = new String[numComponents];
462+
compName = switch (type) {
463+
case TYPE_XYZ -> new String[]{"X", "Y", "Z"};
464+
case TYPE_Lab -> new String[]{"L", "a", "b"};
465+
case TYPE_Luv -> new String[]{"L", "u", "v"};
466+
case TYPE_YCbCr -> new String[]{"Y", "Cb", "Cr"};
467+
case TYPE_Yxy -> new String[]{"Y", "x", "y"};
468+
case TYPE_RGB -> new String[]{"Red", "Green", "Blue"};
469+
case TYPE_GRAY -> new String[]{"Gray"};
470+
case TYPE_HSV -> new String[]{"Hue", "Saturation", "Value"};
471+
case TYPE_HLS -> new String[]{"Hue", "Lightness", "Saturation"};
472+
case TYPE_CMYK -> new String[]{"Cyan", "Magenta", "Yellow",
473+
"Black"};
474+
case TYPE_CMY -> new String[]{"Cyan", "Magenta", "Yellow"};
475+
default -> {
476+
String[] tmp = new String[getNumComponents()];
497477
for (int i = 0; i < tmp.length; i++) {
498478
tmp[i] = "Unnamed color component(" + i + ")";
499479
}
500-
compName = tmp;
501-
}
480+
yield tmp;
481+
}
482+
};
502483
}
503-
return compName[idx];
484+
return compName[component];
504485
}
505486

506487
/**

0 commit comments

Comments
 (0)