package okhttp3.internal.cache2;

import c.q.d.g;
import c.q.d.j;
import d.a0;
import d.b0;
import d.f;
import d.i;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import okhttp3.internal.Util;

/* JADX INFO: compiled from: Relay.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Relay {
    private static final long FILE_HEADER_SIZE = 32;
    private static final int SOURCE_FILE = 2;
    private static final int SOURCE_UPSTREAM = 1;
    private final f buffer;
    private final long bufferMaxSize;
    private boolean complete;
    private RandomAccessFile file;
    private final i metadata;
    private int sourceCount;
    private a0 upstream;
    private final f upstreamBuffer;
    private long upstreamPos;
    private Thread upstreamReader;
    public static final Companion Companion = new Companion(null);
    public static final i PREFIX_CLEAN = i.f1573f.d("OkHttp cache v1\n");
    public static final i PREFIX_DIRTY = i.f1573f.d("OkHttp DIRTY :(\n");

    /* JADX INFO: compiled from: Relay.kt */
    public static final class Companion {
        private Companion() {
        }

        public final Relay edit(File file, a0 a0Var, i iVar, long j) throws IOException {
            j.c(file, "file");
            j.c(a0Var, "upstream");
            j.c(iVar, "metadata");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            Relay relay = new Relay(randomAccessFile, a0Var, 0L, iVar, j, null);
            randomAccessFile.setLength(0L);
            relay.writeHeader(Relay.PREFIX_DIRTY, -1L, -1L);
            return relay;
        }

        public final Relay read(File file) throws IOException {
            j.c(file, "file");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            FileChannel channel = randomAccessFile.getChannel();
            j.b(channel, "randomAccessFile.channel");
            FileOperator fileOperator = new FileOperator(channel);
            f fVar = new f();
            fileOperator.read(0L, fVar, Relay.FILE_HEADER_SIZE);
            if (!j.a(fVar.j(Relay.PREFIX_CLEAN.s()), Relay.PREFIX_CLEAN)) {
                throw new IOException("unreadable cache file");
            }
            long j = fVar.readLong();
            long j2 = fVar.readLong();
            f fVar2 = new f();
            fileOperator.read(j + Relay.FILE_HEADER_SIZE, fVar2, j2);
            return new Relay(randomAccessFile, null, j, fVar2.R(), 0L, null);
        }

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

    /* JADX INFO: compiled from: Relay.kt */
    public final class RelaySource implements a0 {
        private FileOperator fileOperator;
        private long sourcePos;
        private final b0 timeout = new b0();

        public RelaySource() {
            RandomAccessFile file = Relay.this.getFile();
            if (file == null) {
                j.g();
                throw null;
            }
            FileChannel channel = file.getChannel();
            j.b(channel, "file!!.channel");
            this.fileOperator = new FileOperator(channel);
        }

        @Override // d.a0, 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) {
                Relay.this.setSourceCount(r2.getSourceCount() - 1);
                if (Relay.this.getSourceCount() == 0) {
                    RandomAccessFile file = Relay.this.getFile();
                    Relay.this.setFile(null);
                    randomAccessFile = file;
                }
                c.j jVar = c.j.f1417a;
            }
            if (randomAccessFile != null) {
                Util.closeQuietly(randomAccessFile);
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:27:0x008c, code lost:
        
            if (r4 != 2) goto L98;
         */
        /* JADX WARN: Code restructure failed: missing block: B:28:0x008e, code lost:
        
            r9 = java.lang.Math.min(r21, r19.this$0.getUpstreamPos() - r19.sourcePos);
            r2 = r19.fileOperator;
         */
        /* JADX WARN: Code restructure failed: missing block: B:29:0x009d, code lost:
        
            if (r2 == null) goto L32;
         */
        /* JADX WARN: Code restructure failed: missing block: B:30:0x009f, code lost:
        
            r2.read(r19.sourcePos + okhttp3.internal.cache2.Relay.FILE_HEADER_SIZE, r20, r9);
            r19.sourcePos += r9;
         */
        /* JADX WARN: Code restructure failed: missing block: B:31:0x00ad, code lost:
        
            return r9;
         */
        /* JADX WARN: Code restructure failed: missing block: B:32:0x00ae, code lost:
        
            c.q.d.j.g();
         */
        /* JADX WARN: Code restructure failed: missing block: B:33:0x00b1, code lost:
        
            throw null;
         */
        /* JADX WARN: Code restructure failed: missing block: B:34:0x00b2, code lost:
        
            r0 = r19.this$0.getUpstream();
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x00b8, code lost:
        
            if (r0 == null) goto L77;
         */
        /* JADX WARN: Code restructure failed: missing block: B:36:0x00ba, code lost:
        
            r14 = r0.read(r19.this$0.getUpstreamBuffer(), r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:37:0x00cc, code lost:
        
            if (r14 != (-1)) goto L51;
         */
        /* JADX WARN: Code restructure failed: missing block: B:38:0x00ce, code lost:
        
            r19.this$0.commit(r19.this$0.getUpstreamPos());
         */
        /* JADX WARN: Code restructure failed: missing block: B:39:0x00d9, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:40:0x00db, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:41:0x00dc, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:42:0x00e3, code lost:
        
            if (r0 == null) goto L46;
         */
        /* JADX WARN: Code restructure failed: missing block: B:43:0x00e5, code lost:
        
            r0.notifyAll();
            r0 = c.j.f1417a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:44:0x00ea, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:45:0x00eb, code lost:
        
            return -1;
         */
        /* JADX WARN: Code restructure failed: missing block: B:47:0x00f3, code lost:
        
            throw new c.h("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:51:0x00f7, code lost:
        
            r9 = java.lang.Math.min(r14, r21);
            r19.this$0.getUpstreamBuffer().H(r20, 0, r9);
            r19.sourcePos += r9;
            r13 = r19.fileOperator;
         */
        /* JADX WARN: Code restructure failed: missing block: B:52:0x0111, code lost:
        
            if (r13 == null) goto L75;
         */
        /* JADX WARN: Code restructure failed: missing block: B:53:0x0113, code lost:
        
            r13.write(r19.this$0.getUpstreamPos() + okhttp3.internal.cache2.Relay.FILE_HEADER_SIZE, r19.this$0.getUpstreamBuffer().clone(), r14);
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:54:0x012d, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:55:0x012e, code lost:
        
            r19.this$0.getBuffer().write(r19.this$0.getUpstreamBuffer(), r14);
         */
        /* JADX WARN: Code restructure failed: missing block: B:56:0x014f, code lost:
        
            if (r19.this$0.getBuffer().b0() <= r19.this$0.getBufferMaxSize()) goto L58;
         */
        /* JADX WARN: Code restructure failed: missing block: B:57:0x0151, code lost:
        
            r19.this$0.getBuffer().l(r19.this$0.getBuffer().b0() - r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:58:0x016b, code lost:
        
            r0 = r19.this$0;
            r0.setUpstreamPos(r0.getUpstreamPos() + r14);
            r0 = c.j.f1417a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:59:0x0177, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:60:0x0178, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:61:0x017a, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:62:0x017b, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:63:0x0182, code lost:
        
            if (r0 == null) goto L67;
         */
        /* JADX WARN: Code restructure failed: missing block: B:64:0x0184, code lost:
        
            r0.notifyAll();
            r0 = c.j.f1417a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:65:0x0189, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:66:0x018a, code lost:
        
            return r9;
         */
        /* JADX WARN: Code restructure failed: missing block: B:68:0x0192, code lost:
        
            throw new c.h("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:75:0x0199, code lost:
        
            c.q.d.j.g();
         */
        /* JADX WARN: Code restructure failed: missing block: B:76:0x019c, code lost:
        
            throw null;
         */
        /* JADX WARN: Code restructure failed: missing block: B:77:0x019d, code lost:
        
            c.q.d.j.g();
         */
        /* JADX WARN: Code restructure failed: missing block: B:78:0x01a0, code lost:
        
            throw null;
         */
        /* JADX WARN: Code restructure failed: missing block: B:79:0x01a1, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:81:0x01a4, code lost:
        
            monitor-enter(r19.this$0);
         */
        /* JADX WARN: Code restructure failed: missing block: B:82:0x01a5, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r3 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:83:0x01ac, code lost:
        
            if (r3 == null) goto L84;
         */
        /* JADX WARN: Code restructure failed: missing block: B:85:0x01b5, code lost:
        
            throw new c.h("null cannot be cast to non-null type java.lang.Object");
         */
        /* JADX WARN: Code restructure failed: missing block: B:86:0x01b6, code lost:
        
            r3.notifyAll();
            r3 = c.j.f1417a;
         */
        /* JADX WARN: Code restructure failed: missing block: B:88:0x01bc, code lost:
        
            throw r0;
         */
        @Override // d.a0
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public long read(d.f r20, long r21) throws java.io.IOException {
            /*
                Method dump skipped, instruction units count: 463
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.cache2.Relay.RelaySource.read(d.f, long):long");
        }

        @Override // d.a0
        public b0 timeout() {
            return this.timeout;
        }
    }

    private Relay(RandomAccessFile randomAccessFile, a0 a0Var, long j, i iVar, long j2) {
        this.file = randomAccessFile;
        this.upstream = a0Var;
        this.upstreamPos = j;
        this.metadata = iVar;
        this.bufferMaxSize = j2;
        this.upstreamBuffer = new f();
        this.complete = this.upstream == null;
        this.buffer = new f();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void writeHeader(i iVar, long j, long j2) throws IOException {
        f fVar = new f();
        fVar.f0(iVar);
        fVar.n0(j);
        fVar.n0(j2);
        if (!(fVar.b0() == FILE_HEADER_SIZE)) {
            throw new IllegalArgumentException("Failed requirement.".toString());
        }
        RandomAccessFile randomAccessFile = this.file;
        if (randomAccessFile == null) {
            j.g();
            throw null;
        }
        FileChannel channel = randomAccessFile.getChannel();
        j.b(channel, "file!!.channel");
        new FileOperator(channel).write(0L, fVar, FILE_HEADER_SIZE);
    }

    private final void writeMetadata(long j) throws IOException {
        f fVar = new f();
        fVar.f0(this.metadata);
        RandomAccessFile randomAccessFile = this.file;
        if (randomAccessFile == null) {
            j.g();
            throw null;
        }
        FileChannel channel = randomAccessFile.getChannel();
        j.b(channel, "file!!.channel");
        new FileOperator(channel).write(FILE_HEADER_SIZE + j, fVar, this.metadata.s());
    }

    public final void commit(long j) throws IOException {
        writeMetadata(j);
        RandomAccessFile randomAccessFile = this.file;
        if (randomAccessFile == null) {
            j.g();
            throw null;
        }
        randomAccessFile.getChannel().force(false);
        writeHeader(PREFIX_CLEAN, j, this.metadata.s());
        RandomAccessFile randomAccessFile2 = this.file;
        if (randomAccessFile2 == null) {
            j.g();
            throw null;
        }
        randomAccessFile2.getChannel().force(false);
        synchronized (this) {
            this.complete = true;
            c.j jVar = c.j.f1417a;
        }
        a0 a0Var = this.upstream;
        if (a0Var != null) {
            Util.closeQuietly(a0Var);
        }
        this.upstream = null;
    }

    public final f 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 a0 getUpstream() {
        return this.upstream;
    }

    public final f 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 a0 newSource() {
        synchronized (this) {
            if (this.file == null) {
                return null;
            }
            this.sourceCount++;
            return new RelaySource();
        }
    }

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

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

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

    public final void setUpstream(a0 a0Var) {
        this.upstream = a0Var;
    }

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

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

    public /* synthetic */ Relay(RandomAccessFile randomAccessFile, a0 a0Var, long j, i iVar, long j2, g gVar) {
        this(randomAccessFile, a0Var, j, iVar, j2);
    }
}
