Skip to content
Permalink
Browse files
8259699: Reduce char[] copying in URLEncoder.encode(String, Charset)
Reviewed-by: attila, redestad, chegar
  • Loading branch information
stsypanov authored and cl4es committed Jan 14, 2021
1 parent ff3e6e4 commit c822eda1d8ad2e9bd22457c8f66a490168984b2b
Showing with 9 additions and 9 deletions.
  1. +9 −9 src/java.base/share/classes/java/net/URLEncoder.java
@@ -76,7 +76,7 @@
public class URLEncoder {
static BitSet dontNeedEncoding;
static final int caseDiff = ('a' - 'A');
static String dfltEncName = null;
static String dfltEncName;

static {

@@ -171,7 +171,7 @@ public static String encode(String s) {
* Translates a string into {@code application/x-www-form-urlencoded}
* format using a specific encoding scheme.
* <p>
* This method behaves the same as {@linkplain encode(String s, Charset charset)}
* This method behaves the same as {@linkplain #encode(String s, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* using the given encoding name.
*
@@ -225,7 +225,7 @@ public static String encode(String s, Charset charset) {
CharArrayWriter charArrayWriter = new CharArrayWriter();

for (int i = 0; i < s.length();) {
int c = (int) s.charAt(i);
int c = s.charAt(i);
//System.out.println("Examining character: " + c);
if (dontNeedEncoding.get(c)) {
if (c == ' ') {
@@ -253,7 +253,7 @@ public static String encode(String s, Charset charset) {
+ " is high surrogate");
*/
if ( (i+1) < s.length()) {
int d = (int) s.charAt(i+1);
int d = s.charAt(i+1);
/*
System.out.println("\tExamining "
+ Integer.toHexString(d));
@@ -270,21 +270,21 @@ public static String encode(String s, Charset charset) {
}
}
i++;
} while (i < s.length() && !dontNeedEncoding.get((c = (int) s.charAt(i))));
} while (i < s.length() && !dontNeedEncoding.get((c = s.charAt(i))));

charArrayWriter.flush();
String str = new String(charArrayWriter.toCharArray());
String str = charArrayWriter.toString();
byte[] ba = str.getBytes(charset);
for (int j = 0; j < ba.length; j++) {
for (byte b : ba) {
out.append('%');
char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
char ch = Character.forDigit((b >> 4) & 0xF, 16);
// converting to use uppercase letter as part of
// the hex value if ch is a letter.
if (Character.isLetter(ch)) {
ch -= caseDiff;
}
out.append(ch);
ch = Character.forDigit(ba[j] & 0xF, 16);
ch = Character.forDigit(b & 0xF, 16);
if (Character.isLetter(ch)) {
ch -= caseDiff;
}

1 comment on commit c822eda

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on c822eda Jan 14, 2021

Please sign in to comment.