package com.google.firebase.crashlytics.internal.metadata; import android.support.v4.media.session.PlaybackStateCompat; import com.ironsource.v8; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.util.NoSuchElementException; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes3.dex */ public class QueueFile implements Closeable { public static final Logger LOGGER = Logger.getLogger(QueueFile.class.getName()); public final byte[] buffer = new byte[16]; public int elementCount; public int fileLength; public Element first; public Element last; public final RandomAccessFile raf; public interface ElementReader { void read(InputStream inputStream, int i); } public final int wrapPosition(int i) { int i2 = this.fileLength; return i < i2 ? i : (i + 16) - i2; } public QueueFile(File file) { if (!file.exists()) { initialize(file); } this.raf = open(file); readHeader(); } public static void writeInt(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; } public static void writeInts(byte[] bArr, int... iArr) { int i = 0; for (int i2 : iArr) { writeInt(bArr, i, i2); i += 4; } } public static int readInt(byte[] bArr, int i) { return ((bArr[i] & 255) << 24) + ((bArr[i + 1] & 255) << 16) + ((bArr[i + 2] & 255) << 8) + (bArr[i + 3] & 255); } public final void readHeader() { this.raf.seek(0L); this.raf.readFully(this.buffer); int readInt = readInt(this.buffer, 0); this.fileLength = readInt; if (readInt > this.raf.length()) { throw new IOException("File is truncated. Expected length: " + this.fileLength + ", Actual length: " + this.raf.length()); } this.elementCount = readInt(this.buffer, 4); int readInt2 = readInt(this.buffer, 8); int readInt3 = readInt(this.buffer, 12); this.first = readElement(readInt2); this.last = readElement(readInt3); } public final void writeHeader(int i, int i2, int i3, int i4) { writeInts(this.buffer, i, i2, i3, i4); this.raf.seek(0L); this.raf.write(this.buffer); } public final Element readElement(int i) { if (i == 0) { return Element.NULL; } this.raf.seek(i); return new Element(i, this.raf.readInt()); } public static void initialize(File file) { File file2 = new File(file.getPath() + ".tmp"); RandomAccessFile open = open(file2); try { open.setLength(PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM); open.seek(0L); byte[] bArr = new byte[16]; writeInts(bArr, 4096, 0, 0, 0); open.write(bArr); open.close(); if (!file2.renameTo(file)) { throw new IOException("Rename failed!"); } } catch (Throwable th) { open.close(); throw th; } } public static RandomAccessFile open(File file) { return new RandomAccessFile(file, "rwd"); } public final void ringWrite(int i, byte[] bArr, int i2, int i3) { int wrapPosition = wrapPosition(i); int i4 = wrapPosition + i3; int i5 = this.fileLength; if (i4 <= i5) { this.raf.seek(wrapPosition); this.raf.write(bArr, i2, i3); return; } int i6 = i5 - wrapPosition; this.raf.seek(wrapPosition); this.raf.write(bArr, i2, i6); this.raf.seek(16L); this.raf.write(bArr, i2 + i6, i3 - i6); } public final void ringRead(int i, byte[] bArr, int i2, int i3) { int wrapPosition = wrapPosition(i); int i4 = wrapPosition + i3; int i5 = this.fileLength; if (i4 <= i5) { this.raf.seek(wrapPosition); this.raf.readFully(bArr, i2, i3); return; } int i6 = i5 - wrapPosition; this.raf.seek(wrapPosition); this.raf.readFully(bArr, i2, i6); this.raf.seek(16L); this.raf.readFully(bArr, i2 + i6, i3 - i6); } public void add(byte[] bArr) { add(bArr, 0, bArr.length); } public synchronized void add(byte[] bArr, int i, int i2) { int wrapPosition; try { nonNull(bArr, "buffer"); if ((i | i2) < 0 || i2 > bArr.length - i) { throw new IndexOutOfBoundsException(); } expandIfNecessary(i2); boolean isEmpty = isEmpty(); if (isEmpty) { wrapPosition = 16; } else { Element element = this.last; wrapPosition = wrapPosition(element.position + 4 + element.length); } Element element2 = new Element(wrapPosition, i2); writeInt(this.buffer, 0, i2); ringWrite(element2.position, this.buffer, 0, 4); ringWrite(element2.position + 4, bArr, i, i2); writeHeader(this.fileLength, this.elementCount + 1, isEmpty ? element2.position : this.first.position, element2.position); this.last = element2; this.elementCount++; if (isEmpty) { this.first = element2; } } catch (Throwable th) { throw th; } } public int usedBytes() { if (this.elementCount == 0) { return 16; } Element element = this.last; int i = element.position; int i2 = this.first.position; if (i >= i2) { return (i - i2) + 4 + element.length + 16; } return (((i + 4) + element.length) + this.fileLength) - i2; } public final int remainingBytes() { return this.fileLength - usedBytes(); } public synchronized boolean isEmpty() { return this.elementCount == 0; } public final void expandIfNecessary(int i) { int i2 = i + 4; int remainingBytes = remainingBytes(); if (remainingBytes >= i2) { return; } int i3 = this.fileLength; do { remainingBytes += i3; i3 <<= 1; } while (remainingBytes < i2); setLength(i3); Element element = this.last; int wrapPosition = wrapPosition(element.position + 4 + element.length); if (wrapPosition < this.first.position) { FileChannel channel = this.raf.getChannel(); channel.position(this.fileLength); long j = wrapPosition - 4; if (channel.transferTo(16L, j, channel) != j) { throw new AssertionError("Copied insufficient number of bytes!"); } } int i4 = this.last.position; int i5 = this.first.position; if (i4 < i5) { int i6 = (this.fileLength + i4) - 16; writeHeader(i3, this.elementCount, i5, i6); this.last = new Element(i6, this.last.length); } else { writeHeader(i3, this.elementCount, i5, i4); } this.fileLength = i3; } public final void setLength(int i) { this.raf.setLength(i); this.raf.getChannel().force(true); } public synchronized void forEach(ElementReader elementReader) { int i = this.first.position; for (int i2 = 0; i2 < this.elementCount; i2++) { Element readElement = readElement(i); elementReader.read(new ElementInputStream(readElement), readElement.length); i = wrapPosition(readElement.position + 4 + readElement.length); } } public static Object nonNull(Object obj, String str) { if (obj != null) { return obj; } throw new NullPointerException(str); } public final class ElementInputStream extends InputStream { public int position; public int remaining; public ElementInputStream(Element element) { this.position = QueueFile.this.wrapPosition(element.position + 4); this.remaining = element.length; } @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) { QueueFile.nonNull(bArr, "buffer"); if ((i | i2) < 0 || i2 > bArr.length - i) { throw new ArrayIndexOutOfBoundsException(); } int i3 = this.remaining; if (i3 <= 0) { return -1; } if (i2 > i3) { i2 = i3; } QueueFile.this.ringRead(this.position, bArr, i, i2); this.position = QueueFile.this.wrapPosition(this.position + i2); this.remaining -= i2; return i2; } @Override // java.io.InputStream public int read() { if (this.remaining == 0) { return -1; } QueueFile.this.raf.seek(this.position); int read = QueueFile.this.raf.read(); this.position = QueueFile.this.wrapPosition(this.position + 1); this.remaining--; return read; } } public synchronized void remove() { try { if (isEmpty()) { throw new NoSuchElementException(); } if (this.elementCount == 1) { clear(); } else { Element element = this.first; int wrapPosition = wrapPosition(element.position + 4 + element.length); ringRead(wrapPosition, this.buffer, 0, 4); int readInt = readInt(this.buffer, 0); writeHeader(this.fileLength, this.elementCount - 1, wrapPosition, this.last.position); this.elementCount--; this.first = new Element(wrapPosition, readInt); } } catch (Throwable th) { throw th; } } public synchronized void clear() { try { writeHeader(4096, 0, 0, 0); this.elementCount = 0; Element element = Element.NULL; this.first = element; this.last = element; if (this.fileLength > 4096) { setLength(4096); } this.fileLength = 4096; } catch (Throwable th) { throw th; } } @Override // java.io.Closeable, java.lang.AutoCloseable public synchronized void close() { this.raf.close(); } public String toString() { final StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); sb.append('['); sb.append("fileLength="); sb.append(this.fileLength); sb.append(", size="); sb.append(this.elementCount); sb.append(", first="); sb.append(this.first); sb.append(", last="); sb.append(this.last); sb.append(", element lengths=["); try { forEach(new ElementReader() { // from class: com.google.firebase.crashlytics.internal.metadata.QueueFile.1 public boolean first = true; @Override // com.google.firebase.crashlytics.internal.metadata.QueueFile.ElementReader public void read(InputStream inputStream, int i) { if (this.first) { this.first = false; } else { sb.append(", "); } sb.append(i); } }); } catch (IOException e) { LOGGER.log(Level.WARNING, "read error", (Throwable) e); } sb.append("]]"); return sb.toString(); } public static class Element { public static final Element NULL = new Element(0, 0); public final int length; public final int position; public Element(int i, int i2) { this.position = i; this.length = i2; } public String toString() { return getClass().getSimpleName() + "[position = " + this.position + ", length = " + this.length + v8.i.e; } } }