Skip to content
Permalink
Browse files
8274949: Use String.contains() instead of String.indexOf() in java.base
Reviewed-by: weijun, dfuchs, vtewari, lancea
  • Loading branch information
turbanoff authored and Roger Riggs committed Nov 19, 2021
1 parent 09e8c8c commit 6677554374ade32c9f2ddaaa093064de8aebd831
@@ -1078,13 +1078,13 @@ private static int guessCookieVersion(String header) {
int version = 0;

header = header.toLowerCase();
if (header.indexOf("expires=") != -1) {
if (header.contains("expires=")) {
// only netscape cookie using 'expires'
version = 0;
} else if (header.indexOf("version=") != -1) {
} else if (header.contains("version=")) {
// version is mandatory for rfc 2965/2109 cookie
version = 1;
} else if (header.indexOf("max-age") != -1) {
} else if (header.contains("max-age")) {
// rfc 2965/2109 use 'max-age'
version = 1;
} else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
@@ -600,7 +600,7 @@ public String getResponseMessage() throws IOException {
public long getHeaderFieldDate(String name, long Default) {
String dateString = getHeaderField(name);
try {
if (dateString.indexOf("GMT") == -1) {
if (!dateString.contains("GMT")) {
dateString = dateString+" GMT";
}
return Date.parse(dateString);
@@ -30,12 +30,10 @@
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.net.InetAddress;
import java.security.AccessController;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.security.Security;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
@@ -333,7 +331,7 @@ private static String getHost(String host) {
ind = host.lastIndexOf(':');
host = "[" + host.substring(0, ind) + "]" +
host.substring(ind);
} else if (tokens == 8 && host.indexOf("::") == -1) {
} else if (tokens == 8 && !host.contains("::")) {
// IPv6 address only, not followed by port
host = "[" + host + "]";
} else {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, 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
@@ -485,7 +485,7 @@ private static String normalize(String path, int off) {
// Remove DotSlash(./) and resolve DotDot (..) components
private String getResolved() {
int length = path.length();
if (length == 0 || (path.indexOf("./") == -1 && path.charAt(length - 1) != '.')) {
if (length == 0 || (!path.contains("./") && path.charAt(length - 1) != '.')) {
return path;
} else {
return resolvePath();
@@ -561,11 +561,11 @@ public static void check(URL url) throws IOException {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
security.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
URL locUrl = url;
if (urlConnection instanceof JarURLConnection) {
locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
@@ -1254,7 +1254,7 @@ Resource getResource(final String name, boolean check) {
URLClassPath.check(url);

final File file;
if (name.indexOf("..") != -1) {
if (name.contains("..")) {
file = (new File(dir, name.replace('/', File.separatorChar)))
.getCanonicalFile();
if ( !((file.getPath()).startsWith(dir.getPath())) ) {
@@ -157,7 +157,7 @@ public static HttpCapture getCapture(java.net.URL url) {
if (p.matcher(s).find()) {
String f = capFiles.get(i);
File fi;
if (f.indexOf("%d") >= 0) {
if (f.contains("%d")) {
java.util.Random rand = new java.util.Random();
do {
String f2 = f.replace("%d", Integer.toString(rand.nextInt()));
@@ -645,7 +645,7 @@ private void checkURLSpoofing(HostnameVerifier hostnameVerifier)
// ignore
}

if ((cipher != null) && (cipher.indexOf("_anon_") != -1)) {
if ((cipher != null) && (cipher.contains("_anon_"))) {
return;
} else if ((hostnameVerifier != null) &&
(hostnameVerifier.verify(host, session))) {
@@ -723,7 +723,7 @@ private void addGrantEntry(PolicyParser.GrantEntry ge,
+ SELF;
}
// check for self
if (pe.name != null && pe.name.indexOf(SELF) != -1) {
if (pe.name != null && pe.name.contains(SELF)) {
// Create a "SelfPermission" , it could be an
// an unresolved permission which will be resolved
// when implies is called
@@ -59,7 +59,7 @@ public class RSAPSSSignature extends SignatureSpi {
private boolean isDigestEqual(String stdAlg, String givenAlg) {
if (stdAlg == null || givenAlg == null) return false;

if (givenAlg.indexOf("-") != -1) {
if (givenAlg.contains("-")) {
return stdAlg.equalsIgnoreCase(givenAlg);
} else {
if (stdAlg.equals("SHA-1")) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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
@@ -25,7 +25,6 @@

package sun.security.rsa;

import java.io.IOException;
import java.security.*;
import java.security.spec.*;
import sun.security.util.ObjectIdentifier;
@@ -61,9 +60,9 @@ public static KeyType lookup(String name) throws ProviderException {

// match loosely in order to work with 3rd party providers which
// may not follow the standard names
if (name.indexOf("PSS") != -1) {
if (name.contains("PSS")) {
return PSS;
} else if (name.indexOf("RSA") != -1) {
} else if (name.contains("RSA")) {
return RSA;
} else { // no match
throw new ProviderException("Unsupported algorithm " + name);
@@ -151,7 +150,7 @@ public static Object[] getTypeAndParamSpec(AlgorithmId algid)
} catch (ProviderException pe) {
// accommodate RSA keys encoded with various RSA signature oids
// for backward compatibility
if (algName.indexOf("RSA") != -1) {
if (algName.contains("RSA")) {
result[0] = KeyType.RSA;
} else {
// pass it up
@@ -1473,10 +1473,10 @@ private void doGenCert(String alias, String sigAlgName, InputStream in, PrintStr
if (s == null) break;
// OpenSSL does not use NEW
//if (s.startsWith("-----BEGIN NEW CERTIFICATE REQUEST-----")) {
if (s.startsWith("-----BEGIN") && s.indexOf("REQUEST") >= 0) {
if (s.startsWith("-----BEGIN") && s.contains("REQUEST")) {
canRead = true;
//} else if (s.startsWith("-----END NEW CERTIFICATE REQUEST-----")) {
} else if (s.startsWith("-----END") && s.indexOf("REQUEST") >= 0) {
} else if (s.startsWith("-----END") && s.contains("REQUEST")) {
break;
} else if (canRead) {
sb.append(s);
@@ -161,10 +161,10 @@ public static boolean isOn(String option)
if (args == null)
return false;
else {
if (args.indexOf("all") != -1)
if (args.contains("all"))
return true;
else
return (args.indexOf(option) != -1);
return (args.contains(option));
}
}

@@ -54,7 +54,7 @@ public class SignatureUtil {
* form of an OID, or the OID value if no match is found.
*/
private static String checkName(String algName) {
if (algName.indexOf(".") == -1) {
if (!algName.contains(".")) {
return algName;
} else {
// convert oid to String
@@ -100,7 +100,7 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
// AlgorithmParameters.getAlgorithm() may returns oid if it's
// created during DER decoding. Convert to use the standard name
// before passing it to RSAUtil
if (params.getAlgorithm().indexOf(".") != -1) {
if (params.getAlgorithm().contains(".")) {
try {
params = createAlgorithmParameters(sigName,
params.getEncoded());
@@ -109,9 +109,9 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
}
}

if (sigName.indexOf("RSA") != -1) {
if (sigName.contains("RSA")) {
paramSpec = RSAUtil.getParamSpec(params);
} else if (sigName.indexOf("ECDSA") != -1) {
} else if (sigName.contains("ECDSA")) {
try {
paramSpec = params.getParameterSpec(ECParameterSpec.class);
} catch (Exception e) {
@@ -141,11 +141,11 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,

if (paramBytes != null) {
sigName = checkName(sigName).toUpperCase(Locale.ENGLISH);
if (sigName.indexOf("RSA") != -1) {
if (sigName.contains("RSA")) {
AlgorithmParameters params =
createAlgorithmParameters(sigName, paramBytes);
paramSpec = RSAUtil.getParamSpec(params);
} else if (sigName.indexOf("ECDSA") != -1) {
} else if (sigName.contains("ECDSA")) {
try {
Provider p = Signature.getInstance(sigName).getProvider();
paramSpec = ECUtil.getECParameterSpec(p, paramBytes);
@@ -520,7 +520,7 @@ private static ObjectIdentifier algOID(String name) throws IOException {
}

// unknown algorithm oids
if (name.indexOf(".") == -1) {
if (!name.contains(".")) {
// see if there is a matching oid string alias mapping from
// 3rd party providers
name = name.toUpperCase(Locale.ENGLISH);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, 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
@@ -83,7 +83,7 @@ public static List<String> filterTags(List<LanguageRange> priorityList,
for (LanguageRange lr : priorityList) {
String range = lr.getRange();
if (range.startsWith("*-")
|| range.indexOf("-*") != -1) { // Extended range
|| range.contains("-*")) { // Extended range
if (mode == AUTOSELECT_FILTERING) {
return filterExtended(priorityList, tags);
} else if (mode == MAP_EXTENDED_RANGES) {
@@ -203,11 +203,11 @@ private JarFile getCachedJarFile(URL url) {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
sm.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
sm.checkConnect(url.getHost(), url.getPort());
} else {
throw se;
@@ -226,11 +226,11 @@ private JarFile getCachedJarFile(URL url) {
// fallback to checkRead/checkConnect for pre 1.2
// security managers
if ((perm instanceof java.io.FilePermission) &&
perm.getActions().indexOf("read") != -1) {
perm.getActions().contains("read")) {
sm.checkRead(perm.getName());
} else if ((perm instanceof
java.net.SocketPermission) &&
perm.getActions().indexOf("connect") != -1) {
perm.getActions().contains("connect")) {
sm.checkConnect(url.getHost(), url.getPort());
} else {
throw se;

1 comment on commit 6677554

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 6677554 Nov 19, 2021

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.