package okhttp3.internal.cache2;

import h.c;
import h.f;
import h.s;
import h.t;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import okhttp3.internal.Util;

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

    class RelaySource implements s {
        private FileOperator fileOperator;
        private long sourcePos;
        private final t timeout = new t();

        RelaySource() {
            this.fileOperator = new FileOperator(Relay.this.file.getChannel());
        }

        @Override // h.s, 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 relay = Relay.this;
                relay.sourceCount--;
                if (Relay.this.sourceCount == 0) {
                    RandomAccessFile randomAccessFile2 = Relay.this.file;
                    Relay.this.file = null;
                    randomAccessFile = randomAccessFile2;
                }
            }
            if (randomAccessFile != null) {
                Util.closeQuietly(randomAccessFile);
            }
        }

        @Override // h.s
        public long read(c cVar, long j2) throws IOException {
            long j3;
            char c2;
            if (this.fileOperator == null) {
                throw new IllegalStateException("closed");
            }
            synchronized (Relay.this) {
                while (true) {
                    long j4 = this.sourcePos;
                    j3 = Relay.this.upstreamPos;
                    if (j4 != j3) {
                        long jB = j3 - Relay.this.buffer.B();
                        if (this.sourcePos >= jB) {
                            long jMin = Math.min(j2, j3 - this.sourcePos);
                            Relay.this.buffer.h(cVar, this.sourcePos - jB, jMin);
                            this.sourcePos += jMin;
                            return jMin;
                        }
                        c2 = 2;
                    } else if (!Relay.this.complete) {
                        if (Relay.this.upstreamReader == null) {
                            Relay.this.upstreamReader = Thread.currentThread();
                            c2 = 1;
                            break;
                        }
                        this.timeout.waitUntilNotified(Relay.this);
                    } else {
                        return -1L;
                    }
                }
                if (c2 == 2) {
                    long jMin2 = Math.min(j2, j3 - this.sourcePos);
                    this.fileOperator.read(this.sourcePos + Relay.FILE_HEADER_SIZE, cVar, jMin2);
                    this.sourcePos += jMin2;
                    return jMin2;
                }
                try {
                    long j5 = Relay.this.upstream.read(Relay.this.upstreamBuffer, Relay.this.bufferMaxSize);
                    if (j5 == -1) {
                        Relay.this.commit(j3);
                        synchronized (Relay.this) {
                            Relay.this.upstreamReader = null;
                            Relay.this.notifyAll();
                        }
                        return -1L;
                    }
                    long jMin3 = Math.min(j5, j2);
                    Relay.this.upstreamBuffer.h(cVar, 0L, jMin3);
                    this.sourcePos += jMin3;
                    this.fileOperator.write(j3 + Relay.FILE_HEADER_SIZE, Relay.this.upstreamBuffer.clone(), j5);
                    synchronized (Relay.this) {
                        Relay.this.buffer.write(Relay.this.upstreamBuffer, j5);
                        if (Relay.this.buffer.B() > Relay.this.bufferMaxSize) {
                            Relay.this.buffer.skip(Relay.this.buffer.B() - Relay.this.bufferMaxSize);
                        }
                        Relay.this.upstreamPos += j5;
                    }
                    synchronized (Relay.this) {
                        Relay.this.upstreamReader = null;
                        Relay.this.notifyAll();
                    }
                    return jMin3;
                } catch (Throwable th) {
                    synchronized (Relay.this) {
                        Relay.this.upstreamReader = null;
                        Relay.this.notifyAll();
                        throw th;
                    }
                }
            }
        }

        @Override // h.s
        public t timeout() {
            return this.timeout;
        }
    }

    private Relay(RandomAccessFile randomAccessFile, s sVar, long j2, f fVar, long j3) {
        this.file = randomAccessFile;
        this.upstream = sVar;
        this.complete = sVar == null;
        this.upstreamPos = j2;
        this.metadata = fVar;
        this.bufferMaxSize = j3;
    }

    public static Relay edit(File file, s sVar, f fVar, long j2) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        Relay relay = new Relay(randomAccessFile, sVar, 0L, fVar, j2);
        randomAccessFile.setLength(0L);
        relay.writeHeader(PREFIX_DIRTY, -1L, -1L);
        return relay;
    }

    public static Relay read(File file) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        FileOperator fileOperator = new FileOperator(randomAccessFile.getChannel());
        c cVar = new c();
        fileOperator.read(0L, cVar, FILE_HEADER_SIZE);
        if (!cVar.e(PREFIX_CLEAN.q()).equals(PREFIX_CLEAN)) {
            throw new IOException("unreadable cache file");
        }
        long j2 = cVar.readLong();
        long j3 = cVar.readLong();
        c cVar2 = new c();
        fileOperator.read(j2 + FILE_HEADER_SIZE, cVar2, j3);
        return new Relay(randomAccessFile, null, j2, cVar2.p(), 0L);
    }

    private void writeHeader(f fVar, long j2, long j3) throws IOException {
        c cVar = new c();
        cVar.H(fVar);
        cVar.S(j2);
        cVar.S(j3);
        if (cVar.B() != FILE_HEADER_SIZE) {
            throw new IllegalArgumentException();
        }
        new FileOperator(this.file.getChannel()).write(0L, cVar, FILE_HEADER_SIZE);
    }

    private void writeMetadata(long j2) throws IOException {
        c cVar = new c();
        cVar.H(this.metadata);
        new FileOperator(this.file.getChannel()).write(FILE_HEADER_SIZE + j2, cVar, this.metadata.q());
    }

    void commit(long j2) throws IOException {
        writeMetadata(j2);
        this.file.getChannel().force(false);
        writeHeader(PREFIX_CLEAN, j2, this.metadata.q());
        this.file.getChannel().force(false);
        synchronized (this) {
            this.complete = true;
        }
        Util.closeQuietly(this.upstream);
        this.upstream = null;
    }

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

    public f metadata() {
        return this.metadata;
    }

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