package okhttp3.internal.cache2;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import kotlin.jvm.internal.g;
import kotlin.jvm.internal.l;
import n1.s;
import okhttp3.internal.Util;
import okio.C0688f;
import okio.E;
import okio.F;
import okio.i;

/* JADX INFO: loaded from: classes2.dex */
public final class Relay {
    public static final Companion Companion = new Companion(null);
    private static final long FILE_HEADER_SIZE = 32;
    public static final i PREFIX_CLEAN;
    public static final i PREFIX_DIRTY;
    private static final int SOURCE_FILE = 2;
    private static final int SOURCE_UPSTREAM = 1;
    private final C0688f buffer;
    private final long bufferMaxSize;
    private boolean complete;
    private RandomAccessFile file;
    private final i metadata;
    private int sourceCount;
    private E upstream;
    private final C0688f upstreamBuffer;
    private long upstreamPos;
    private Thread upstreamReader;

    public static final class Companion {
        private Companion() {
        }

        public final Relay edit(File file, E upstream, i metadata, long j2) throws IOException {
            l.e(file, "file");
            l.e(upstream, "upstream");
            l.e(metadata, "metadata");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            Relay relay = new Relay(randomAccessFile, upstream, 0L, metadata, j2, null);
            randomAccessFile.setLength(0L);
            relay.writeHeader(Relay.PREFIX_DIRTY, -1L, -1L);
            return relay;
        }

        public final Relay read(File file) throws IOException {
            l.e(file, "file");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            FileChannel channel = randomAccessFile.getChannel();
            l.d(channel, "randomAccessFile.channel");
            FileOperator fileOperator = new FileOperator(channel);
            C0688f c0688f = new C0688f();
            fileOperator.read(0L, c0688f, 32L);
            if (!l.a(c0688f.c(r1.s()), Relay.PREFIX_CLEAN)) {
                throw new IOException("unreadable cache file");
            }
            long j2 = c0688f.readLong();
            long j3 = c0688f.readLong();
            C0688f c0688f2 = new C0688f();
            fileOperator.read(j2 + 32, c0688f2, j3);
            return new Relay(randomAccessFile, null, j2, c0688f2.B(), 0L, null);
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    public final class RelaySource implements E {
        private FileOperator fileOperator;
        private long sourcePos;
        private final F timeout = new F();

        public RelaySource() {
            RandomAccessFile file = Relay.this.getFile();
            l.b(file);
            FileChannel channel = file.getChannel();
            l.d(channel, "file!!.channel");
            this.fileOperator = new FileOperator(channel);
        }

        @Override // okio.E, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.fileOperator == null) {
                return;
            }
            RandomAccessFile randomAccessFile = null;
            this.fileOperator = null;
            synchronized (Relay.this) {
                try {
                    Relay.this.setSourceCount(r2.getSourceCount() - 1);
                    if (Relay.this.getSourceCount() == 0) {
                        RandomAccessFile file = Relay.this.getFile();
                        Relay.this.setFile(null);
                        randomAccessFile = file;
                    }
                    s sVar = s.f5871a;
                } catch (Throwable th) {
                    throw th;
                }
            }
            if (randomAccessFile != null) {
                Util.closeQuietly(randomAccessFile);
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:29:0x008e, code lost:
        
            if (r4 != 2) goto L32;
         */
        /* JADX WARN: Code restructure failed: missing block: B:30:0x0090, code lost:
        
            r8 = java.lang.Math.min(r21, r19.this$0.getUpstreamPos() - r19.sourcePos);
            r2 = r19.fileOperator;
            kotlin.jvm.internal.l.b(r2);
            r2.read(r19.sourcePos + 32, r20, r8);
            r19.sourcePos += r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:31:0x00b0, code lost:
        
            return r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:33:0x00b2, code lost:
        
            r0 = r19.this$0.getUpstream();
            kotlin.jvm.internal.l.b(r0);
            r14 = r0.read(r19.this$0.getUpstreamBuffer(), r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:34:0x00cd, code lost:
        
            if (r14 != (-1)) goto L51;
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x00cf, code lost:
        
            r0 = r19.this$0;
            r0.commit(r0.getUpstreamPos());
         */
        /* JADX WARN: Code restructure failed: missing block: B:36:0x00d8, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:37:0x00da, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:38:0x00db, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:39:0x00e2, code lost:
        
            if (r0 == null) goto L45;
         */
        /* JADX WARN: Code restructure failed: missing block: B:40:0x00e4, code lost:
        
            r0.notifyAll();
            r0 = n1.s.f5871a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:41:0x00e9, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:42:0x00ea, code lost:
        
            return -1;
         */
        /* JADX WARN: Code restructure failed: missing block: B:46:0x00f4, code lost:
        
            throw new java.lang.NullPointerException("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:49:0x00f7, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:51:0x00fa, code lost:
        
            r9 = java.lang.Math.min(r14, r21);
            r19.this$0.getUpstreamBuffer().f(r20, 0, r9);
            r19.sourcePos += r9;
            r13 = r19.fileOperator;
            kotlin.jvm.internal.l.b(r13);
            r13.write(r19.this$0.getUpstreamPos() + 32, r19.this$0.getUpstreamBuffer().clone(), r14);
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:52:0x0131, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:53:0x0132, code lost:
        
            r19.this$0.getBuffer().write(r19.this$0.getUpstreamBuffer(), r14);
         */
        /* JADX WARN: Code restructure failed: missing block: B:54:0x0153, code lost:
        
            if (r19.this$0.getBuffer().G() <= r19.this$0.getBufferMaxSize()) goto L58;
         */
        /* JADX WARN: Code restructure failed: missing block: B:55:0x0155, code lost:
        
            r19.this$0.getBuffer().skip(r19.this$0.getBuffer().G() - r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:56:0x0170, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:58:0x0172, code lost:
        
            r0 = r19.this$0;
            r0.setUpstreamPos(r0.getUpstreamPos() + r14);
            r0 = n1.s.f5871a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:59:0x017e, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:60:0x017f, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:61:0x0181, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:62:0x0182, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:63:0x0189, code lost:
        
            if (r0 == null) goto L69;
         */
        /* JADX WARN: Code restructure failed: missing block: B:64:0x018b, code lost:
        
            r0.notifyAll();
         */
        /* JADX WARN: Code restructure failed: missing block: B:65:0x018e, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:66:0x018f, code lost:
        
            return r9;
         */
        /* JADX WARN: Code restructure failed: missing block: B:70:0x0199, code lost:
        
            throw new java.lang.NullPointerException("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:74:0x019d, code lost:
        
            throw r0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:76:0x01a0, code lost:
        
            monitor-enter(r19.this$0);
         */
        /* JADX WARN: Code restructure failed: missing block: B:77:0x01a1, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r3 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:78:0x01a8, code lost:
        
            if (r3 == null) goto L79;
         */
        /* JADX WARN: Code restructure failed: missing block: B:80:0x01b1, code lost:
        
            throw new java.lang.NullPointerException("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:81:0x01b2, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:83:0x01b4, code lost:
        
            r3.notifyAll();
            r3 = n1.s.f5871a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:85:0x01ba, code lost:
        
            throw r0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:87:0x01bc, code lost:
        
            throw r0;
         */
        @Override // okio.E
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public long read(okio.C0688f r20, long r21) throws java.io.IOException {
            /*
                Method dump skipped, instruction units count: 455
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.cache2.Relay.RelaySource.read(okio.f, long):long");
        }

        @Override // okio.E
        public F timeout() {
            return this.timeout;
        }
    }

    static {
        i.a aVar = i.f6032y;
        PREFIX_CLEAN = aVar.d("OkHttp cache v1\n");
        PREFIX_DIRTY = aVar.d("OkHttp DIRTY :(\n");
    }

    private Relay(RandomAccessFile randomAccessFile, E e2, long j2, i iVar, long j3) {
        this.file = randomAccessFile;
        this.upstream = e2;
        this.upstreamPos = j2;
        this.metadata = iVar;
        this.bufferMaxSize = j3;
        this.upstreamBuffer = new C0688f();
        this.complete = this.upstream == null;
        this.buffer = new C0688f();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void writeHeader(i iVar, long j2, long j3) throws IOException {
        C0688f c0688f = new C0688f();
        c0688f.write(iVar);
        c0688f.V(j2);
        c0688f.V(j3);
        if (!(c0688f.G() == 32)) {
            throw new IllegalArgumentException("Failed requirement.");
        }
        RandomAccessFile randomAccessFile = this.file;
        l.b(randomAccessFile);
        FileChannel channel = randomAccessFile.getChannel();
        l.d(channel, "file!!.channel");
        new FileOperator(channel).write(0L, c0688f, 32L);
    }

    private final void writeMetadata(long j2) throws IOException {
        C0688f c0688f = new C0688f();
        c0688f.write(this.metadata);
        RandomAccessFile randomAccessFile = this.file;
        l.b(randomAccessFile);
        FileChannel channel = randomAccessFile.getChannel();
        l.d(channel, "file!!.channel");
        new FileOperator(channel).write(32 + j2, c0688f, this.metadata.s());
    }

    public final void commit(long j2) throws IOException {
        writeMetadata(j2);
        RandomAccessFile randomAccessFile = this.file;
        l.b(randomAccessFile);
        randomAccessFile.getChannel().force(false);
        writeHeader(PREFIX_CLEAN, j2, this.metadata.s());
        RandomAccessFile randomAccessFile2 = this.file;
        l.b(randomAccessFile2);
        randomAccessFile2.getChannel().force(false);
        synchronized (this) {
            this.complete = true;
            s sVar = s.f5871a;
        }
        E e2 = this.upstream;
        if (e2 != null) {
            Util.closeQuietly(e2);
        }
        this.upstream = null;
    }

    public final C0688f getBuffer() {
        return this.buffer;
    }

    public final long getBufferMaxSize() {
        return this.bufferMaxSize;
    }

    public final boolean getComplete() {
        return this.complete;
    }

    public final RandomAccessFile getFile() {
        return this.file;
    }

    public final int getSourceCount() {
        return this.sourceCount;
    }

    public final E getUpstream() {
        return this.upstream;
    }

    public final C0688f getUpstreamBuffer() {
        return this.upstreamBuffer;
    }

    public final long getUpstreamPos() {
        return this.upstreamPos;
    }

    public final Thread getUpstreamReader() {
        return this.upstreamReader;
    }

    public final boolean isClosed() {
        return this.file == null;
    }

    public final i metadata() {
        return this.metadata;
    }

    public final E newSource() {
        synchronized (this) {
            if (this.file == null) {
                return null;
            }
            this.sourceCount++;
            return new RelaySource();
        }
    }

    public final void setComplete(boolean z2) {
        this.complete = z2;
    }

    public final void setFile(RandomAccessFile randomAccessFile) {
        this.file = randomAccessFile;
    }

    public final void setSourceCount(int i2) {
        this.sourceCount = i2;
    }

    public final void setUpstream(E e2) {
        this.upstream = e2;
    }

    public final void setUpstreamPos(long j2) {
        this.upstreamPos = j2;
    }

    public final void setUpstreamReader(Thread thread) {
        this.upstreamReader = thread;
    }

    public /* synthetic */ Relay(RandomAccessFile randomAccessFile, E e2, long j2, i iVar, long j3, g gVar) {
        this(randomAccessFile, e2, j2, iVar, j3);
    }
}
