Skip to content

Commit 3efbd5f

Browse files
committedSep 30, 2022
8294626: Improve URL protocol lower casing
Reviewed-by: dfuchs
1 parent 052a924 commit 3efbd5f

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed
 

‎src/java.base/share/classes/java/net/URL.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public URL(String protocol, String host, int port, String file,
440440
}
441441
}
442442

443-
protocol = toLowerCase(protocol);
443+
protocol = lowerCaseProtocol(protocol);
444444
this.protocol = protocol;
445445
if (host != null) {
446446

@@ -633,7 +633,7 @@ public URL(URL context, String spec, URLStreamHandler handler)
633633
for (i = start ; !aRef && (i < limit) &&
634634
((c = spec.charAt(i)) != '/') ; i++) {
635635
if (c == ':') {
636-
String s = toLowerCase(spec.substring(start, i));
636+
String s = lowerCaseProtocol(spec.substring(start, i));
637637
if (isValidProtocol(s)) {
638638
newProtocol = s;
639639
start = i + 1;
@@ -1384,9 +1384,13 @@ public URLStreamHandler run() {
13841384
* Returns the protocol in lower case. Special cases known protocols
13851385
* to avoid loading locale classes during startup.
13861386
*/
1387-
static String toLowerCase(String protocol) {
1388-
if (protocol.equals("jrt") || protocol.equals("file") || protocol.equals("jar")) {
1389-
return protocol;
1387+
static String lowerCaseProtocol(String protocol) {
1388+
if (protocol.equals("jrt")) {
1389+
return "jrt";
1390+
} else if (protocol.equals("file")) {
1391+
return "file";
1392+
} else if (protocol.equals("jar")) {
1393+
return "jar";
13901394
} else {
13911395
return protocol.toLowerCase(Locale.ROOT);
13921396
}

‎src/java.base/share/classes/java/net/URLConnection.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ public void setDefaultUseCaches(boolean defaultusecaches) {
10921092
* @since 9
10931093
*/
10941094
public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
1095-
protocol = protocol.toLowerCase(Locale.US);
1095+
protocol = URL.lowerCaseProtocol(protocol);
10961096
defaultCaching.put(protocol, defaultVal);
10971097
}
10981098

@@ -1108,7 +1108,7 @@ public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
11081108
* @since 9
11091109
*/
11101110
public static boolean getDefaultUseCaches(String protocol) {
1111-
Boolean protoDefault = defaultCaching.get(protocol.toLowerCase(Locale.US));
1111+
Boolean protoDefault = defaultCaching.get(URL.lowerCaseProtocol(protocol));
11121112
if (protoDefault != null) {
11131113
return protoDefault.booleanValue();
11141114
} else {

‎src/java.base/share/classes/sun/net/util/URLUtil.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ public static String urlNoFragString(URL url) {
4949

5050
String protocol = url.getProtocol();
5151
if (protocol != null) {
52-
/* protocol is compared case-insensitive, so convert to lowercase */
53-
protocol = protocol.toLowerCase();
52+
/* protocol is compared case-insensitive, so convert to lowercase
53+
* if needed. URL will store from lower-cased String literals for
54+
* built-in protocols, so avoid calling toLowerCase for these and
55+
* use identity tests for speed
56+
*/
57+
if (protocol != "file" && protocol != "jrt" && protocol != "jar") {
58+
protocol = protocol.toLowerCase();
59+
}
5460
strForm.append(protocol);
5561
strForm.append("://");
5662
}
5763

5864
String host = url.getHost();
5965
if (host != null) {
6066
/* host is compared case-insensitive, so convert to lowercase */
61-
host = host.toLowerCase();
62-
strForm.append(host);
67+
if (!host.isEmpty()) {
68+
strForm.append(host.toLowerCase());
69+
}
6370

6471
int port = url.getPort();
6572
if (port == -1) {

0 commit comments

Comments
 (0)
Please sign in to comment.