Skip to content

Commit 7fff22a

Browse files
committed
8272805: Avoid looking up standard charsets
Reviewed-by: weijun, naoto, dfuchs, azvegint, erikj
1 parent 92b05fe commit 7fff22a

File tree

53 files changed

+236
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+236
-266
lines changed

make/jdk/src/classes/build/tools/generatelsrequivmaps/EquivMapsGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, 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
@@ -27,7 +27,6 @@
2727

2828
import java.io.BufferedWriter;
2929
import java.io.IOException;
30-
import java.nio.charset.Charset;
3130
import java.nio.file.Files;
3231
import java.nio.file.Paths;
3332
import java.time.ZoneId;
@@ -79,8 +78,7 @@ private static void readLSRfile(String filename) throws Exception {
7978
String preferred = null;
8079
String prefix = null;
8180

82-
for (String line : Files.readAllLines(Paths.get(filename),
83-
Charset.forName("UTF-8"))) {
81+
for (String line : Files.readAllLines(Paths.get(filename))) {
8482
line = line.toLowerCase(Locale.ROOT);
8583
int index = line.indexOf(' ') + 1;
8684
if (line.startsWith("file-date:")) {

src/demo/share/jfc/Font2DTest/Font2DTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -72,6 +72,8 @@
7272
import javax.swing.*;
7373
import javax.swing.event.*;
7474

75+
import static java.nio.charset.StandardCharsets.UTF_16;
76+
7577
/**
7678
* Font2DTest.java
7779
*
@@ -589,7 +591,7 @@ private void readTextFile( String fileName ) {
589591
if (numBytes >= 2 &&
590592
(( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) ||
591593
( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF )))
592-
fileText = new String( byteData, "UTF-16" );
594+
fileText = new String(byteData, UTF_16);
593595
/// Otherwise, use system default encoding
594596
else
595597
fileText = new String( byteData );
@@ -647,7 +649,7 @@ private void writeCurrentOptions( String fileName ) {
647649
showFontInfoCBMI.getState() + "\n" +
648650
rm.getSelectedItem() + "\n" +
649651
range[0] + "\n" + range[1] + "\n" + curOptions + tFileName);
650-
byte[] toBeWritten = completeOptions.getBytes( "UTF-16" );
652+
byte[] toBeWritten = completeOptions.getBytes(UTF_16);
651653
bos.write( toBeWritten, 0, toBeWritten.length );
652654
bos.close();
653655
}
@@ -712,7 +714,7 @@ private void loadOptions( String fileName ) {
712714
(byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) )
713715
throw new Exception( "Not a Font2DTest options file" );
714716

715-
String options = new String( byteData, "UTF-16" );
717+
String options = new String(byteData, UTF_16);
716718
StringTokenizer perLine = new StringTokenizer( options, "\n" );
717719
String title = perLine.nextToken();
718720
if ( !title.equals( "Font2DTest Option File" ))

src/demo/share/jfc/Font2DTest/FontPanel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -80,6 +80,7 @@
8080
import javax.swing.*;
8181

8282
import static java.awt.RenderingHints.*;
83+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
8384

8485
/**
8586
* FontPanel.java
@@ -643,7 +644,7 @@ private void modeSpecificDrawLine( Graphics2D g2, String line,
643644
break;
644645
case DRAW_BYTES:
645646
try {
646-
byte[] lineBytes = line.getBytes( "ISO-8859-1" );
647+
byte[] lineBytes = line.getBytes(ISO_8859_1);
647648
g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
648649
}
649650
catch ( Exception e ) {

src/demo/share/jfc/SwingSet2/DemoModule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
*
3-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
43
*
54
* Redistribution and use in source and binary forms, with or without
65
* modification, are permitted provided that the following conditions
@@ -46,6 +45,8 @@
4645
import java.applet.*;
4746
import java.net.*;
4847

48+
import static java.nio.charset.StandardCharsets.UTF_8;
49+
4950
/**
5051
* A generic SwingSet2 demo module
5152
*
@@ -155,7 +156,7 @@ public void loadSourceCode() {
155156
try {
156157
url = getClass().getResource(filename);
157158
is = url.openStream();
158-
isr = new InputStreamReader(is, "UTF-8");
159+
isr = new InputStreamReader(is, UTF_8);
159160
BufferedReader reader = new BufferedReader(isr);
160161

161162
// Read one line at a time, htmlize using super-spiffy

src/demo/share/jfc/SwingSet2/TreeDemo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
*
3-
* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
43
*
54
* Redistribution and use in source and binary forms, with or without
65
* modification, are permitted provided that the following conditions
@@ -44,6 +43,8 @@
4443
import java.applet.*;
4544
import java.net.*;
4645

46+
import static java.nio.charset.StandardCharsets.UTF_8;
47+
4748
/**
4849
* JTree Demo
4950
*
@@ -84,7 +85,7 @@ public JScrollPane createTree() {
8485
try {
8586
// convert url to buffered string
8687
InputStream is = url.openStream();
87-
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
88+
InputStreamReader isr = new InputStreamReader(is, UTF_8);
8889
BufferedReader reader = new BufferedReader(isr);
8990

9091
// read one line at a time, put into tree

src/java.base/share/classes/sun/security/util/DerOutputStream.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
import java.util.Arrays;
3838
import java.util.Locale;
3939

40-
import static java.nio.charset.StandardCharsets.*;
40+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
41+
import static java.nio.charset.StandardCharsets.US_ASCII;
42+
import static java.nio.charset.StandardCharsets.UTF_16BE;
43+
import static java.nio.charset.StandardCharsets.UTF_8;
4144

4245
/**
4346
* Output stream marshaling DER-encoded data. This is eventually provided

src/java.base/share/classes/sun/security/util/DerValue.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
import java.nio.charset.Charset;
3535
import java.util.*;
3636

37-
import static java.nio.charset.StandardCharsets.*;
37+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
38+
import static java.nio.charset.StandardCharsets.US_ASCII;
39+
import static java.nio.charset.StandardCharsets.UTF_16BE;
40+
import static java.nio.charset.StandardCharsets.UTF_8;
3841

3942
/**
4043
* Represents a single DER-encoded value. DER encoding rules are a subset

src/java.datatransfer/share/classes/sun/datatransfer/DataFlavorUtil.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2021, 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
@@ -51,6 +51,13 @@
5151
import java.util.TreeSet;
5252
import java.util.function.Supplier;
5353

54+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
55+
import static java.nio.charset.StandardCharsets.US_ASCII;
56+
import static java.nio.charset.StandardCharsets.UTF_16;
57+
import static java.nio.charset.StandardCharsets.UTF_16BE;
58+
import static java.nio.charset.StandardCharsets.UTF_16LE;
59+
import static java.nio.charset.StandardCharsets.UTF_8;
60+
5461
/**
5562
* Utility class with different datatransfer helper functions.
5663
*
@@ -115,12 +122,12 @@ private static class StandardEncodingsHolder {
115122

116123
private static SortedSet<String> load() {
117124
final SortedSet<String> tempSet = new TreeSet<>(getCharsetComparator().reversed());
118-
tempSet.add("US-ASCII");
119-
tempSet.add("ISO-8859-1");
120-
tempSet.add("UTF-8");
121-
tempSet.add("UTF-16BE");
122-
tempSet.add("UTF-16LE");
123-
tempSet.add("UTF-16");
125+
tempSet.add(US_ASCII.name());
126+
tempSet.add(ISO_8859_1.name());
127+
tempSet.add(UTF_8.name());
128+
tempSet.add(UTF_16BE.name());
129+
tempSet.add(UTF_16LE.name());
130+
tempSet.add(UTF_16.name());
124131
tempSet.add(Charset.defaultCharset().name());
125132
return Collections.unmodifiableSortedSet(tempSet);
126133
}
@@ -318,13 +325,13 @@ private static class CharsetComparator implements Comparator<String> {
318325
Map<String, Integer> charsetsMap = new HashMap<>(8, 1.0f);
319326

320327
// we prefer Unicode charsets
321-
charsetsMap.put(canonicalName("UTF-16LE"), 4);
322-
charsetsMap.put(canonicalName("UTF-16BE"), 5);
323-
charsetsMap.put(canonicalName("UTF-8"), 6);
324-
charsetsMap.put(canonicalName("UTF-16"), 7);
328+
charsetsMap.put(UTF_16LE.name(), 4);
329+
charsetsMap.put(UTF_16BE.name(), 5);
330+
charsetsMap.put(UTF_8.name(), 6);
331+
charsetsMap.put(UTF_16.name(), 7);
325332

326333
// US-ASCII is the worst charset supported
327-
charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX);
334+
charsetsMap.put(US_ASCII.name(), WORST_CHARSET_INDEX);
328335

329336
charsetsMap.putIfAbsent(Charset.defaultCharset().name(), DEFAULT_CHARSET_INDEX);
330337

src/java.desktop/macosx/classes/sun/font/CFontConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, 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
@@ -27,9 +27,10 @@
2727

2828
import java.nio.charset.Charset;
2929
import java.util.HashMap;
30+
3031
import sun.awt.FontConfiguration;
31-
import sun.font.CompositeFontDescriptor;
32-
import sun.font.SunFontManager;
32+
33+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
3334

3435
class CFontConfiguration extends FontConfiguration {
3536

@@ -79,7 +80,7 @@ protected String mapFileName(String fileName) {
7980

8081
@Override
8182
protected Charset getDefaultFontCharset(String fontName) {
82-
return Charset.forName("ISO8859_1");
83+
return ISO_8859_1;
8384
}
8485

8586
@Override

src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.IOException;
4343
import java.io.InputStream;
4444
import java.io.SequenceInputStream;
45+
import java.nio.charset.Charset;
4546
import java.util.ArrayList;
4647
import java.util.Arrays;
4748
import java.util.Enumeration;
@@ -216,7 +217,7 @@ public void setInput(Object input,
216217
resetStreamSettings();
217218
}
218219

219-
private String readNullTerminatedString(String charset, int maxLen) throws IOException {
220+
private String readNullTerminatedString(Charset charset, int maxLen) throws IOException {
220221
ByteArrayOutputStream baos = new ByteArrayOutputStream();
221222
int b = 0;
222223
int count = 0;
@@ -438,7 +439,7 @@ private void parse_hIST_chunk(int chunkLength) throws IOException,
438439
}
439440

440441
private void parse_iCCP_chunk(int chunkLength) throws IOException {
441-
String keyword = readNullTerminatedString("ISO-8859-1", 80);
442+
String keyword = readNullTerminatedString(ISO_8859_1, 80);
442443
int compressedProfileLength = chunkLength - keyword.length() - 2;
443444
if (compressedProfileLength <= 0) {
444445
throw new IIOException("iCCP chunk length is not proper");
@@ -458,7 +459,7 @@ private void parse_iCCP_chunk(int chunkLength) throws IOException {
458459
private void parse_iTXt_chunk(int chunkLength) throws IOException {
459460
long chunkStart = stream.getStreamPosition();
460461

461-
String keyword = readNullTerminatedString("ISO-8859-1", 80);
462+
String keyword = readNullTerminatedString(ISO_8859_1, 80);
462463
metadata.iTXt_keyword.add(keyword);
463464

464465
int compressionFlag = stream.readUnsignedByte();
@@ -469,7 +470,7 @@ private void parse_iTXt_chunk(int chunkLength) throws IOException {
469470

470471
long pos = stream.getStreamPosition();
471472
int remainingLen = (int)(chunkStart + chunkLength - pos);
472-
String languageTag = readNullTerminatedString("UTF8", remainingLen);
473+
String languageTag = readNullTerminatedString(UTF_8, remainingLen);
473474
metadata.iTXt_languageTag.add(languageTag);
474475

475476
pos = stream.getStreamPosition();
@@ -478,7 +479,7 @@ private void parse_iTXt_chunk(int chunkLength) throws IOException {
478479
throw new IIOException("iTXt chunk length is not proper");
479480
}
480481
String translatedKeyword =
481-
readNullTerminatedString("UTF8", remainingLen);
482+
readNullTerminatedString(UTF_8, remainingLen);
482483
metadata.iTXt_translatedKeyword.add(translatedKeyword);
483484

484485
String text;
@@ -538,7 +539,7 @@ private void parse_sBIT_chunk() throws IOException {
538539

539540
private void parse_sPLT_chunk(int chunkLength)
540541
throws IOException, IIOException {
541-
metadata.sPLT_paletteName = readNullTerminatedString("ISO-8859-1", 80);
542+
metadata.sPLT_paletteName = readNullTerminatedString(ISO_8859_1, 80);
542543
int remainingChunkLength = chunkLength -
543544
(metadata.sPLT_paletteName.length() + 1);
544545
if (remainingChunkLength <= 0) {
@@ -585,7 +586,7 @@ private void parse_sRGB_chunk() throws IOException {
585586
}
586587

587588
private void parse_tEXt_chunk(int chunkLength) throws IOException {
588-
String keyword = readNullTerminatedString("ISO-8859-1", 80);
589+
String keyword = readNullTerminatedString(ISO_8859_1, 80);
589590
int textLength = chunkLength - keyword.length() - 1;
590591
if (textLength < 0) {
591592
throw new IIOException("tEXt chunk length is not proper");
@@ -683,7 +684,7 @@ private static byte[] inflate(byte[] b) throws IOException {
683684
}
684685

685686
private void parse_zTXt_chunk(int chunkLength) throws IOException {
686-
String keyword = readNullTerminatedString("ISO-8859-1", 80);
687+
String keyword = readNullTerminatedString(ISO_8859_1, 80);
687688
int textLength = chunkLength - keyword.length() - 2;
688689
if (textLength < 0) {
689690
throw new IIOException("zTXt chunk length is not proper");

src/java.desktop/unix/classes/sun/font/FcFontConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public String getFileNameFromPlatformName(String platformName) {
158158

159159
@Override
160160
protected Charset getDefaultFontCharset(String fontName) {
161-
return Charset.forName("ISO8859_1");
161+
return ISO_8859_1;
162162
}
163163

164164
@Override

src/java.desktop/unix/classes/sun/font/MFontConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, 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
@@ -39,6 +39,8 @@
3939
import java.util.Properties;
4040
import java.util.Scanner;
4141

42+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
43+
4244
public class MFontConfiguration extends FontConfiguration {
4345

4446
private static FontConfiguration fontConfig = null;
@@ -180,7 +182,7 @@ protected String getEncoding(String awtFontName,
180182
}
181183

182184
protected Charset getDefaultFontCharset(String fontName) {
183-
return Charset.forName("ISO8859_1");
185+
return ISO_8859_1;
184186
}
185187

186188
protected String getFaceNameFromComponentFontName(String componentFontName) {

src/java.security.jgss/share/classes/sun/security/krb5/internal/PAData.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
import java.io.IOException;
3535
import java.util.Vector;
3636

37-
import static java.nio.charset.StandardCharsets.*;
37+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
38+
import static java.nio.charset.StandardCharsets.UTF_8;
3839

3940
import sun.security.krb5.Asn1Exception;
4041
import sun.security.krb5.internal.util.KerberosString;

src/java.security.jgss/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
import java.util.Arrays;
3737
import java.io.ByteArrayInputStream;
3838
import java.io.ByteArrayOutputStream;
39-
import java.nio.charset.Charset;
4039
import java.nio.CharBuffer;
4140
import java.nio.ByteBuffer;
4241
import sun.security.util.HexDumpEncoder;
4342
import sun.security.krb5.Confounder;
4443
import sun.security.krb5.internal.crypto.KeyUsage;
4544
import sun.security.krb5.KrbCryptoException;
4645

47-
import static java.nio.charset.StandardCharsets.*;
46+
import static java.nio.charset.StandardCharsets.UTF_16LE;
47+
import static java.nio.charset.StandardCharsets.UTF_8;
4848

4949
/**
5050
* Implements Derive Key cryptography functionality as defined in RFC 3961.

0 commit comments

Comments
 (0)