1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Merge pull request #1234 from square/jw/musings

Remove some needless allocations.
This commit is contained in:
Jesse Wilson
2014-12-24 22:12:59 -05:00
5 changed files with 30 additions and 22 deletions

View File

@@ -109,8 +109,8 @@ public final class ConnectionPool {
}
}
}
for (Connection expiredConnection : expiredConnections) {
Util.closeQuietly(expiredConnection.getSocket());
for (int i = 0, size = expiredConnections.size(); i < size; i++) {
Util.closeQuietly(expiredConnections.get(i).getSocket());
}
}
};

View File

@@ -149,10 +149,10 @@ public final class ConnectionSpec {
* supported by {@code sslSocket}.
*/
private ConnectionSpec supportedSpec(SSLSocket sslSocket) {
List<String> supportedCipherSuites = Util.intersect(Arrays.asList(cipherSuites),
Arrays.asList(sslSocket.getSupportedCipherSuites()));
List<String> supportedTlsVersions = Util.intersect(Arrays.asList(tlsVersions),
Arrays.asList(sslSocket.getSupportedProtocols()));
List<String> supportedCipherSuites =
Util.intersect(cipherSuites, sslSocket.getSupportedCipherSuites());
List<String> supportedTlsVersions =
Util.intersect(tlsVersions, sslSocket.getSupportedProtocols());
return new Builder(this)
.cipherSuites(supportedCipherSuites.toArray(new String[supportedCipherSuites.size()]))
.tlsVersions(supportedTlsVersions.toArray(new String[supportedTlsVersions.size()]))

View File

@@ -30,7 +30,6 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@@ -270,16 +269,19 @@ public final class Util {
}
/**
* Returns a copy of {@code a} containing only elements also in {@code b}. The returned elements
* are in the same order as in {@code a}.
* Returns a mutable copy of {@code first} containing only elements also in {@code second}. The
* returned elements are in the same order as in {@code first}.
*/
public static <T> List<T> intersect(Collection<T> a, Collection<T> b) {
public static <T> List<T> intersect(T[] first, T[] second) {
List<T> result = new ArrayList<>();
for (T t : a) {
if (b.contains(t)) {
result.add(t);
for (T a : first) {
for (T b : second) {
if (a.equals(b)) {
result.add(b);
break;
}
}
}
return Collections.unmodifiableList(result);
return result;
}
}

View File

@@ -309,7 +309,9 @@ public final class RouteSelector {
/** Prepares the connection specs to attempt. */
private void resetConnectionSpecs() {
connectionSpecs = new ArrayList<>();
for (ConnectionSpec spec : address.getConnectionSpecs()) {
List<ConnectionSpec> specs = address.getConnectionSpecs();
for (int i = 0, size = specs.size(); i < size; i++) {
ConnectionSpec spec = specs.get(i);
if (request.isHttps() == spec.isTls()) {
connectionSpecs.add(spec);
}

View File

@@ -82,8 +82,9 @@ public final class OkHostnameVerifier implements HostnameVerifier {
* Returns true if {@code certificate} matches {@code ipAddress}.
*/
private boolean verifyIpAddress(String ipAddress, X509Certificate certificate) {
for (String altName : getSubjectAltNames(certificate, ALT_IPA_NAME)) {
if (ipAddress.equalsIgnoreCase(altName)) {
List<String> altNames = getSubjectAltNames(certificate, ALT_IPA_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
if (ipAddress.equalsIgnoreCase(altNames.get(i))) {
return true;
}
}
@@ -96,9 +97,10 @@ public final class OkHostnameVerifier implements HostnameVerifier {
private boolean verifyHostName(String hostName, X509Certificate certificate) {
hostName = hostName.toLowerCase(Locale.US);
boolean hasDns = false;
for (String altName : getSubjectAltNames(certificate, ALT_DNS_NAME)) {
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
hasDns = true;
if (verifyHostName(hostName, altName)) {
if (verifyHostName(hostName, altNames.get(i))) {
return true;
}
}
@@ -116,9 +118,11 @@ public final class OkHostnameVerifier implements HostnameVerifier {
}
public static List<String> allSubjectAltNames(X509Certificate certificate) {
List<String> result = new ArrayList<>();
result.addAll(getSubjectAltNames(certificate, ALT_IPA_NAME));
result.addAll(getSubjectAltNames(certificate, ALT_DNS_NAME));
List<String> altIpaNames = getSubjectAltNames(certificate, ALT_IPA_NAME);
List<String> altDnsNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
List<String> result = new ArrayList<>(altIpaNames.size() + altDnsNames.size());
result.addAll(altIpaNames);
result.addAll(altDnsNames);
return result;
}