package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;

/* JADX INFO: loaded from: classes.dex */
class Bzip2BitReader {
    private static final int MAX_COUNT_OF_READABLE_BYTES = 268435455;
    private long bitBuffer;
    private int bitCount;
    private ByteBuf in;

    Bzip2BitReader() {
    }

    boolean hasReadableBits(int i2) {
        if (i2 >= 0) {
            return this.bitCount >= i2 || ((this.in.readableBytes() << 3) & Integer.MAX_VALUE) >= i2 - this.bitCount;
        }
        throw new IllegalArgumentException("count: " + i2 + " (expected value greater than 0)");
    }

    boolean hasReadableBytes(int i2) {
        if (i2 >= 0 && i2 <= MAX_COUNT_OF_READABLE_BYTES) {
            return hasReadableBits(i2 << 3);
        }
        throw new IllegalArgumentException("count: " + i2 + " (expected: 0-" + MAX_COUNT_OF_READABLE_BYTES + ')');
    }

    boolean isReadable() {
        return this.bitCount > 0 || this.in.isReadable();
    }

    int readBits(int i2) {
        long unsignedByte;
        int i3;
        if (i2 < 0 || i2 > 32) {
            throw new IllegalArgumentException("count: " + i2 + " (expected: 0-32 )");
        }
        int i4 = this.bitCount;
        long j2 = this.bitBuffer;
        if (i4 < i2) {
            int i5 = this.in.readableBytes();
            if (i5 == 1) {
                unsignedByte = this.in.readUnsignedByte();
                i3 = 8;
            } else if (i5 == 2) {
                unsignedByte = this.in.readUnsignedShort();
                i3 = 16;
            } else if (i5 != 3) {
                unsignedByte = this.in.readUnsignedInt();
                i3 = 32;
            } else {
                unsignedByte = this.in.readUnsignedMedium();
                i3 = 24;
            }
            j2 = (j2 << i3) | unsignedByte;
            i4 += i3;
            this.bitBuffer = j2;
        }
        int i6 = i4 - i2;
        this.bitCount = i6;
        return (int) ((j2 >>> i6) & (i2 != 32 ? (1 << i2) - 1 : 4294967295L));
    }

    boolean readBoolean() {
        return readBits(1) != 0;
    }

    int readInt() {
        return readBits(32);
    }

    void refill() {
        this.bitBuffer = (this.bitBuffer << 8) | ((long) this.in.readUnsignedByte());
        this.bitCount += 8;
    }

    void setByteBuf(ByteBuf byteBuf) {
        this.in = byteBuf;
    }
}
