package okhttp3; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import o.C14953gcr; import o.C14957gcv; import o.gbB; import o.gdZ; /* loaded from: classes.dex */ public final class MediaType { private static final String QUOTED = "\"([^\"]*)\""; private static final String TOKEN = "([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)"; private final String mediaType; private final String[] parameterNamesAndValues; private final String subtype; private final String type; public static final Companion Companion = new Companion(null); private static final Pattern TYPE_SUBTYPE = Pattern.compile("([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)/([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)"); private static final Pattern PARAMETER = Pattern.compile(";\\s*(?:([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)=(?:([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)|\"([^\"]*)\"))?"); private MediaType(String str, String str2, String str3, String[] strArr) { this.mediaType = str; this.type = str2; this.subtype = str3; this.parameterNamesAndValues = strArr; } public static /* synthetic */ Charset charset$default(MediaType mediaType, Charset charset, int i, Object obj) { if ((i & 1) != 0) { charset = null; } return mediaType.charset(charset); } public final Charset charset(Charset charset) { String parameter = parameter("charset"); if (parameter == null) { return charset; } try { return Charset.forName(parameter); } catch (IllegalArgumentException unused) { return charset; } } public final String parameter(String str) { C14957gcv.e(str, ""); int i = 0; int a = gbB.a(0, this.parameterNamesAndValues.length - 1, 2); if (a < 0) { return null; } while (!gdZ.a(this.parameterNamesAndValues[i], str, true)) { if (i == a) { return null; } i += 2; } return this.parameterNamesAndValues[i + 1]; } public final boolean equals(Object obj) { return (obj instanceof MediaType) && C14957gcv.b((Object) ((MediaType) obj).mediaType, (Object) this.mediaType); } public final int hashCode() { return this.mediaType.hashCode(); } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final MediaType get(String str) { C14957gcv.e(str, ""); String str2 = str; Matcher matcher = MediaType.TYPE_SUBTYPE.matcher(str2); if (!matcher.lookingAt()) { StringBuilder sb = new StringBuilder("No subtype found for: \""); sb.append(str); sb.append('\"'); throw new IllegalArgumentException(sb.toString().toString()); } String group = matcher.group(1); C14957gcv.c((Object) group, ""); Locale locale = Locale.US; C14957gcv.c(locale, ""); String lowerCase = group.toLowerCase(locale); C14957gcv.c((Object) lowerCase, ""); String group2 = matcher.group(2); C14957gcv.c((Object) group2, ""); Locale locale2 = Locale.US; C14957gcv.c(locale2, ""); String lowerCase2 = group2.toLowerCase(locale2); C14957gcv.c((Object) lowerCase2, ""); ArrayList arrayList = new ArrayList(); Matcher matcher2 = MediaType.PARAMETER.matcher(str2); int end = matcher.end(); while (end < str.length()) { matcher2.region(end, str.length()); if (!matcher2.lookingAt()) { StringBuilder sb2 = new StringBuilder("Parameter is not formatted correctly: \""); String substring = str.substring(end); C14957gcv.c((Object) substring, ""); sb2.append(substring); sb2.append("\" for: \""); sb2.append(str); sb2.append('\"'); throw new IllegalArgumentException(sb2.toString().toString()); } String group3 = matcher2.group(1); if (group3 == null) { end = matcher2.end(); } else { String group4 = matcher2.group(2); if (group4 == null) { group4 = matcher2.group(3); } else if (gdZ.e(group4, "'", false) && gdZ.d(group4, "'", false) && group4.length() > 2) { group4 = group4.substring(1, group4.length() - 1); C14957gcv.c((Object) group4, ""); } ArrayList arrayList2 = arrayList; arrayList2.add(group3); arrayList2.add(group4); end = matcher2.end(); } } Object[] array = arrayList.toArray(new String[0]); if (array != null) { return new MediaType(str, lowerCase, lowerCase2, (String[]) array, null); } throw new NullPointerException("null cannot be cast to non-null type kotlin.Array"); } public final MediaType parse(String str) { C14957gcv.e(str, ""); try { return get(str); } catch (IllegalArgumentException unused) { return null; } } /* renamed from: -deprecated_get */ public final MediaType m433deprecated_get(String str) { C14957gcv.e(str, ""); return get(str); } /* renamed from: -deprecated_parse */ public final MediaType m434deprecated_parse(String str) { C14957gcv.e(str, ""); return parse(str); } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final String type() { return this.type; } public final String toString() { return this.mediaType; } public final String subtype() { return this.subtype; } public final Charset charset() { return charset$default(this, null, 1, null); } /* renamed from: -deprecated_type */ public final String m432deprecated_type() { return this.type; } /* renamed from: -deprecated_subtype */ public final String m431deprecated_subtype() { return this.subtype; } public static final MediaType parse(String str) { return Companion.parse(str); } public static final MediaType get(String str) { return Companion.get(str); } public /* synthetic */ MediaType(String str, String str2, String str3, String[] strArr, C14953gcr c14953gcr) { this(str, str2, str3, strArr); } }