8285521: Minor improvements in java.net.URI

Reviewed-by: dfuchs
This commit is contained in:
Sergey Tsypanov 2022-06-23 17:52:37 +00:00 committed by Daniel Fuchs
parent 13cbb3a416
commit 740169ce1c

View file

@ -864,7 +864,7 @@ public final class URI
private static boolean validSchemeAndPath(String scheme, String path) { private static boolean validSchemeAndPath(String scheme, String path) {
try { try {
URI u = new URI(scheme + ":" + path); URI u = new URI(scheme + ':' + path);
return scheme.equals(u.scheme) && path.equals(u.path); return scheme.equals(u.scheme) && path.equals(u.path);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
return false; return false;
@ -1928,7 +1928,7 @@ public final class URI
if ((sn != tn) && testForEquality) if ((sn != tn) && testForEquality)
return sn - tn; return sn - tn;
int val = 0; int val = 0;
int n = sn < tn ? sn : tn; int n = Math.min(sn, tn);
for (int i = 0; i < n; ) { for (int i = 0; i < n; ) {
char c = s.charAt(i); char c = s.charAt(i);
char d = t.charAt(i); char d = t.charAt(i);
@ -2023,18 +2023,12 @@ public final class URI
if (authority.startsWith("[")) { if (authority.startsWith("[")) {
// authority should (but may not) contain an embedded IPv6 address // authority should (but may not) contain an embedded IPv6 address
int end = authority.indexOf(']'); int end = authority.indexOf(']');
String doquote = authority, dontquote = ""; String doquote = authority;
if (end != -1 && authority.indexOf(':') != -1) { if (end != -1 && authority.indexOf(':') != -1) {
// the authority contains an IPv6 address // the authority contains an IPv6 address
if (end == authority.length()) { sb.append(authority, 0, end + 1);
dontquote = authority; doquote = authority.substring(end + 1);
doquote = "";
} else {
dontquote = authority.substring(0 , end + 1);
doquote = authority.substring(end + 1);
}
} }
sb.append(dontquote);
sb.append(quote(doquote, sb.append(quote(doquote,
L_REG_NAME | L_SERVER, L_REG_NAME | L_SERVER,
H_REG_NAME | H_SERVER)); H_REG_NAME | H_SERVER));
@ -2062,15 +2056,8 @@ public final class URI
if (opaquePart.startsWith("//[")) { if (opaquePart.startsWith("//[")) {
int end = opaquePart.indexOf(']'); int end = opaquePart.indexOf(']');
if (end != -1 && opaquePart.indexOf(':')!=-1) { if (end != -1 && opaquePart.indexOf(':')!=-1) {
String doquote, dontquote; String doquote = opaquePart.substring(end + 1);
if (end == opaquePart.length()) { sb.append(opaquePart, 0, end + 1);
dontquote = opaquePart;
doquote = "";
} else {
dontquote = opaquePart.substring(0,end+1);
doquote = opaquePart.substring(end+1);
}
sb.append (dontquote);
sb.append(quote(doquote, L_URIC, H_URIC)); sb.append(quote(doquote, L_URIC, H_URIC));
} }
} else { } else {
@ -2119,8 +2106,7 @@ public final class URI
// -- Normalization, resolution, and relativization -- // -- Normalization, resolution, and relativization --
// RFC2396 5.2 (6) // RFC2396 5.2 (6)
private static String resolvePath(String base, String child, private static String resolvePath(String base, String child)
boolean absolute)
{ {
int i = base.lastIndexOf('/'); int i = base.lastIndexOf('/');
int cn = child.length(); int cn = child.length();
@ -2131,13 +2117,12 @@ public final class URI
if (i >= 0) if (i >= 0)
path = base.substring(0, i + 1); path = base.substring(0, i + 1);
} else { } else {
StringBuilder sb = new StringBuilder(base.length() + cn);
// 5.2 (6a) // 5.2 (6a)
if (i >= 0) if (i >= 0)
sb.append(base, 0, i + 1); path = base.substring(0, i + 1).concat(child);
// 5.2 (6b) // 5.2 (6b)
sb.append(child); else
path = sb.toString(); path = child;
} }
// 5.2 (6c-f) // 5.2 (6c-f)
@ -2192,13 +2177,13 @@ public final class URI
ru.userInfo = base.userInfo; ru.userInfo = base.userInfo;
ru.port = base.port; ru.port = base.port;
String cp = (child.path == null) ? "" : child.path; String cp = child.path;
if (!cp.isEmpty() && cp.charAt(0) == '/') { if (!cp.isEmpty() && cp.charAt(0) == '/') {
// 5.2 (5): Child path is absolute // 5.2 (5): Child path is absolute
ru.path = child.path; ru.path = child.path;
} else { } else {
// 5.2 (6): Resolve relative path // 5.2 (6): Resolve relative path
ru.path = resolvePath(base.path, cp, base.isAbsolute()); ru.path = resolvePath(base.path, cp);
} }
} else { } else {
ru.authority = child.authority; ru.authority = child.authority;
@ -2768,7 +2753,7 @@ public final class URI
private static void appendEncoded(CharsetEncoder encoder, StringBuilder sb, char c) { private static void appendEncoded(CharsetEncoder encoder, StringBuilder sb, char c) {
ByteBuffer bb = null; ByteBuffer bb = null;
try { try {
bb = encoder.encode(CharBuffer.wrap("" + c)); bb = encoder.encode(CharBuffer.wrap(new char[]{c}));
} catch (CharacterCodingException x) { } catch (CharacterCodingException x) {
assert false; assert false;
} }
@ -2920,7 +2905,6 @@ public final class URI
continue; continue;
} }
bb.clear(); bb.clear();
int ui = i;
for (;;) { for (;;) {
assert (n - i >= 2); assert (n - i >= 2);
bb.put(decode(s.charAt(++i), s.charAt(++i))); bb.put(decode(s.charAt(++i), s.charAt(++i)));