Skip to content

Commit

Permalink
8300176: URLEncoder/URLDecoder static fields should be private static…
Browse files Browse the repository at this point in the history
… final

Reviewed-by: redestad, michaelm, stsypanov, dfuchs, jpai
  • Loading branch information
DarraghClarke authored and jaikiran committed Jan 27, 2023
1 parent 107e184 commit d7aa87f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
8 changes: 5 additions & 3 deletions src/java.base/share/classes/java/net/URLDecoder.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,8 @@

package java.net;

import jdk.internal.util.StaticProperty;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
Expand Down Expand Up @@ -89,7 +91,7 @@ public class URLDecoder {
private URLDecoder() {}

// 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.
Expand All @@ -108,7 +110,7 @@ public static String decode(String s) {
String str = null;

try {
str = decode(s, dfltEncName);
str = decode(s, DEFAULT_ENCODING_NAME);
} catch (UnsupportedEncodingException e) {
// The system should always have the default charset
}
Expand Down
46 changes: 20 additions & 26 deletions src/java.base/share/classes/java/net/URLEncoder.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -77,9 +77,9 @@
* @since 1.0
*/
public class URLEncoder {
static BitSet dontNeedEncoding;
static final int caseDiff = ('a' - 'A');
static String dfltEncName;
private static final BitSet DONT_NEED_ENCODING;
private static final int CASE_DIFF = ('a' - 'A');
private static final String DEFAULT_ENCODING_NAME;

static {

Expand Down Expand Up @@ -119,25 +119,19 @@ public class URLEncoder {
*
*/

dontNeedEncoding = new BitSet(256);
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
DONT_NEED_ENCODING = new BitSet(128);

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 */
dontNeedEncoding.set('-');
dontNeedEncoding.set('_');
dontNeedEncoding.set('.');
dontNeedEncoding.set('*');
DONT_NEED_ENCODING.set('-');
DONT_NEED_ENCODING.set('_');
DONT_NEED_ENCODING.set('.');
DONT_NEED_ENCODING.set('*');

dfltEncName = StaticProperty.fileEncoding();
DEFAULT_ENCODING_NAME = StaticProperty.fileEncoding();
}

/**
Expand All @@ -162,7 +156,7 @@ public static String encode(String s) {
String str = null;

try {
str = encode(s, dfltEncName);
str = encode(s, DEFAULT_ENCODING_NAME);
} catch (UnsupportedEncodingException e) {
// The system should always have the default charset
}
Expand Down Expand Up @@ -230,7 +224,7 @@ public static String encode(String s, Charset charset) {
for (int i = 0; i < s.length();) {
int c = s.charAt(i);
//System.out.println("Examining character: " + c);
if (dontNeedEncoding.get(c)) {
if (DONT_NEED_ENCODING.get(c)) {
if (c == ' ') {
c = '+';
needToChange = true;
Expand Down Expand Up @@ -273,7 +267,7 @@ public static String encode(String s, Charset charset) {
}
}
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();
String str = charArrayWriter.toString();
Expand All @@ -284,12 +278,12 @@ public static String encode(String s, Charset charset) {
// converting to use uppercase letter as part of
// the hex value if ch is a letter.
if (Character.isLetter(ch)) {
ch -= caseDiff;
ch -= CASE_DIFF;
}
out.append(ch);
ch = Character.forDigit(b & 0xF, 16);
if (Character.isLetter(ch)) {
ch -= caseDiff;
ch -= CASE_DIFF;
}
out.append(ch);
}
Expand Down

1 comment on commit d7aa87f

@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.