package okhttp3; import com.facebook.internal.security.CertificateUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TreeMap; import okhttp3.internal.Util; /* loaded from: classes5.dex */ public final class Headers { public final String[] namesAndValues; public Headers(Builder builder) { List list = builder.namesAndValues; this.namesAndValues = (String[]) list.toArray(new String[list.size()]); } public Headers(String[] strArr) { this.namesAndValues = strArr; } public String get(String str) { return get(this.namesAndValues, str); } public int size() { return this.namesAndValues.length / 2; } public String name(int i) { return this.namesAndValues[i * 2]; } public String value(int i) { return this.namesAndValues[(i * 2) + 1]; } public List values(String str) { int size = size(); ArrayList arrayList = null; for (int i = 0; i < size; i++) { if (str.equalsIgnoreCase(name(i))) { if (arrayList == null) { arrayList = new ArrayList(2); } arrayList.add(value(i)); } } if (arrayList != null) { return Collections.unmodifiableList(arrayList); } return Collections.emptyList(); } public Builder newBuilder() { Builder builder = new Builder(); Collections.addAll(builder.namesAndValues, this.namesAndValues); return builder; } public boolean equals(Object obj) { return (obj instanceof Headers) && Arrays.equals(((Headers) obj).namesAndValues, this.namesAndValues); } public int hashCode() { return Arrays.hashCode(this.namesAndValues); } public String toString() { StringBuilder sb = new StringBuilder(); int size = size(); for (int i = 0; i < size; i++) { sb.append(name(i)); sb.append(": "); sb.append(value(i)); sb.append("\n"); } return sb.toString(); } public Map toMultimap() { TreeMap treeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); int size = size(); for (int i = 0; i < size; i++) { String lowerCase = name(i).toLowerCase(Locale.US); List list = (List) treeMap.get(lowerCase); if (list == null) { list = new ArrayList(2); treeMap.put(lowerCase, list); } list.add(value(i)); } return treeMap; } public static String get(String[] strArr, String str) { for (int length = strArr.length - 2; length >= 0; length -= 2) { if (str.equalsIgnoreCase(strArr[length])) { return strArr[length + 1]; } } return null; } public static Headers of(String... strArr) { if (strArr == null) { throw new NullPointerException("namesAndValues == null"); } if (strArr.length % 2 != 0) { throw new IllegalArgumentException("Expected alternating header names and values"); } String[] strArr2 = (String[]) strArr.clone(); for (int i = 0; i < strArr2.length; i++) { String str = strArr2[i]; if (str == null) { throw new IllegalArgumentException("Headers cannot be null"); } strArr2[i] = str.trim(); } for (int i2 = 0; i2 < strArr2.length; i2 += 2) { String str2 = strArr2[i2]; String str3 = strArr2[i2 + 1]; checkName(str2); checkValue(str3, str2); } return new Headers(strArr2); } public static void checkName(String str) { if (str == null) { throw new NullPointerException("name == null"); } if (str.isEmpty()) { throw new IllegalArgumentException("name is empty"); } int length = str.length(); for (int i = 0; i < length; i++) { char charAt = str.charAt(i); if (charAt <= ' ' || charAt >= 127) { throw new IllegalArgumentException(Util.format("Unexpected char %#04x at %d in header name: %s", Integer.valueOf(charAt), Integer.valueOf(i), str)); } } } public static void checkValue(String str, String str2) { if (str == null) { throw new NullPointerException("value for name " + str2 + " == null"); } int length = str.length(); for (int i = 0; i < length; i++) { char charAt = str.charAt(i); if ((charAt <= 31 && charAt != '\t') || charAt >= 127) { throw new IllegalArgumentException(Util.format("Unexpected char %#04x at %d in %s value: %s", Integer.valueOf(charAt), Integer.valueOf(i), str2, str)); } } } public static final class Builder { public final List namesAndValues = new ArrayList(20); public Builder addLenient(String str) { int indexOf = str.indexOf(CertificateUtil.DELIMITER, 1); if (indexOf != -1) { return addLenient(str.substring(0, indexOf), str.substring(indexOf + 1)); } if (str.startsWith(CertificateUtil.DELIMITER)) { return addLenient("", str.substring(1)); } return addLenient("", str); } public Builder add(String str, String str2) { Headers.checkName(str); Headers.checkValue(str2, str); return addLenient(str, str2); } public Builder addLenient(String str, String str2) { this.namesAndValues.add(str); this.namesAndValues.add(str2.trim()); return this; } public Builder removeAll(String str) { int i = 0; while (i < this.namesAndValues.size()) { if (str.equalsIgnoreCase((String) this.namesAndValues.get(i))) { this.namesAndValues.remove(i); this.namesAndValues.remove(i); i -= 2; } i += 2; } return this; } public Builder set(String str, String str2) { Headers.checkName(str); Headers.checkValue(str2, str); removeAll(str); addLenient(str, str2); return this; } public String get(String str) { for (int size = this.namesAndValues.size() - 2; size >= 0; size -= 2) { if (str.equalsIgnoreCase((String) this.namesAndValues.get(size))) { return (String) this.namesAndValues.get(size + 1); } } return null; } public Headers build() { return new Headers(this); } } }