package io.fabric.sdk.android.services.common; import com.ubt.jimu.base.util.FileUtil; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes2.dex */ public class QueueFile implements Closeable { private static final Logger g = Logger.getLogger(QueueFile.class.getName()); private final RandomAccessFile a; int b; private int c; private Element d; private Element e; private final byte[] f = new byte[16]; static class Element { static final Element c = new Element(0, 0); final int a; final int b; Element(int i, int i2) { this.a = i; this.b = i2; } public String toString() { return Element.class.getSimpleName() + "[position = " + this.a + ", length = " + this.b + "]"; } } private final class ElementInputStream extends InputStream { private int a; private int b; @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) throws IOException { QueueFile.a(bArr, "buffer"); if ((i | i2) < 0 || i2 > bArr.length - i) { throw new ArrayIndexOutOfBoundsException(); } int i3 = this.b; if (i3 <= 0) { return -1; } if (i2 > i3) { i2 = i3; } QueueFile.this.a(this.a, bArr, i, i2); this.a = QueueFile.this.d(this.a + i2); this.b -= i2; return i2; } private ElementInputStream(Element element) { this.a = QueueFile.this.d(element.a + 4); this.b = element.b; } @Override // java.io.InputStream public int read() throws IOException { if (this.b == 0) { return -1; } QueueFile.this.a.seek(this.a); int read = QueueFile.this.a.read(); this.a = QueueFile.this.d(this.a + 1); this.b--; return read; } } public interface ElementReader { void a(InputStream inputStream, int i) throws IOException; } public QueueFile(File file) throws IOException { if (!file.exists()) { a(file); } this.a = b(file); c(); } private static void b(byte[] bArr, int i, int i2) { bArr[i] = (byte) (i2 >> 24); bArr[i + 1] = (byte) (i2 >> 16); bArr[i + 2] = (byte) (i2 >> 8); bArr[i + 3] = (byte) i2; } private void c() throws IOException { this.a.seek(0L); this.a.readFully(this.f); this.b = a(this.f, 0); if (this.b <= this.a.length()) { this.c = a(this.f, 4); int a = a(this.f, 8); int a2 = a(this.f, 12); this.d = b(a); this.e = b(a2); return; } throw new IOException("File is truncated. Expected length: " + this.b + ", Actual length: " + this.a.length()); } /* JADX INFO: Access modifiers changed from: private */ public int d(int i) { int i2 = this.b; return i < i2 ? i : (i + 16) - i2; } @Override // java.io.Closeable, java.lang.AutoCloseable public synchronized void close() throws IOException { this.a.close(); } public String toString() { final StringBuilder sb = new StringBuilder(); sb.append(QueueFile.class.getSimpleName()); sb.append('['); sb.append("fileLength="); sb.append(this.b); sb.append(", size="); sb.append(this.c); sb.append(", first="); sb.append(this.d); sb.append(", last="); sb.append(this.e); sb.append(", element lengths=["); try { a(new ElementReader(this) { // from class: io.fabric.sdk.android.services.common.QueueFile.1 boolean a = true; @Override // io.fabric.sdk.android.services.common.QueueFile.ElementReader public void a(InputStream inputStream, int i) throws IOException { if (this.a) { this.a = false; } else { sb.append(", "); } sb.append(i); } }); } catch (IOException e) { g.log(Level.WARNING, "read error", (Throwable) e); } sb.append("]]"); return sb.toString(); } static /* synthetic */ Object a(Object obj, String str) { b(obj, str); return obj; } private int d() { return this.b - b(); } private static void a(byte[] bArr, int... iArr) { int i = 0; for (int i2 : iArr) { b(bArr, i, i2); i += 4; } } private Element b(int i) throws IOException { if (i == 0) { return Element.c; } this.a.seek(i); return new Element(i, this.a.readInt()); } private static int a(byte[] bArr, int i) { return ((bArr[i] & 255) << 24) + ((bArr[i + 1] & 255) << 16) + ((bArr[i + 2] & 255) << 8) + (bArr[i + 3] & 255); } private void a(int i, int i2, int i3, int i4) throws IOException { a(this.f, i, i2, i3, i4); this.a.seek(0L); this.a.write(this.f); } private static RandomAccessFile b(File file) throws FileNotFoundException { return new RandomAccessFile(file, "rwd"); } private void b(int i, byte[] bArr, int i2, int i3) throws IOException { int d = d(i); int i4 = d + i3; int i5 = this.b; if (i4 <= i5) { this.a.seek(d); this.a.write(bArr, i2, i3); return; } int i6 = i5 - d; this.a.seek(d); this.a.write(bArr, i2, i6); this.a.seek(16L); this.a.write(bArr, i2 + i6, i3 - i6); } private static void a(File file) throws IOException { File file2 = new File(file.getPath() + ".tmp"); RandomAccessFile b = b(file2); try { b.setLength(4096L); b.seek(0L); byte[] bArr = new byte[16]; a(bArr, FileUtil.ZIP_BUFFER_SIZE, 0, 0, 0); b.write(bArr); b.close(); if (!file2.renameTo(file)) { throw new IOException("Rename failed!"); } } catch (Throwable th) { b.close(); throw th; } } private void c(int i) throws IOException { this.a.setLength(i); this.a.getChannel().force(true); } public int b() { if (this.c == 0) { return 16; } Element element = this.e; int i = element.a; int i2 = this.d.a; if (i >= i2) { return (i - i2) + 4 + element.b + 16; } return (((i + 4) + element.b) + this.b) - i2; } /* JADX INFO: Access modifiers changed from: private */ public void a(int i, byte[] bArr, int i2, int i3) throws IOException { int d = d(i); int i4 = d + i3; int i5 = this.b; if (i4 <= i5) { this.a.seek(d); this.a.readFully(bArr, i2, i3); return; } int i6 = i5 - d; this.a.seek(d); this.a.readFully(bArr, i2, i6); this.a.seek(16L); this.a.readFully(bArr, i2 + i6, i3 - i6); } private static T b(T t, String str) { if (t != null) { return t; } throw new NullPointerException(str); } public void a(byte[] bArr) throws IOException { a(bArr, 0, bArr.length); } public synchronized void a(byte[] bArr, int i, int i2) throws IOException { b(bArr, "buffer"); if ((i | i2) >= 0 && i2 <= bArr.length - i) { a(i2); boolean a = a(); Element element = new Element(a ? 16 : d(this.e.a + 4 + this.e.b), i2); b(this.f, 0, i2); b(element.a, this.f, 0, 4); b(element.a + 4, bArr, i, i2); a(this.b, this.c + 1, a ? element.a : this.d.a, element.a); this.e = element; this.c++; if (a) { this.d = this.e; } } else { throw new IndexOutOfBoundsException(); } } public synchronized boolean a() { return this.c == 0; } private void a(int i) throws IOException { int i2 = i + 4; int d = d(); if (d >= i2) { return; } int i3 = this.b; do { d += i3; i3 <<= 1; } while (d < i2); c(i3); Element element = this.e; int d2 = d(element.a + 4 + element.b); if (d2 < this.d.a) { FileChannel channel = this.a.getChannel(); channel.position(this.b); long j = d2 - 4; if (channel.transferTo(16L, j, channel) != j) { throw new AssertionError("Copied insufficient number of bytes!"); } } int i4 = this.e.a; int i5 = this.d.a; if (i4 < i5) { int i6 = (this.b + i4) - 16; a(i3, this.c, i5, i6); this.e = new Element(i6, this.e.b); } else { a(i3, this.c, i5, i4); } this.b = i3; } public synchronized void a(ElementReader elementReader) throws IOException { int i = this.d.a; for (int i2 = 0; i2 < this.c; i2++) { Element b = b(i); elementReader.a(new ElementInputStream(b), b.b); i = d(b.a + 4 + b.b); } } public boolean a(int i, int i2) { return (b() + 4) + i <= i2; } }