package okhttp3.internal.cache2;

import g.h0.c.g;
import g.h0.c.l;
import j.b0;
import j.c0;
import j.d;
import j.h;
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 {
    public static final Companion Companion = new Companion(null);
    private static final long FILE_HEADER_SIZE = 32;
    public static final h PREFIX_CLEAN;
    public static final h PREFIX_DIRTY;
    private static final int SOURCE_FILE = 2;
    private static final int SOURCE_UPSTREAM = 1;
    private final d buffer;
    private final long bufferMaxSize;
    private boolean complete;
    private RandomAccessFile file;
    private final h metadata;
    private int sourceCount;
    private b0 upstream;
    private final d upstreamBuffer;
    private long upstreamPos;
    private Thread upstreamReader;

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

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

        public final Relay read(File file) throws IOException {
            l.f(file, "file");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            FileChannel channel = randomAccessFile.getChannel();
            l.e(channel, "randomAccessFile.channel");
            FileOperator fileOperator = new FileOperator(channel);
            d dVar = new d();
            fileOperator.read(0L, dVar, Relay.FILE_HEADER_SIZE);
            h hVar = Relay.PREFIX_CLEAN;
            if (!l.a(dVar.c(hVar.c()), hVar)) {
                throw new IOException("unreadable cache file");
            }
            long j2 = dVar.j();
            long j3 = dVar.j();
            d dVar2 = new d();
            fileOperator.read(j2 + Relay.FILE_HEADER_SIZE, dVar2, j3);
            return new Relay(randomAccessFile, null, j2, dVar2.I(), 0L, null);
        }

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

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

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

        @Override // j.b0, 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;
                }
            }
            if (randomAccessFile != null) {
                Util.closeQuietly(randomAccessFile);
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:27:0x0089, code lost:
        
            if (r4 != 2) goto L30;
         */
        /* JADX WARN: Code restructure failed: missing block: B:28:0x008b, code lost:
        
            r8 = java.lang.Math.min(r21, r19.this$0.getUpstreamPos() - r19.sourcePos);
            r2 = r19.fileOperator;
            g.h0.c.l.c(r2);
            r2.read(r19.sourcePos + okhttp3.internal.cache2.Relay.FILE_HEADER_SIZE, r20, r8);
            r19.sourcePos += r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:29:0x00ab, code lost:
        
            return r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:31:0x00ad, code lost:
        
            r0 = r19.this$0.getUpstream();
            g.h0.c.l.c(r0);
            r14 = r0.read(r19.this$0.getUpstreamBuffer(), r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:32:0x00c8, code lost:
        
            if (r14 != (-1)) goto L46;
         */
        /* JADX WARN: Code restructure failed: missing block: B:33:0x00ca, code lost:
        
            r0 = r19.this$0;
            r0.commit(r0.getUpstreamPos());
         */
        /* JADX WARN: Code restructure failed: missing block: B:34:0x00d3, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x00d5, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:36:0x00d6, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:37:0x00dd, code lost:
        
            if (r0 == null) goto L41;
         */
        /* JADX WARN: Code restructure failed: missing block: B:38:0x00df, code lost:
        
            r0.notifyAll();
         */
        /* JADX WARN: Code restructure failed: missing block: B:39:0x00e2, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:40:0x00e3, code lost:
        
            return -1;
         */
        /* JADX WARN: Code restructure failed: missing block: B:42:0x00eb, 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:46:0x00ef, code lost:
        
            r9 = java.lang.Math.min(r14, r21);
            r19.this$0.getUpstreamBuffer().d(r20, 0, r9);
            r19.sourcePos += r9;
            r13 = r19.fileOperator;
            g.h0.c.l.c(r13);
            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:47:0x0126, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:48:0x0127, code lost:
        
            r19.this$0.getBuffer().write(r19.this$0.getUpstreamBuffer(), r14);
         */
        /* JADX WARN: Code restructure failed: missing block: B:49:0x0146, code lost:
        
            if (r19.this$0.getBuffer().f4601e <= r19.this$0.getBufferMaxSize()) goto L53;
         */
        /* JADX WARN: Code restructure failed: missing block: B:50:0x0148, code lost:
        
            r19.this$0.getBuffer().K(r19.this$0.getBuffer().f4601e - r19.this$0.getBufferMaxSize());
         */
        /* JADX WARN: Code restructure failed: missing block: B:51:0x0161, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:53:0x0163, code lost:
        
            r0 = r19.this$0;
            r0.setUpstreamPos(r0.getUpstreamPos() + r14);
         */
        /* JADX WARN: Code restructure failed: missing block: B:54:0x016d, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:55:0x016e, code lost:
        
            r2 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:56:0x0170, code lost:
        
            monitor-enter(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:57:0x0171, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r0 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:58:0x0178, code lost:
        
            if (r0 == null) goto L62;
         */
        /* JADX WARN: Code restructure failed: missing block: B:59:0x017a, code lost:
        
            r0.notifyAll();
         */
        /* JADX WARN: Code restructure failed: missing block: B:60:0x017d, code lost:
        
            monitor-exit(r2);
         */
        /* JADX WARN: Code restructure failed: missing block: B:61:0x017e, code lost:
        
            return r9;
         */
        /* JADX WARN: Code restructure failed: missing block: B:63:0x0186, 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:68:0x018b, code lost:
        
            throw r0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:69:0x018c, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:71:0x018f, code lost:
        
            monitor-enter(r19.this$0);
         */
        /* JADX WARN: Code restructure failed: missing block: B:72:0x0190, code lost:
        
            r19.this$0.setUpstreamReader(null);
            r3 = r19.this$0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:73:0x0197, code lost:
        
            if (r3 == null) goto L74;
         */
        /* JADX WARN: Code restructure failed: missing block: B:75:0x01a0, 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:76:0x01a1, code lost:
        
            r3.notifyAll();
         */
        /* JADX WARN: Code restructure failed: missing block: B:78:0x01a5, code lost:
        
            throw r0;
         */
        @Override // j.b0
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public long read(j.d r20, long r21) throws java.io.IOException {
            /*
                Method dump skipped, instruction units count: 440
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.cache2.Relay.RelaySource.read(j.d, long):long");
        }

        @Override // j.b0
        public c0 timeout() {
            return this.timeout;
        }
    }

    static {
        h.a aVar = h.f4612e;
        PREFIX_CLEAN = aVar.c("OkHttp cache v1\n");
        PREFIX_DIRTY = aVar.c("OkHttp DIRTY :(\n");
    }

    private Relay(RandomAccessFile randomAccessFile, b0 b0Var, long j2, h hVar, long j3) {
        this.file = randomAccessFile;
        this.upstream = b0Var;
        this.upstreamPos = j2;
        this.metadata = hVar;
        this.bufferMaxSize = j3;
        this.upstreamBuffer = new d();
        this.complete = b0Var == null;
        this.buffer = new d();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void writeHeader(h hVar, long j2, long j3) throws IOException {
        d dVar = new d();
        dVar.y(hVar);
        dVar.L(j2);
        dVar.L(j3);
        if (!(dVar.f4601e == FILE_HEADER_SIZE)) {
            throw new IllegalArgumentException("Failed requirement.".toString());
        }
        RandomAccessFile randomAccessFile = this.file;
        l.c(randomAccessFile);
        FileChannel channel = randomAccessFile.getChannel();
        l.e(channel, "file!!.channel");
        new FileOperator(channel).write(0L, dVar, FILE_HEADER_SIZE);
    }

    private final void writeMetadata(long j2) throws IOException {
        d dVar = new d();
        dVar.y(this.metadata);
        RandomAccessFile randomAccessFile = this.file;
        l.c(randomAccessFile);
        FileChannel channel = randomAccessFile.getChannel();
        l.e(channel, "file!!.channel");
        new FileOperator(channel).write(FILE_HEADER_SIZE + j2, dVar, this.metadata.c());
    }

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

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

    public final d 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 h metadata() {
        return this.metadata;
    }

    public final b0 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 i2) {
        this.sourceCount = i2;
    }

    public final void setUpstream(b0 b0Var) {
        this.upstream = b0Var;
    }

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

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

    public /* synthetic */ Relay(RandomAccessFile randomAccessFile, b0 b0Var, long j2, h hVar, long j3, g gVar) {
        this(randomAccessFile, b0Var, j2, hVar, j3);
    }
}
