package androidx.media3.common.util;

import A4.g;
import E3.f;
import F3.Z;
import Z0.o;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class ParsableByteArray {
    private static final char[] CR_AND_LF = {'\r', '\n'};
    private static final char[] LF = {'\n'};
    private static final Z SUPPORTED_CHARSETS_FOR_READLINE = Z.o(5, f.f1143a, f.f1145c, f.f1148f, f.f1146d, f.f1147e);
    private byte[] data;
    private int limit;
    private int position;

    public ParsableByteArray() {
        this.data = Util.EMPTY_BYTE_ARRAY;
    }

    /* JADX WARN: Removed duplicated region for block: B:37:0x0087  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private int findNextLineTerminator(Charset charset) {
        int i6;
        if (charset.equals(f.f1145c) || charset.equals(f.f1143a)) {
            i6 = 1;
        } else {
            if (!charset.equals(f.f1148f) && !charset.equals(f.f1147e) && !charset.equals(f.f1146d)) {
                throw new IllegalArgumentException("Unsupported charset: " + charset);
            }
            i6 = 2;
        }
        int i7 = this.position;
        while (true) {
            int i8 = this.limit;
            if (i7 >= i8 - (i6 - 1)) {
                return i8;
            }
            if ((charset.equals(f.f1145c) || charset.equals(f.f1143a)) && Util.isLinebreak(this.data[i7])) {
                break;
            }
            if (charset.equals(f.f1148f) || charset.equals(f.f1146d)) {
                byte[] bArr = this.data;
                if (bArr[i7] == 0 && Util.isLinebreak(bArr[i7 + 1])) {
                    break;
                }
                if (charset.equals(f.f1147e)) {
                    byte[] bArr2 = this.data;
                    if (bArr2[i7 + 1] == 0 && Util.isLinebreak(bArr2[i7])) {
                        break;
                    }
                }
                i7 += i6;
            }
        }
        return i7;
    }

    private int peekCharacterAndSize(Charset charset) {
        byte b5;
        int i6;
        byte b6;
        byte b7;
        if ((charset.equals(f.f1145c) || charset.equals(f.f1143a)) && bytesLeft() >= 1) {
            long j4 = this.data[this.position] & 255;
            char c6 = (char) j4;
            g.i(j4, ((long) c6) == j4, "Out of range: %s");
            b5 = (byte) c6;
            i6 = 1;
        } else {
            i6 = 2;
            if ((charset.equals(f.f1148f) || charset.equals(f.f1146d)) && bytesLeft() >= 2) {
                byte[] bArr = this.data;
                int i7 = this.position;
                b6 = bArr[i7];
                b7 = bArr[i7 + 1];
            } else {
                if (!charset.equals(f.f1147e) || bytesLeft() < 2) {
                    return 0;
                }
                byte[] bArr2 = this.data;
                int i8 = this.position;
                b6 = bArr2[i8 + 1];
                b7 = bArr2[i8];
            }
            b5 = (byte) ((char) ((b7 & 255) | (b6 << 8)));
        }
        long j6 = b5;
        char c7 = (char) j6;
        g.i(j6, ((long) c7) == j6, "Out of range: %s");
        return (c7 << 16) + i6;
    }

    private char readCharacterIfInList(Charset charset, char[] cArr) {
        int iPeekCharacterAndSize = peekCharacterAndSize(charset);
        if (iPeekCharacterAndSize != 0) {
            char c6 = (char) (iPeekCharacterAndSize >> 16);
            for (char c7 : cArr) {
                if (c7 == c6) {
                    this.position += iPeekCharacterAndSize & 65535;
                    return c6;
                }
            }
        }
        return (char) 0;
    }

    private void skipLineTerminator(Charset charset) {
        if (readCharacterIfInList(charset, CR_AND_LF) == '\r') {
            readCharacterIfInList(charset, LF);
        }
    }

    public int bytesLeft() {
        return this.limit - this.position;
    }

    public int capacity() {
        return this.data.length;
    }

    public void ensureCapacity(int i6) {
        if (i6 > capacity()) {
            this.data = Arrays.copyOf(this.data, i6);
        }
    }

    public byte[] getData() {
        return this.data;
    }

    public int getPosition() {
        return this.position;
    }

    public int limit() {
        return this.limit;
    }

    public char peekChar() {
        byte[] bArr = this.data;
        int i6 = this.position;
        return (char) ((bArr[i6 + 1] & 255) | ((bArr[i6] & 255) << 8));
    }

    public int peekUnsignedByte() {
        return this.data[this.position] & 255;
    }

    public void readBytes(ParsableBitArray parsableBitArray, int i6) {
        readBytes(parsableBitArray.data, 0, i6);
        parsableBitArray.setPosition(0);
    }

    public String readDelimiterTerminatedString(char c6) {
        if (bytesLeft() == 0) {
            return null;
        }
        int i6 = this.position;
        while (i6 < this.limit && this.data[i6] != c6) {
            i6++;
        }
        byte[] bArr = this.data;
        int i7 = this.position;
        String strFromUtf8Bytes = Util.fromUtf8Bytes(bArr, i7, i6 - i7);
        this.position = i6;
        if (i6 < this.limit) {
            this.position = i6 + 1;
        }
        return strFromUtf8Bytes;
    }

    public double readDouble() {
        return Double.longBitsToDouble(readLong());
    }

    public float readFloat() {
        return Float.intBitsToFloat(readInt());
    }

    public int readInt() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = (bArr[i6] & 255) << 24;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 16) | i8;
        int i11 = i6 + 3;
        this.position = i11;
        int i12 = i10 | ((bArr[i9] & 255) << 8);
        this.position = i6 + 4;
        return (bArr[i11] & 255) | i12;
    }

    public int readInt24() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = ((bArr[i6] & 255) << 24) >> 8;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 8) | i8;
        this.position = i6 + 3;
        return (bArr[i9] & 255) | i10;
    }

    public String readLine() {
        return readLine(f.f1145c);
    }

    public int readLittleEndianInt() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = bArr[i6] & 255;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 8) | i8;
        int i11 = i6 + 3;
        this.position = i11;
        int i12 = i10 | ((bArr[i9] & 255) << 16);
        this.position = i6 + 4;
        return ((bArr[i11] & 255) << 24) | i12;
    }

    public int readLittleEndianInt24() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = bArr[i6] & 255;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 8) | i8;
        this.position = i6 + 3;
        return ((bArr[i9] & 255) << 16) | i10;
    }

    public long readLittleEndianLong() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        long j4 = ((long) bArr[i6]) & 255;
        int i8 = i6 + 2;
        this.position = i8;
        long j6 = j4 | ((((long) bArr[i7]) & 255) << 8);
        int i9 = i6 + 3;
        this.position = i9;
        long j7 = j6 | ((((long) bArr[i8]) & 255) << 16);
        int i10 = i6 + 4;
        this.position = i10;
        long j8 = j7 | ((((long) bArr[i9]) & 255) << 24);
        int i11 = i6 + 5;
        this.position = i11;
        long j9 = j8 | ((((long) bArr[i10]) & 255) << 32);
        int i12 = i6 + 6;
        this.position = i12;
        long j10 = j9 | ((((long) bArr[i11]) & 255) << 40);
        int i13 = i6 + 7;
        this.position = i13;
        long j11 = j10 | ((((long) bArr[i12]) & 255) << 48);
        this.position = i6 + 8;
        return ((((long) bArr[i13]) & 255) << 56) | j11;
    }

    public short readLittleEndianShort() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = bArr[i6] & 255;
        this.position = i6 + 2;
        return (short) (((bArr[i7] & 255) << 8) | i8);
    }

    public long readLittleEndianUnsignedInt() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        long j4 = ((long) bArr[i6]) & 255;
        int i8 = i6 + 2;
        this.position = i8;
        long j6 = j4 | ((((long) bArr[i7]) & 255) << 8);
        int i9 = i6 + 3;
        this.position = i9;
        long j7 = j6 | ((((long) bArr[i8]) & 255) << 16);
        this.position = i6 + 4;
        return ((((long) bArr[i9]) & 255) << 24) | j7;
    }

    public int readLittleEndianUnsignedInt24() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = bArr[i6] & 255;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 8) | i8;
        this.position = i6 + 3;
        return ((bArr[i9] & 255) << 16) | i10;
    }

    public int readLittleEndianUnsignedIntToInt() {
        int littleEndianInt = readLittleEndianInt();
        if (littleEndianInt >= 0) {
            return littleEndianInt;
        }
        throw new IllegalStateException(o.l("Top bit not zero: ", littleEndianInt));
    }

    public int readLittleEndianUnsignedShort() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = bArr[i6] & 255;
        this.position = i6 + 2;
        return ((bArr[i7] & 255) << 8) | i8;
    }

    public long readLong() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        long j4 = (((long) bArr[i6]) & 255) << 56;
        int i8 = i6 + 2;
        this.position = i8;
        long j6 = j4 | ((((long) bArr[i7]) & 255) << 48);
        int i9 = i6 + 3;
        this.position = i9;
        long j7 = j6 | ((((long) bArr[i8]) & 255) << 40);
        int i10 = i6 + 4;
        this.position = i10;
        long j8 = j7 | ((((long) bArr[i9]) & 255) << 32);
        int i11 = i6 + 5;
        this.position = i11;
        long j9 = j8 | ((((long) bArr[i10]) & 255) << 24);
        int i12 = i6 + 6;
        this.position = i12;
        long j10 = j9 | ((((long) bArr[i11]) & 255) << 16);
        int i13 = i6 + 7;
        this.position = i13;
        long j11 = j10 | ((((long) bArr[i12]) & 255) << 8);
        this.position = i6 + 8;
        return (((long) bArr[i13]) & 255) | j11;
    }

    public String readNullTerminatedString(int i6) {
        if (i6 == 0) {
            return "";
        }
        int i7 = this.position;
        int i8 = (i7 + i6) - 1;
        String strFromUtf8Bytes = Util.fromUtf8Bytes(this.data, i7, (i8 >= this.limit || this.data[i8] != 0) ? i6 : i6 - 1);
        this.position += i6;
        return strFromUtf8Bytes;
    }

    public short readShort() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = (bArr[i6] & 255) << 8;
        this.position = i6 + 2;
        return (short) ((bArr[i7] & 255) | i8);
    }

    public String readString(int i6) {
        return readString(i6, f.f1145c);
    }

    public int readSynchSafeInt() {
        return (readUnsignedByte() << 21) | (readUnsignedByte() << 14) | (readUnsignedByte() << 7) | readUnsignedByte();
    }

    public int readUnsignedByte() {
        byte[] bArr = this.data;
        int i6 = this.position;
        this.position = i6 + 1;
        return bArr[i6] & 255;
    }

    public int readUnsignedFixedPoint1616() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = (bArr[i6] & 255) << 8;
        this.position = i6 + 2;
        int i9 = (bArr[i7] & 255) | i8;
        this.position = i6 + 4;
        return i9;
    }

    public long readUnsignedInt() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        long j4 = (((long) bArr[i6]) & 255) << 24;
        int i8 = i6 + 2;
        this.position = i8;
        long j6 = j4 | ((((long) bArr[i7]) & 255) << 16);
        int i9 = i6 + 3;
        this.position = i9;
        long j7 = j6 | ((((long) bArr[i8]) & 255) << 8);
        this.position = i6 + 4;
        return (((long) bArr[i9]) & 255) | j7;
    }

    public int readUnsignedInt24() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = (bArr[i6] & 255) << 16;
        int i9 = i6 + 2;
        this.position = i9;
        int i10 = ((bArr[i7] & 255) << 8) | i8;
        this.position = i6 + 3;
        return (bArr[i9] & 255) | i10;
    }

    public int readUnsignedIntToInt() {
        int i6 = readInt();
        if (i6 >= 0) {
            return i6;
        }
        throw new IllegalStateException(o.l("Top bit not zero: ", i6));
    }

    public long readUnsignedLongToLong() {
        long j4 = readLong();
        if (j4 >= 0) {
            return j4;
        }
        throw new IllegalStateException(A0.a.m("Top bit not zero: ", j4));
    }

    public int readUnsignedShort() {
        byte[] bArr = this.data;
        int i6 = this.position;
        int i7 = i6 + 1;
        this.position = i7;
        int i8 = (bArr[i6] & 255) << 8;
        this.position = i6 + 2;
        return (bArr[i7] & 255) | i8;
    }

    public long readUtf8EncodedLong() {
        int i6;
        int i7;
        long j4 = this.data[this.position];
        int i8 = 7;
        while (true) {
            if (i8 < 0) {
                break;
            }
            int i9 = 1 << i8;
            if ((((long) i9) & j4) != 0) {
                i8--;
            } else if (i8 < 6) {
                j4 &= (long) (i9 - 1);
                i7 = 7 - i8;
            } else if (i8 == 7) {
                i7 = 1;
            }
        }
        i7 = 0;
        if (i7 == 0) {
            throw new NumberFormatException(A0.a.m("Invalid UTF-8 sequence first byte: ", j4));
        }
        for (i6 = 1; i6 < i7; i6++) {
            byte b5 = this.data[this.position + i6];
            if ((b5 & 192) != 128) {
                throw new NumberFormatException(A0.a.m("Invalid UTF-8 sequence continuation byte: ", j4));
            }
            j4 = (j4 << 6) | ((long) (b5 & 63));
        }
        this.position += i7;
        return j4;
    }

    public Charset readUtfCharsetFromBom() {
        if (bytesLeft() >= 3) {
            byte[] bArr = this.data;
            int i6 = this.position;
            if (bArr[i6] == -17 && bArr[i6 + 1] == -69 && bArr[i6 + 2] == -65) {
                this.position = i6 + 3;
                return f.f1145c;
            }
        }
        if (bytesLeft() < 2) {
            return null;
        }
        byte[] bArr2 = this.data;
        int i7 = this.position;
        byte b5 = bArr2[i7];
        if (b5 == -2 && bArr2[i7 + 1] == -1) {
            this.position = i7 + 2;
            return f.f1146d;
        }
        if (b5 != -1 || bArr2[i7 + 1] != -2) {
            return null;
        }
        this.position = i7 + 2;
        return f.f1147e;
    }

    public void reset(int i6) {
        reset(capacity() < i6 ? new byte[i6] : this.data, i6);
    }

    public void setLimit(int i6) {
        Assertions.checkArgument(i6 >= 0 && i6 <= this.data.length);
        this.limit = i6;
    }

    public void setPosition(int i6) {
        Assertions.checkArgument(i6 >= 0 && i6 <= this.limit);
        this.position = i6;
    }

    public void skipBytes(int i6) {
        setPosition(this.position + i6);
    }

    public char peekChar(Charset charset) {
        Assertions.checkArgument(SUPPORTED_CHARSETS_FOR_READLINE.contains(charset), "Unsupported charset: " + charset);
        return (char) (peekCharacterAndSize(charset) >> 16);
    }

    public String readLine(Charset charset) {
        Assertions.checkArgument(SUPPORTED_CHARSETS_FOR_READLINE.contains(charset), "Unsupported charset: " + charset);
        if (bytesLeft() == 0) {
            return null;
        }
        if (!charset.equals(f.f1143a)) {
            readUtfCharsetFromBom();
        }
        String string = readString(findNextLineTerminator(charset) - this.position, charset);
        if (this.position == this.limit) {
            return string;
        }
        skipLineTerminator(charset);
        return string;
    }

    public String readString(int i6, Charset charset) {
        String str = new String(this.data, this.position, i6, charset);
        this.position += i6;
        return str;
    }

    public void reset(byte[] bArr) {
        reset(bArr, bArr.length);
    }

    public ParsableByteArray(int i6) {
        this.data = new byte[i6];
        this.limit = i6;
    }

    public void readBytes(byte[] bArr, int i6, int i7) {
        System.arraycopy(this.data, this.position, bArr, i6, i7);
        this.position += i7;
    }

    public void reset(byte[] bArr, int i6) {
        this.data = bArr;
        this.limit = i6;
        this.position = 0;
    }

    public void readBytes(ByteBuffer byteBuffer, int i6) {
        byteBuffer.put(this.data, this.position, i6);
        this.position += i6;
    }

    public ParsableByteArray(byte[] bArr) {
        this.data = bArr;
        this.limit = bArr.length;
    }

    public String readNullTerminatedString() {
        return readDelimiterTerminatedString((char) 0);
    }

    public ParsableByteArray(byte[] bArr, int i6) {
        this.data = bArr;
        this.limit = i6;
    }
}
