package io.netty.handler.codec.http2;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;

/* JADX INFO: loaded from: classes.dex */
public final class HpackHuffmanEncoder {
    private final int[] codes;
    private final EncodeProcessor encodeProcessor;
    private final EncodedLengthProcessor encodedLengthProcessor;
    private final byte[] lengths;

    public final class EncodeProcessor implements ByteProcessor {
        private long current;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        private int f5801n;
        public ByteBuf out;

        private EncodeProcessor() {
        }

        public void end() {
            try {
                int i2 = this.f5801n;
                if (i2 > 0) {
                    long j2 = this.current << (8 - i2);
                    this.current = j2;
                    long j3 = j2 | ((long) (DnsRecord.CLASS_ANY >>> i2));
                    this.current = j3;
                    this.out.writeByte((int) j3);
                }
            } finally {
                this.out = null;
                this.current = 0L;
                this.f5801n = 0;
            }
        }

        @Override // io.netty.util.ByteProcessor
        public boolean process(byte b2) {
            int i2 = b2 & 255;
            byte b3 = HpackHuffmanEncoder.this.lengths[i2];
            long j2 = this.current << b3;
            this.current = j2;
            this.current = j2 | ((long) HpackHuffmanEncoder.this.codes[i2]);
            this.f5801n += b3;
            while (true) {
                int i3 = this.f5801n;
                if (i3 < 8) {
                    return true;
                }
                int i4 = i3 - 8;
                this.f5801n = i4;
                this.out.writeByte((int) (this.current >> i4));
            }
        }
    }

    public final class EncodedLengthProcessor implements ByteProcessor {
        private long len;

        private EncodedLengthProcessor() {
        }

        public int length() {
            return (int) ((this.len + 7) >> 3);
        }

        @Override // io.netty.util.ByteProcessor
        public boolean process(byte b2) {
            this.len += (long) HpackHuffmanEncoder.this.lengths[b2 & 255];
            return true;
        }

        public void reset() {
            this.len = 0L;
        }
    }

    public HpackHuffmanEncoder() {
        this(HpackUtil.HUFFMAN_CODES, HpackUtil.HUFFMAN_CODE_LENGTHS);
    }

    private HpackHuffmanEncoder(int[] iArr, byte[] bArr) {
        this.encodedLengthProcessor = new EncodedLengthProcessor();
        this.encodeProcessor = new EncodeProcessor();
        this.codes = iArr;
        this.lengths = bArr;
    }

    private void encodeSlowPath(ByteBuf byteBuf, CharSequence charSequence) {
        long j2 = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < charSequence.length(); i3++) {
            int iCharAt = charSequence.charAt(i3) & 255;
            int i4 = this.codes[iCharAt];
            byte b2 = this.lengths[iCharAt];
            j2 = (j2 << b2) | ((long) i4);
            i2 += b2;
            while (i2 >= 8) {
                i2 -= 8;
                byteBuf.writeByte((int) (j2 >> i2));
            }
        }
        if (i2 > 0) {
            byteBuf.writeByte((int) (((long) (DnsRecord.CLASS_ANY >>> i2)) | (j2 << (8 - i2))));
        }
    }

    private int getEncodedLengthSlowPath(CharSequence charSequence) {
        long j2 = 0;
        for (int i2 = 0; i2 < charSequence.length(); i2++) {
            j2 += (long) this.lengths[charSequence.charAt(i2) & 255];
        }
        return (int) ((j2 + 7) >> 3);
    }

    public void encode(ByteBuf byteBuf, CharSequence charSequence) {
        ObjectUtil.checkNotNull(byteBuf, "out");
        if (!(charSequence instanceof AsciiString)) {
            encodeSlowPath(byteBuf, charSequence);
            return;
        }
        AsciiString asciiString = (AsciiString) charSequence;
        try {
            try {
                EncodeProcessor encodeProcessor = this.encodeProcessor;
                encodeProcessor.out = byteBuf;
                asciiString.forEachByte(encodeProcessor);
            } catch (Exception e2) {
                PlatformDependent.throwException(e2);
            }
        } finally {
            this.encodeProcessor.end();
        }
    }

    public int getEncodedLength(CharSequence charSequence) {
        if (!(charSequence instanceof AsciiString)) {
            return getEncodedLengthSlowPath(charSequence);
        }
        AsciiString asciiString = (AsciiString) charSequence;
        try {
            this.encodedLengthProcessor.reset();
            asciiString.forEachByte(this.encodedLengthProcessor);
            return this.encodedLengthProcessor.length();
        } catch (Exception e2) {
            PlatformDependent.throwException(e2);
            return -1;
        }
    }
}
