package okhttp3.repackaged; import com.google.common.net.HttpHeaders; import java.io.Closeable; import java.io.File; import java.io.Flushable; import java.io.IOException; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import o.C15145giV; import o.C15180gja; import o.InterfaceC15142giR; import o.giM; import o.giO; import o.giP; import o.giQ; import o.giS; import o.giX; import o.giZ; import okhttp3.repackaged.Headers; import okhttp3.repackaged.Request; import okhttp3.repackaged.Response; import okhttp3.repackaged.internal.DiskLruCache; import okhttp3.repackaged.internal.InternalCache; import okhttp3.repackaged.internal.Util; import okhttp3.repackaged.internal.http.CacheRequest; import okhttp3.repackaged.internal.http.CacheStrategy; import okhttp3.repackaged.internal.http.HttpMethod; import okhttp3.repackaged.internal.http.OkHeaders; import okhttp3.repackaged.internal.http.StatusLine; import okhttp3.repackaged.internal.io.FileSystem; /* loaded from: classes6.dex */ public final class Cache implements Closeable, Flushable { private static final int agA = 201105; private static final int agB = 0; private static final int agC = 1; private static final int agD = 2; final InternalCache agE; private final DiskLruCache agF; private int agG; private int agH; private int agI; private int writeAbortCount; private int writeSuccessCount; static /* synthetic */ int c(Cache cache) { int i = cache.writeSuccessCount; cache.writeSuccessCount = i + 1; return i; } static /* synthetic */ int d(Cache cache) { int i = cache.writeAbortCount; cache.writeAbortCount = i + 1; return i; } public Cache(File file, long j) { this(file, j, FileSystem.SYSTEM); } Cache(File file, long j, FileSystem fileSystem) { this.agE = new InternalCache(this) { // from class: okhttp3.repackaged.Cache.1 final Cache agJ; { this.agJ = this; } @Override // okhttp3.repackaged.internal.InternalCache public Response get(Request request) throws IOException { return this.agJ.get(request); } @Override // okhttp3.repackaged.internal.InternalCache public CacheRequest put(Response response) throws IOException { return this.agJ.put(response); } @Override // okhttp3.repackaged.internal.InternalCache public void remove(Request request) throws IOException { this.agJ.remove(request); } @Override // okhttp3.repackaged.internal.InternalCache public void update(Response response, Response response2) throws IOException { this.agJ.update(response, response2); } @Override // okhttp3.repackaged.internal.InternalCache public void trackConditionalCacheHit() { this.agJ.trackConditionalCacheHit(); } @Override // okhttp3.repackaged.internal.InternalCache public void trackResponse(CacheStrategy cacheStrategy) { this.agJ.trackResponse(cacheStrategy); } }; this.agF = DiskLruCache.create(fileSystem, file, agA, 2, j); } private static String a(Request request) { return Util.md5Hex(request.url().toString()); } final Response get(Request request) { try { DiskLruCache.Snapshot snapshot = this.agF.get(a(request)); if (snapshot == null) { return null; } try { c cVar = new c(snapshot.getSource(0)); Response a2 = cVar.a(snapshot); if (cVar.a(request, a2)) { return a2; } Util.closeQuietly(a2.body()); return null; } catch (IOException unused) { Util.closeQuietly(snapshot); return null; } } catch (IOException unused2) { } } /* JADX INFO: Access modifiers changed from: private */ public CacheRequest put(Response response) throws IOException { DiskLruCache.Editor editor; String method = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException unused) { } return null; } if (!method.equals("GET") || OkHeaders.hasVaryAll(response)) { return null; } c cVar = new c(response); try { editor = this.agF.edit(a(response.request())); if (editor == null) { return null; } try { cVar.b(editor); return new a(this, editor); } catch (IOException unused2) { a(editor); return null; } } catch (IOException unused3) { editor = null; } } /* JADX INFO: Access modifiers changed from: private */ public void remove(Request request) throws IOException { this.agF.remove(a(request)); } /* JADX INFO: Access modifiers changed from: private */ public void update(Response response, Response response2) { DiskLruCache.Editor editor; c cVar = new c(response2); try { editor = ((b) response.body()).agQ.edit(); if (editor != null) { try { cVar.b(editor); editor.commit(); } catch (IOException unused) { a(editor); } } } catch (IOException unused2) { editor = null; } } private void a(DiskLruCache.Editor editor) { if (editor != null) { try { editor.abort(); } catch (IOException unused) { } } } public final void initialize() throws IOException { this.agF.initialize(); } public final void delete() throws IOException { this.agF.delete(); } public final void evictAll() throws IOException { this.agF.evictAll(); } public final Iterator urls() throws IOException { return new Iterator(this) { // from class: okhttp3.repackaged.Cache.2 final Cache agJ; boolean canRemove; final Iterator delegate; String nextUrl; { this.agJ = this; this.delegate = this.agF.snapshots(); } @Override // java.util.Iterator public boolean hasNext() { if (this.nextUrl != null) { return true; } this.canRemove = false; while (this.delegate.hasNext()) { DiskLruCache.Snapshot next = this.delegate.next(); try { this.nextUrl = C15145giV.a(next.getSource(0)).p(); return true; } catch (IOException unused) { } finally { next.close(); } } return false; } @Override // java.util.Iterator public String next() { if (!hasNext()) { throw new NoSuchElementException(); } String str = this.nextUrl; this.nextUrl = null; this.canRemove = true; return str; } @Override // java.util.Iterator public void remove() { if (!this.canRemove) { throw new IllegalStateException("remove() before next()"); } this.delegate.remove(); } }; } public final int writeAbortCount() { int i; synchronized (this) { i = this.writeAbortCount; } return i; } public final int writeSuccessCount() { int i; synchronized (this) { i = this.writeSuccessCount; } return i; } public final long size() throws IOException { return this.agF.size(); } public final long maxSize() { return this.agF.getMaxSize(); } @Override // java.io.Flushable public final void flush() throws IOException { this.agF.flush(); } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { this.agF.close(); } public final File directory() { return this.agF.getDirectory(); } public final boolean isClosed() { return this.agF.isClosed(); } /* JADX INFO: Access modifiers changed from: private */ public void trackResponse(CacheStrategy cacheStrategy) { synchronized (this) { this.agI++; if (cacheStrategy.networkRequest != null) { this.agG++; } else if (cacheStrategy.cacheResponse != null) { this.agH++; } } } /* JADX INFO: Access modifiers changed from: private */ public void trackConditionalCacheHit() { synchronized (this) { this.agH++; } } public final int networkCount() { int i; synchronized (this) { i = this.agG; } return i; } public final int hitCount() { int i; synchronized (this) { i = this.agH; } return i; } public final int requestCount() { int i; synchronized (this) { i = this.agI; } return i; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class a implements CacheRequest { final Cache agJ; private final DiskLruCache.Editor agK; private giX agL; private giX agM; private boolean done; public a(Cache cache, DiskLruCache.Editor editor) throws IOException { this.agJ = cache; this.agK = editor; giX newSink = editor.newSink(1); this.agL = newSink; this.agM = new giQ(this, newSink, cache, editor) { // from class: okhttp3.repackaged.Cache.a.1 final Cache agN; final DiskLruCache.Editor agO; final a agP; { this.agP = this; this.agN = cache; this.agO = editor; } @Override // o.giQ, o.giX, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (this.agP.agJ) { if (this.agP.done) { return; } this.agP.done = true; Cache.c(this.agP.agJ); super.close(); this.agO.commit(); } } }; } @Override // okhttp3.repackaged.internal.http.CacheRequest public final void abort() { synchronized (this.agJ) { if (this.done) { return; } this.done = true; Cache.d(this.agJ); Util.closeQuietly(this.agL); try { this.agK.abort(); } catch (IOException unused) { } } } @Override // okhttp3.repackaged.internal.http.CacheRequest public final giX body() { return this.agM; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class c { private final Headers agW; private final String agX; private final Headers agY; private final Handshake agZ; private final int code; private final String message; private final Protocol protocol; private final String url; public c(giZ giz) throws IOException { try { InterfaceC15142giR a = C15145giV.a(giz); this.url = a.p(); this.agX = a.p(); Headers.Builder builder = new Headers.Builder(); int a2 = Cache.a(a); for (int i = 0; i < a2; i++) { builder.aT(a.p()); } this.agW = builder.build(); StatusLine parse = StatusLine.parse(a.p()); this.protocol = parse.protocol; this.code = parse.code; this.message = parse.message; Headers.Builder builder2 = new Headers.Builder(); int a3 = Cache.a(a); for (int i2 = 0; i2 < a3; i2++) { builder2.aT(a.p()); } this.agY = builder2.build(); if (isHttps()) { String p = a.p(); if (p.length() > 0) { StringBuilder sb = new StringBuilder(); sb.append("expected \"\" but was \""); sb.append(p); sb.append("\""); throw new IOException(sb.toString()); } this.agZ = Handshake.get(a.a() ? null : TlsVersion.forJavaName(a.p()), CipherSuite.forJavaName(a.p()), c(a), c(a)); } else { this.agZ = null; } } finally { giz.close(); } } public c(Response response) { this.url = response.request().url().toString(); this.agW = OkHeaders.varyHeaders(response); this.agX = response.request().method(); this.protocol = response.protocol(); this.code = response.code(); this.message = response.message(); this.agY = response.headers(); this.agZ = response.handshake(); } public final void b(DiskLruCache.Editor editor) throws IOException { giO e = C15145giV.e(editor.newSink(0)); e.c(this.url); e.b(10); e.c(this.agX); e.b(10); e.i(this.agW.size()); e.b(10); int size = this.agW.size(); for (int i = 0; i < size; i++) { e.c(this.agW.name(i)); e.c(": "); e.c(this.agW.value(i)); e.b(10); } e.c(new StatusLine(this.protocol, this.code, this.message).toString()); e.b(10); e.i(this.agY.size()); e.b(10); int size2 = this.agY.size(); for (int i2 = 0; i2 < size2; i2++) { e.c(this.agY.name(i2)); e.c(": "); e.c(this.agY.value(i2)); e.b(10); } if (isHttps()) { e.b(10); e.c(this.agZ.cipherSuite().javaName()); e.b(10); a(e, this.agZ.peerCertificates()); a(e, this.agZ.localCertificates()); if (this.agZ.tlsVersion() != null) { e.c(this.agZ.tlsVersion().javaName()); e.b(10); } } e.close(); } private boolean isHttps() { return this.url.startsWith("https://"); } private List c(InterfaceC15142giR interfaceC15142giR) throws IOException { int a = Cache.a(interfaceC15142giR); if (a == -1) { return Collections.emptyList(); } try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); ArrayList arrayList = new ArrayList(a); for (int i = 0; i < a; i++) { String p = interfaceC15142giR.p(); giM gim = new giM(); giP d = giP.d(p); if (d == null) { throw new IllegalArgumentException("byteString == null"); } byte[] bArr = d.d; gim.a(bArr, 0, bArr.length); arrayList.add(certificateFactory.generateCertificate(new giM.AnonymousClass3(gim))); } return arrayList; } catch (CertificateException e) { throw new IOException(e.getMessage()); } } private void a(giO gio, List list) throws IOException { try { gio.i(list.size()); gio.b(10); int size = list.size(); for (int i = 0; i < size; i++) { gio.c(C15180gja.c(giP.c(list.get(i).getEncoded()).d)); gio.b(10); } } catch (CertificateEncodingException e) { throw new IOException(e.getMessage()); } } public final boolean a(Request request, Response response) { return this.url.equals(request.url().toString()) && this.agX.equals(request.method()) && OkHeaders.varyMatches(response, this.agW, request); } public final Response a(DiskLruCache.Snapshot snapshot) { String str = this.agY.get(HttpHeaders.CONTENT_TYPE); String str2 = this.agY.get(HttpHeaders.CONTENT_LENGTH); return new Response.Builder().request(new Request.Builder().url(this.url).method(this.agX, null).headers(this.agW).build()).protocol(this.protocol).code(this.code).message(this.message).headers(this.agY).body(new b(snapshot, str, str2)).handshake(this.agZ).build(); } } /* JADX INFO: Access modifiers changed from: private */ public static int a(InterfaceC15142giR interfaceC15142giR) throws IOException { try { long f = interfaceC15142giR.f(); String p = interfaceC15142giR.p(); if (f >= 0 && f <= 2147483647L && p.isEmpty()) { return (int) f; } StringBuilder sb = new StringBuilder("expected an int but was \""); sb.append(f); sb.append(p); sb.append("\""); throw new IOException(sb.toString()); } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static class b extends ResponseBody { private final DiskLruCache.Snapshot agQ; private final InterfaceC15142giR agR; private final String agS; private final String agT; public b(DiskLruCache.Snapshot snapshot, String str, String str2) { this.agQ = snapshot; this.agS = str; this.agT = str2; this.agR = C15145giV.a(new giS(this, snapshot.getSource(1), snapshot) { // from class: okhttp3.repackaged.Cache.b.1 final DiskLruCache.Snapshot agU; final b agV; { this.agV = this; this.agU = snapshot; } @Override // o.giS, o.giZ, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.agU.close(); super.close(); } }); } @Override // okhttp3.repackaged.ResponseBody public MediaType contentType() { String str = this.agS; if (str != null) { return MediaType.parse(str); } return null; } @Override // okhttp3.repackaged.ResponseBody public long contentLength() { try { String str = this.agT; if (str != null) { return Long.parseLong(str); } return -1L; } catch (NumberFormatException unused) { return -1L; } } @Override // okhttp3.repackaged.ResponseBody public InterfaceC15142giR source() { return this.agR; } } }