what-the-bank/sources/okhttp3/internal/cache2/Relay.java

371 lines
13 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.cache2;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import o.C14866gag;
import o.C14953gcr;
import o.C14957gcv;
import o.C15111ghZ;
import o.C15139giB;
import o.C15155gij;
import o.gdP;
import o.giD;
import okhttp3.internal.Util;
/* loaded from: classes6.dex */
public final class Relay {
public static final Companion Companion = new Companion(null);
private static final long FILE_HEADER_SIZE = 32;
public static final C15155gij PREFIX_CLEAN;
public static final C15155gij PREFIX_DIRTY;
private static final int SOURCE_FILE = 2;
private static final int SOURCE_UPSTREAM = 1;
private final C15111ghZ buffer;
private final long bufferMaxSize;
private boolean complete;
private RandomAccessFile file;
private final C15155gij metadata;
private int sourceCount;
private giD upstream;
private final C15111ghZ upstreamBuffer;
private long upstreamPos;
private Thread upstreamReader;
private Relay(RandomAccessFile randomAccessFile, giD gid, long j, C15155gij c15155gij, long j2) {
this.file = randomAccessFile;
this.upstream = gid;
this.upstreamPos = j;
this.metadata = c15155gij;
this.bufferMaxSize = j2;
this.upstreamBuffer = new C15111ghZ();
this.complete = this.upstream == null;
this.buffer = new C15111ghZ();
}
/* JADX INFO: Access modifiers changed from: private */
public final void writeHeader(C15155gij c15155gij, long j, long j2) throws IOException {
C15111ghZ c15111ghZ = new C15111ghZ();
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ, c15155gij.a());
c15111ghZ.n(j);
c15111ghZ.n(j2);
if (c15111ghZ.c != FILE_HEADER_SIZE) {
throw new IllegalArgumentException("Failed requirement.".toString());
}
RandomAccessFile randomAccessFile = this.file;
C14957gcv.e(randomAccessFile);
FileChannel channel = randomAccessFile.getChannel();
C14957gcv.c(channel, "");
new FileOperator(channel).write(0L, c15111ghZ, FILE_HEADER_SIZE);
}
private final void writeMetadata(long j) throws IOException {
C15111ghZ c15111ghZ = new C15111ghZ();
C15155gij c15155gij = this.metadata;
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ, c15155gij.a());
RandomAccessFile randomAccessFile = this.file;
C14957gcv.e(randomAccessFile);
FileChannel channel = randomAccessFile.getChannel();
C14957gcv.c(channel, "");
new FileOperator(channel).write(j + FILE_HEADER_SIZE, c15111ghZ, this.metadata.a());
}
public final void commit(long j) throws IOException {
writeMetadata(j);
RandomAccessFile randomAccessFile = this.file;
C14957gcv.e(randomAccessFile);
randomAccessFile.getChannel().force(false);
writeHeader(PREFIX_CLEAN, j, this.metadata.a());
RandomAccessFile randomAccessFile2 = this.file;
C14957gcv.e(randomAccessFile2);
randomAccessFile2.getChannel().force(false);
synchronized (this) {
setComplete(true);
C14866gag c14866gag = C14866gag.c;
}
giD gid = this.upstream;
if (gid != null) {
Util.closeQuietly(gid);
}
this.upstream = null;
}
public final giD newSource() {
synchronized (this) {
if (getFile() == null) {
return null;
}
setSourceCount(getSourceCount() + 1);
return new RelaySource(this);
}
}
/* loaded from: classes6.dex */
public final class RelaySource implements giD {
private FileOperator fileOperator;
private long sourcePos;
final Relay this$0;
private final C15139giB timeout;
public RelaySource(Relay relay) {
C14957gcv.e(relay, "");
this.this$0 = relay;
this.timeout = new C15139giB();
RandomAccessFile file = relay.getFile();
C14957gcv.e(file);
FileChannel channel = file.getChannel();
C14957gcv.c(channel, "");
this.fileOperator = new FileOperator(channel);
}
@Override // o.giD
public final long read(C15111ghZ c15111ghZ, long j) throws IOException {
char c;
C14957gcv.e(c15111ghZ, "");
if (this.fileOperator == null) {
throw new IllegalStateException("Check failed.".toString());
}
Relay relay = this.this$0;
synchronized (relay) {
while (true) {
if (this.sourcePos == relay.getUpstreamPos()) {
if (!relay.getComplete()) {
if (relay.getUpstreamReader() == null) {
relay.setUpstreamReader(Thread.currentThread());
c = 1;
break;
}
this.timeout.waitUntilNotified(relay);
} else {
return -1L;
}
} else {
long upstreamPos = relay.getUpstreamPos() - relay.getBuffer().c;
if (this.sourcePos >= upstreamPos) {
long min = Math.min(j, relay.getUpstreamPos() - this.sourcePos);
relay.getBuffer().c(c15111ghZ, this.sourcePos - upstreamPos, min);
this.sourcePos += min;
return min;
}
c = 2;
}
}
if (c == 2) {
long min2 = Math.min(j, this.this$0.getUpstreamPos() - this.sourcePos);
FileOperator fileOperator = this.fileOperator;
C14957gcv.e(fileOperator);
fileOperator.read(this.sourcePos + Relay.FILE_HEADER_SIZE, c15111ghZ, min2);
this.sourcePos += min2;
return min2;
}
try {
giD upstream = this.this$0.getUpstream();
C14957gcv.e(upstream);
long read = upstream.read(this.this$0.getUpstreamBuffer(), this.this$0.getBufferMaxSize());
if (read == -1) {
Relay relay2 = this.this$0;
relay2.commit(relay2.getUpstreamPos());
Relay relay3 = this.this$0;
synchronized (relay3) {
relay3.setUpstreamReader(null);
relay3.notifyAll();
C14866gag c14866gag = C14866gag.c;
}
return -1L;
}
long min3 = Math.min(read, j);
this.this$0.getUpstreamBuffer().c(c15111ghZ, 0L, min3);
this.sourcePos += min3;
FileOperator fileOperator2 = this.fileOperator;
C14957gcv.e(fileOperator2);
fileOperator2.write(this.this$0.getUpstreamPos() + Relay.FILE_HEADER_SIZE, this.this$0.getUpstreamBuffer().clone(), read);
Relay relay4 = this.this$0;
synchronized (relay4) {
relay4.getBuffer().write(relay4.getUpstreamBuffer(), read);
if (relay4.getBuffer().c > relay4.getBufferMaxSize()) {
relay4.getBuffer().j(relay4.getBuffer().c - relay4.getBufferMaxSize());
}
relay4.setUpstreamPos(relay4.getUpstreamPos() + read);
C14866gag c14866gag2 = C14866gag.c;
}
Relay relay5 = this.this$0;
synchronized (relay5) {
relay5.setUpstreamReader(null);
relay5.notifyAll();
C14866gag c14866gag3 = C14866gag.c;
}
return min3;
} catch (Throwable th) {
Relay relay6 = this.this$0;
synchronized (relay6) {
relay6.setUpstreamReader(null);
relay6.notifyAll();
C14866gag c14866gag4 = C14866gag.c;
throw th;
}
}
}
}
@Override // o.giD, java.io.Closeable, java.lang.AutoCloseable
public final void close() throws IOException {
if (this.fileOperator == null) {
return;
}
RandomAccessFile randomAccessFile = null;
this.fileOperator = null;
Relay relay = this.this$0;
synchronized (relay) {
relay.setSourceCount(relay.getSourceCount() - 1);
if (relay.getSourceCount() == 0) {
RandomAccessFile file = relay.getFile();
relay.setFile(null);
randomAccessFile = file;
}
C14866gag c14866gag = C14866gag.c;
}
if (randomAccessFile != null) {
Util.closeQuietly(randomAccessFile);
}
}
@Override // o.giD
public final C15139giB timeout() {
return this.timeout;
}
}
/* loaded from: classes6.dex */
public static final class Companion {
private Companion() {
}
public final Relay edit(File file, giD gid, C15155gij c15155gij, long j) throws IOException {
C14957gcv.e(file, "");
C14957gcv.e(gid, "");
C14957gcv.e(c15155gij, "");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
Relay relay = new Relay(randomAccessFile, gid, 0L, c15155gij, j, null);
randomAccessFile.setLength(0L);
relay.writeHeader(Relay.PREFIX_DIRTY, -1L, -1L);
return relay;
}
public final Relay read(File file) throws IOException {
C14957gcv.e(file, "");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileChannel channel = randomAccessFile.getChannel();
C14957gcv.c(channel, "");
FileOperator fileOperator = new FileOperator(channel);
C15111ghZ c15111ghZ = new C15111ghZ();
fileOperator.read(0L, c15111ghZ, Relay.FILE_HEADER_SIZE);
if (!C14957gcv.b(c15111ghZ.c(Relay.PREFIX_CLEAN.a()), Relay.PREFIX_CLEAN)) {
throw new IOException("unreadable cache file");
}
long p = c15111ghZ.p();
long p2 = c15111ghZ.p();
C15111ghZ c15111ghZ2 = new C15111ghZ();
fileOperator.read(p + Relay.FILE_HEADER_SIZE, c15111ghZ2, p2);
return new Relay(randomAccessFile, null, p, c15111ghZ2.c(c15111ghZ2.c), 0L, null);
}
public /* synthetic */ Companion(C14953gcr c14953gcr) {
this();
}
}
static {
C15155gij.LWm lWm = C15155gij.c;
C14957gcv.e("OkHttp cache v1\n", "");
C14957gcv.e("OkHttp cache v1\n", "");
byte[] bytes = "OkHttp cache v1\n".getBytes(gdP.a);
C14957gcv.c(bytes, "");
C15155gij c15155gij = new C15155gij(bytes);
c15155gij.d = "OkHttp cache v1\n";
PREFIX_CLEAN = c15155gij;
C15155gij.LWm lWm2 = C15155gij.c;
C14957gcv.e("OkHttp DIRTY :(\n", "");
C14957gcv.e("OkHttp DIRTY :(\n", "");
byte[] bytes2 = "OkHttp DIRTY :(\n".getBytes(gdP.a);
C14957gcv.c(bytes2, "");
C15155gij c15155gij2 = new C15155gij(bytes2);
c15155gij2.d = "OkHttp DIRTY :(\n";
PREFIX_DIRTY = c15155gij2;
}
public final void setUpstreamReader(Thread thread) {
this.upstreamReader = thread;
}
public final void setUpstreamPos(long j) {
this.upstreamPos = j;
}
public final void setUpstream(giD gid) {
this.upstream = gid;
}
public final void setSourceCount(int i) {
this.sourceCount = i;
}
public final void setFile(RandomAccessFile randomAccessFile) {
this.file = randomAccessFile;
}
public final void setComplete(boolean z) {
this.complete = z;
}
public final C15155gij metadata() {
return this.metadata;
}
public final boolean isClosed() {
return this.file == null;
}
public final Thread getUpstreamReader() {
return this.upstreamReader;
}
public final long getUpstreamPos() {
return this.upstreamPos;
}
public final C15111ghZ getUpstreamBuffer() {
return this.upstreamBuffer;
}
public final giD getUpstream() {
return this.upstream;
}
public final int getSourceCount() {
return this.sourceCount;
}
public final RandomAccessFile getFile() {
return this.file;
}
public final boolean getComplete() {
return this.complete;
}
public final long getBufferMaxSize() {
return this.bufferMaxSize;
}
public final C15111ghZ getBuffer() {
return this.buffer;
}
public /* synthetic */ Relay(RandomAccessFile randomAccessFile, giD gid, long j, C15155gij c15155gij, long j2, C14953gcr c14953gcr) {
this(randomAccessFile, gid, j, c15155gij, j2);
}
}