8300176: URLEncoder/URLDecoder static fields should be private static final

Reviewed-by: redestad, michaelm, stsypanov, dfuchs, jpai
This commit is contained in:
Darragh Clarke 2023-01-27 10:52:06 +00:00 committed by Jaikiran Pai
parent 107e184d59
commit d7aa87fdbd
2 changed files with 26 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package java.net; package java.net;
import jdk.internal.util.StaticProperty;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.IllegalCharsetNameException;
@ -89,7 +91,7 @@ public class URLDecoder {
private URLDecoder() {} private URLDecoder() {}
// The default charset // The default charset
static String dfltEncName = URLEncoder.dfltEncName; private static final String DEFAULT_ENCODING_NAME = StaticProperty.fileEncoding();
/** /**
* Decodes a {@code x-www-form-urlencoded} string. * Decodes a {@code x-www-form-urlencoded} string.
@ -108,7 +110,7 @@ public class URLDecoder {
String str = null; String str = null;
try { try {
str = decode(s, dfltEncName); str = decode(s, DEFAULT_ENCODING_NAME);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
// The system should always have the default charset // The system should always have the default charset
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -77,9 +77,9 @@ import jdk.internal.util.StaticProperty;
* @since 1.0 * @since 1.0
*/ */
public class URLEncoder { public class URLEncoder {
static BitSet dontNeedEncoding; private static final BitSet DONT_NEED_ENCODING;
static final int caseDiff = ('a' - 'A'); private static final int CASE_DIFF = ('a' - 'A');
static String dfltEncName; private static final String DEFAULT_ENCODING_NAME;
static { static {
@ -119,25 +119,19 @@ public class URLEncoder {
* *
*/ */
dontNeedEncoding = new BitSet(256); DONT_NEED_ENCODING = new BitSet(128);
int i;
for (i = 'a'; i <= 'z'; i++) {
dontNeedEncoding.set(i);
}
for (i = 'A'; i <= 'Z'; i++) {
dontNeedEncoding.set(i);
}
for (i = '0'; i <= '9'; i++) {
dontNeedEncoding.set(i);
}
dontNeedEncoding.set(' '); /* encoding a space to a + is done
* in the encode() method */
dontNeedEncoding.set('-');
dontNeedEncoding.set('_');
dontNeedEncoding.set('.');
dontNeedEncoding.set('*');
dfltEncName = StaticProperty.fileEncoding(); DONT_NEED_ENCODING.set('a', 'z' + 1);
DONT_NEED_ENCODING.set('A', 'Z' + 1);
DONT_NEED_ENCODING.set('0', '9' + 1);
DONT_NEED_ENCODING.set(' '); /* encoding a space to a + is done
* in the encode() method */
DONT_NEED_ENCODING.set('-');
DONT_NEED_ENCODING.set('_');
DONT_NEED_ENCODING.set('.');
DONT_NEED_ENCODING.set('*');
DEFAULT_ENCODING_NAME = StaticProperty.fileEncoding();
} }
/** /**
@ -162,7 +156,7 @@ public class URLEncoder {
String str = null; String str = null;
try { try {
str = encode(s, dfltEncName); str = encode(s, DEFAULT_ENCODING_NAME);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
// The system should always have the default charset // The system should always have the default charset
} }
@ -230,7 +224,7 @@ public class URLEncoder {
for (int i = 0; i < s.length();) { for (int i = 0; i < s.length();) {
int c = s.charAt(i); int c = s.charAt(i);
//System.out.println("Examining character: " + c); //System.out.println("Examining character: " + c);
if (dontNeedEncoding.get(c)) { if (DONT_NEED_ENCODING.get(c)) {
if (c == ' ') { if (c == ' ') {
c = '+'; c = '+';
needToChange = true; needToChange = true;
@ -273,7 +267,7 @@ public class URLEncoder {
} }
} }
i++; i++;
} while (i < s.length() && !dontNeedEncoding.get((c = s.charAt(i)))); } while (i < s.length() && !DONT_NEED_ENCODING.get((c = s.charAt(i))));
charArrayWriter.flush(); charArrayWriter.flush();
String str = charArrayWriter.toString(); String str = charArrayWriter.toString();
@ -284,12 +278,12 @@ public class URLEncoder {
// converting to use uppercase letter as part of // converting to use uppercase letter as part of
// the hex value if ch is a letter. // the hex value if ch is a letter.
if (Character.isLetter(ch)) { if (Character.isLetter(ch)) {
ch -= caseDiff; ch -= CASE_DIFF;
} }
out.append(ch); out.append(ch);
ch = Character.forDigit(b & 0xF, 16); ch = Character.forDigit(b & 0xF, 16);
if (Character.isLetter(ch)) { if (Character.isLetter(ch)) {
ch -= caseDiff; ch -= CASE_DIFF;
} }
out.append(ch); out.append(ch);
} }