Skip to content

Commit

Permalink
8272805: Avoid looking up standard charsets
Browse files Browse the repository at this point in the history
Reviewed-by: weijun, naoto, dfuchs, azvegint, erikj
  • Loading branch information
mrserb committed Sep 2, 2021
1 parent 92b05fe commit 7fff22a
Showing 53 changed files with 236 additions and 266 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, 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
@@ -27,7 +27,6 @@

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

for (String line : Files.readAllLines(Paths.get(filename),
Charset.forName("UTF-8"))) {
for (String line : Files.readAllLines(Paths.get(filename))) {
line = line.toLowerCase(Locale.ROOT);
int index = line.indexOf(' ') + 1;
if (line.startsWith("file-date:")) {
10 changes: 6 additions & 4 deletions src/demo/share/jfc/Font2DTest/Font2DTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -72,6 +72,8 @@
import javax.swing.*;
import javax.swing.event.*;

import static java.nio.charset.StandardCharsets.UTF_16;

/**
* Font2DTest.java
*
@@ -589,7 +591,7 @@ private void readTextFile( String fileName ) {
if (numBytes >= 2 &&
(( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) ||
( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF )))
fileText = new String( byteData, "UTF-16" );
fileText = new String(byteData, UTF_16);
/// Otherwise, use system default encoding
else
fileText = new String( byteData );
@@ -647,7 +649,7 @@ private void writeCurrentOptions( String fileName ) {
showFontInfoCBMI.getState() + "\n" +
rm.getSelectedItem() + "\n" +
range[0] + "\n" + range[1] + "\n" + curOptions + tFileName);
byte[] toBeWritten = completeOptions.getBytes( "UTF-16" );
byte[] toBeWritten = completeOptions.getBytes(UTF_16);
bos.write( toBeWritten, 0, toBeWritten.length );
bos.close();
}
@@ -712,7 +714,7 @@ private void loadOptions( String fileName ) {
(byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) )
throw new Exception( "Not a Font2DTest options file" );

String options = new String( byteData, "UTF-16" );
String options = new String(byteData, UTF_16);
StringTokenizer perLine = new StringTokenizer( options, "\n" );
String title = perLine.nextToken();
if ( !title.equals( "Font2DTest Option File" ))
5 changes: 3 additions & 2 deletions src/demo/share/jfc/Font2DTest/FontPanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -80,6 +80,7 @@
import javax.swing.*;

import static java.awt.RenderingHints.*;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

/**
* FontPanel.java
@@ -643,7 +644,7 @@ private void modeSpecificDrawLine( Graphics2D g2, String line,
break;
case DRAW_BYTES:
try {
byte[] lineBytes = line.getBytes( "ISO-8859-1" );
byte[] lineBytes = line.getBytes(ISO_8859_1);
g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
}
catch ( Exception e ) {
7 changes: 4 additions & 3 deletions src/demo/share/jfc/SwingSet2/DemoModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
*
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -46,6 +45,8 @@
import java.applet.*;
import java.net.*;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* A generic SwingSet2 demo module
*
@@ -155,7 +156,7 @@ public void loadSourceCode() {
try {
url = getClass().getResource(filename);
is = url.openStream();
isr = new InputStreamReader(is, "UTF-8");
isr = new InputStreamReader(is, UTF_8);
BufferedReader reader = new BufferedReader(isr);

// Read one line at a time, htmlize using super-spiffy
7 changes: 4 additions & 3 deletions src/demo/share/jfc/SwingSet2/TreeDemo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
*
* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -44,6 +43,8 @@
import java.applet.*;
import java.net.*;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* JTree Demo
*
@@ -84,7 +85,7 @@ public JScrollPane createTree() {
try {
// convert url to buffered string
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
InputStreamReader isr = new InputStreamReader(is, UTF_8);
BufferedReader reader = new BufferedReader(isr);

// read one line at a time, put into tree
Original file line number Diff line number Diff line change
@@ -37,7 +37,10 @@
import java.util.Arrays;
import java.util.Locale;

import static java.nio.charset.StandardCharsets.*;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Output stream marshaling DER-encoded data. This is eventually provided
5 changes: 4 additions & 1 deletion src/java.base/share/classes/sun/security/util/DerValue.java
Original file line number Diff line number Diff line change
@@ -34,7 +34,10 @@
import java.nio.charset.Charset;
import java.util.*;

import static java.nio.charset.StandardCharsets.*;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Represents a single DER-encoded value. DER encoding rules are a subset
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, 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
@@ -51,6 +51,13 @@
import java.util.TreeSet;
import java.util.function.Supplier;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Utility class with different datatransfer helper functions.
*
@@ -115,12 +122,12 @@ private static class StandardEncodingsHolder {

private static SortedSet<String> load() {
final SortedSet<String> tempSet = new TreeSet<>(getCharsetComparator().reversed());
tempSet.add("US-ASCII");
tempSet.add("ISO-8859-1");
tempSet.add("UTF-8");
tempSet.add("UTF-16BE");
tempSet.add("UTF-16LE");
tempSet.add("UTF-16");
tempSet.add(US_ASCII.name());
tempSet.add(ISO_8859_1.name());
tempSet.add(UTF_8.name());
tempSet.add(UTF_16BE.name());
tempSet.add(UTF_16LE.name());
tempSet.add(UTF_16.name());
tempSet.add(Charset.defaultCharset().name());
return Collections.unmodifiableSortedSet(tempSet);
}
@@ -318,13 +325,13 @@ private static class CharsetComparator implements Comparator<String> {
Map<String, Integer> charsetsMap = new HashMap<>(8, 1.0f);

// we prefer Unicode charsets
charsetsMap.put(canonicalName("UTF-16LE"), 4);
charsetsMap.put(canonicalName("UTF-16BE"), 5);
charsetsMap.put(canonicalName("UTF-8"), 6);
charsetsMap.put(canonicalName("UTF-16"), 7);
charsetsMap.put(UTF_16LE.name(), 4);
charsetsMap.put(UTF_16BE.name(), 5);
charsetsMap.put(UTF_8.name(), 6);
charsetsMap.put(UTF_16.name(), 7);

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

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, 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
@@ -27,9 +27,10 @@

import java.nio.charset.Charset;
import java.util.HashMap;

import sun.awt.FontConfiguration;
import sun.font.CompositeFontDescriptor;
import sun.font.SunFontManager;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

class CFontConfiguration extends FontConfiguration {

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

@Override
protected Charset getDefaultFontCharset(String fontName) {
return Charset.forName("ISO8859_1");
return ISO_8859_1;
}

@Override
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
@@ -216,7 +217,7 @@ public void setInput(Object input,
resetStreamSettings();
}

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

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

String keyword = readNullTerminatedString("ISO-8859-1", 80);
String keyword = readNullTerminatedString(ISO_8859_1, 80);
metadata.iTXt_keyword.add(keyword);

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

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

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

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

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

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

private void parse_zTXt_chunk(int chunkLength) throws IOException {
String keyword = readNullTerminatedString("ISO-8859-1", 80);
String keyword = readNullTerminatedString(ISO_8859_1, 80);
int textLength = chunkLength - keyword.length() - 2;
if (textLength < 0) {
throw new IIOException("zTXt chunk length is not proper");
Original file line number Diff line number Diff line change
@@ -158,7 +158,7 @@ public String getFileNameFromPlatformName(String platformName) {

@Override
protected Charset getDefaultFontCharset(String fontName) {
return Charset.forName("ISO8859_1");
return ISO_8859_1;
}

@Override
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, 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
@@ -39,6 +39,8 @@
import java.util.Properties;
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

public class MFontConfiguration extends FontConfiguration {

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

protected Charset getDefaultFontCharset(String fontName) {
return Charset.forName("ISO8859_1");
return ISO_8859_1;
}

protected String getFaceNameFromComponentFontName(String componentFontName) {
Original file line number Diff line number Diff line change
@@ -34,7 +34,8 @@
import java.io.IOException;
import java.util.Vector;

import static java.nio.charset.StandardCharsets.*;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;

import sun.security.krb5.Asn1Exception;
import sun.security.krb5.internal.util.KerberosString;
Original file line number Diff line number Diff line change
@@ -36,15 +36,15 @@
import java.util.Arrays;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.CharBuffer;
import java.nio.ByteBuffer;
import sun.security.util.HexDumpEncoder;
import sun.security.krb5.Confounder;
import sun.security.krb5.internal.crypto.KeyUsage;
import sun.security.krb5.KrbCryptoException;

import static java.nio.charset.StandardCharsets.*;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_8;

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

1 comment on commit 7fff22a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.