package org.bouncycastle.apache.bzip2;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.Vector;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.crypto.digests.Blake2xsDigest;
import org.bouncycastle.crypto.hpke.HPKE;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Integers;

/* JADX INFO: loaded from: classes2.dex */
public class CBZip2OutputStream extends OutputStream implements BZip2Constants {
    protected static final int CLEARMASK = -2097153;
    protected static final int DEPTH_THRESH = 10;
    protected static final int GREATER_ICOST = 15;
    protected static final int LESSER_ICOST = 0;
    protected static final int SETMASK = 2097152;
    protected static final int SMALL_THRESH = 20;
    private final int allowableBlockSize;
    private byte[] blockBytes;
    private final CRC blockCRC;
    boolean blockRandomised;
    private final int blockSize100k;
    private final Vector blocksortStack;
    int bsBuff;
    int bsLivePos;
    private OutputStream bsStream;
    boolean closed;
    int count;
    private int currentByte;
    private boolean finished;
    private boolean firstAttempt;
    private int[] ftab;
    private boolean[] inUse;
    private int[] mtfFreq;
    private int nInUse;
    private int nMTF;
    int origPtr;
    private short[] quadrantShorts;
    private int runLength;
    private byte[] selectors;
    private int streamCRC;
    private int[] szptr;
    private int workDone;
    private int workFactor;
    private int workLimit;
    private int[] zptr;
    static final short[] R_NUMS = {619, 720, 127, 481, 931, 816, 813, 233, 566, 247, 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, 170, 607, 520, 932, 727, 476, 693, 425, 174, 647, 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, 161, 604, 958, 533, 221, 400, 386, 867, 600, 782, 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, 469, 68, 770, 919, 190, 373, 294, 822, 808, 206, 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, 652, 934, 970, 447, 318, 353, 859, 672, 112, 785, 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, 411, 521, 938, 300, 821, 78, 343, 175, 128, 250, 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, 944, 375, 748, 52, 600, 747, 642, 182, 862, 81, 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, 686, 754, 806, 760, 493, 403, 415, 394, 687, 700, 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, 707, 151, 457, 449, 797, 195, 791, 558, 945, 679, 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, 140, 206, 73, 263, 980, 736, 876, 478, 430, 305, 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, 896, 831, 547, 261, 524, 462, 293, 465, 502, 56, 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, 372, 567, 466, 434, 645, 210, 389, 550, 919, 135, 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, 936, 638};
    private static final int[] INCS = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161, 2391484};

    /* JADX INFO: loaded from: classes3.dex */
    private static class StackElem {
        int dd;
        int hh;
        int ll;

        private StackElem() {
        }
    }

    public CBZip2OutputStream(OutputStream outputStream) throws IOException {
        this(outputStream, 9);
    }

    /* JADX WARN: Code duplicated, block: B:4:0x004e A[PHI: r1
      0x004e: PHI (r1v11 int) = (r1v5 int), (r1v6 int) binds: [B:3:0x004c, B:6:0x0051] A[DONT_GENERATE, DONT_INLINE]] */
    public CBZip2OutputStream(OutputStream outputStream, int i) throws IOException {
        this.blocksortStack = new Vector();
        this.blockCRC = new CRC();
        this.inUse = new boolean[256];
        this.selectors = new byte[BZip2Constants.MAX_SELECTORS];
        this.mtfFreq = new int[BZip2Constants.MAX_ALPHA_SIZE];
        this.currentByte = -1;
        this.runLength = 0;
        this.closed = false;
        this.blockBytes = null;
        this.quadrantShorts = null;
        this.zptr = null;
        this.ftab = null;
        outputStream.write(66);
        outputStream.write(90);
        this.bsStream = outputStream;
        this.bsBuff = 0;
        this.bsLivePos = 32;
        this.workFactor = 50;
        int i2 = 9;
        if (i <= 9) {
            i2 = 1;
            i = i < 1 ? i2 : i;
        }
        this.blockSize100k = i;
        this.allowableBlockSize = (this.blockSize100k * BZip2Constants.baseBlockSize) - 20;
        int i3 = this.blockSize100k * BZip2Constants.baseBlockSize;
        int i4 = i3 + 1 + 20;
        this.blockBytes = new byte[i4];
        this.quadrantShorts = new short[i4];
        this.zptr = new int[i3];
        this.ftab = new int[65537];
        this.szptr = this.zptr;
        outputStream.write(PublicKeyAlgorithmTags.EXPERIMENTAL_5);
        outputStream.write(this.blockSize100k + 48);
        this.streamCRC = 0;
        initBlock();
    }

    private void bsFinishedWithStream() throws IOException {
        if (this.bsLivePos < 32) {
            this.bsStream.write(this.bsBuff >>> 24);
            this.bsBuff = 0;
            this.bsLivePos = 32;
        }
    }

    private void bsPutBit(int i) throws IOException {
        this.bsLivePos--;
        this.bsBuff = (i << this.bsLivePos) | this.bsBuff;
        if (this.bsLivePos <= 24) {
            this.bsStream.write(this.bsBuff >>> 24);
            this.bsBuff <<= 8;
            this.bsLivePos += 8;
        }
    }

    private void bsPutBits(int i, int i2) throws IOException {
        this.bsLivePos -= i;
        this.bsBuff |= i2 << this.bsLivePos;
        while (this.bsLivePos <= 24) {
            this.bsStream.write(this.bsBuff >>> 24);
            this.bsBuff <<= 8;
            this.bsLivePos += 8;
        }
    }

    private void bsPutBitsSmall(int i, int i2) throws IOException {
        this.bsLivePos -= i;
        this.bsBuff |= i2 << this.bsLivePos;
        if (this.bsLivePos <= 24) {
            this.bsStream.write(this.bsBuff >>> 24);
            this.bsBuff <<= 8;
            this.bsLivePos += 8;
        }
    }

    private void bsPutInt32(int i) throws IOException {
        bsPutBits(16, i >>> 16);
        bsPutBits(16, i & Blake2xsDigest.UNKNOWN_DIGEST_LENGTH);
    }

    private void bsPutLong48(long j) throws IOException {
        bsPutBits(24, ((int) (j >>> 24)) & 16777215);
        bsPutBits(24, ((int) j) & 16777215);
    }

    private void doReversibleTransformation() {
        this.workLimit = this.workFactor * (this.count - 1);
        this.workDone = 0;
        this.blockRandomised = false;
        this.firstAttempt = true;
        mainSort();
        if (this.workDone > this.workLimit && this.firstAttempt) {
            randomiseBlock();
            this.workDone = 0;
            this.workLimit = 0;
            this.blockRandomised = true;
            this.firstAttempt = false;
            mainSort();
        }
        this.origPtr = -1;
        for (int i = 0; i < this.count; i++) {
            if (this.zptr[i] == 0) {
                this.origPtr = i;
                break;
            }
        }
        if (this.origPtr == -1) {
            throw new IllegalStateException();
        }
    }

    private void endBlock() throws IOException {
        int i = this.blockCRC.getFinal();
        this.streamCRC = Integers.rotateLeft(this.streamCRC, 1) ^ i;
        doReversibleTransformation();
        bsPutLong48(54156738319193L);
        bsPutInt32(i);
        bsPutBit(this.blockRandomised ? 1 : 0);
        moveToFrontCodeAndSend();
    }

    private void endCompression() throws IOException {
        bsPutLong48(25779555029136L);
        bsPutInt32(this.streamCRC);
        bsFinishedWithStream();
    }

    private boolean fullGtU(int i, int i2) {
        int i3 = i + 1;
        int i4 = this.blockBytes[i3] & 255;
        int i5 = i2 + 1;
        int i6 = this.blockBytes[i5] & 255;
        if (i4 != i6) {
            return i4 > i6;
        }
        int i7 = i3 + 1;
        int i8 = this.blockBytes[i7] & 255;
        int i9 = i5 + 1;
        int i10 = this.blockBytes[i9] & 255;
        if (i8 != i10) {
            return i8 > i10;
        }
        int i11 = i7 + 1;
        int i12 = this.blockBytes[i11] & 255;
        int i13 = i9 + 1;
        int i14 = this.blockBytes[i13] & 255;
        if (i12 != i14) {
            return i12 > i14;
        }
        int i15 = i11 + 1;
        int i16 = this.blockBytes[i15] & 255;
        int i17 = i13 + 1;
        int i18 = this.blockBytes[i17] & 255;
        if (i16 != i18) {
            return i16 > i18;
        }
        int i19 = i15 + 1;
        int i20 = this.blockBytes[i19] & 255;
        int i21 = i17 + 1;
        int i22 = this.blockBytes[i21] & 255;
        if (i20 != i22) {
            return i20 > i22;
        }
        int i23 = i19 + 1;
        int i24 = this.blockBytes[i23] & 255;
        int i25 = i21 + 1;
        int i26 = this.blockBytes[i25] & 255;
        if (i24 != i26) {
            return i24 > i26;
        }
        int i27 = this.count;
        do {
            int i28 = i23 + 1;
            int i29 = this.blockBytes[i28] & 255;
            int i30 = i25 + 1;
            int i31 = this.blockBytes[i30] & 255;
            if (i29 != i31) {
                return i29 > i31;
            }
            int i32 = this.quadrantShorts[i28] & HPKE.aead_EXPORT_ONLY;
            int i33 = this.quadrantShorts[i30] & HPKE.aead_EXPORT_ONLY;
            if (i32 != i33) {
                return i32 > i33;
            }
            int i34 = i28 + 1;
            int i35 = this.blockBytes[i34] & 255;
            int i36 = i30 + 1;
            int i37 = this.blockBytes[i36] & 255;
            if (i35 != i37) {
                return i35 > i37;
            }
            int i38 = this.quadrantShorts[i34] & HPKE.aead_EXPORT_ONLY;
            int i39 = this.quadrantShorts[i36] & HPKE.aead_EXPORT_ONLY;
            if (i38 != i39) {
                return i38 > i39;
            }
            int i40 = i34 + 1;
            int i41 = this.blockBytes[i40] & 255;
            int i42 = i36 + 1;
            int i43 = this.blockBytes[i42] & 255;
            if (i41 != i43) {
                return i41 > i43;
            }
            int i44 = this.quadrantShorts[i40] & HPKE.aead_EXPORT_ONLY;
            int i45 = this.quadrantShorts[i42] & HPKE.aead_EXPORT_ONLY;
            if (i44 != i45) {
                return i44 > i45;
            }
            i23 = i40 + 1;
            int i46 = this.blockBytes[i23] & 255;
            i25 = i42 + 1;
            int i47 = this.blockBytes[i25] & 255;
            if (i46 != i47) {
                return i46 > i47;
            }
            int i48 = this.quadrantShorts[i23] & HPKE.aead_EXPORT_ONLY;
            int i49 = 65535 & this.quadrantShorts[i25];
            if (i48 != i49) {
                return i48 > i49;
            }
            if (i23 >= this.count) {
                i23 -= this.count;
            }
            if (i25 >= this.count) {
                i25 -= this.count;
            }
            i27 -= 4;
            this.workDone++;
        } while (i27 >= 0);
        return false;
    }

    private void generateMTFValues() {
        byte b;
        int i;
        int[] iArr;
        this.nInUse = 0;
        byte[] bArr = new byte[256];
        for (int i2 = 0; i2 < 256; i2++) {
            if (this.inUse[i2]) {
                int i3 = this.nInUse;
                this.nInUse = i3 + 1;
                bArr[i3] = (byte) i2;
            }
        }
        int i4 = this.nInUse + 1;
        for (int i5 = 0; i5 <= i4; i5++) {
            this.mtfFreq[i5] = 0;
        }
        int i6 = 0;
        int i7 = 0;
        for (int i8 = 0; i8 < this.count; i8++) {
            byte b2 = this.blockBytes[this.zptr[i8]];
            byte b3 = bArr[0];
            if (b2 == b3) {
                i7++;
            } else {
                int i9 = 1;
                while (true) {
                    b = bArr[i9];
                    i = i9 + 1;
                    bArr[i9] = b3;
                    if (b2 == b) {
                        break;
                    }
                    b3 = b;
                    i9 = i;
                }
                bArr[0] = b;
                while (true) {
                    iArr = this.szptr;
                    if (i7 <= 0) {
                        break;
                    }
                    int i10 = i7 - 1;
                    int i11 = i10 & 1;
                    int i12 = i6 + 1;
                    iArr[i6] = i11;
                    int[] iArr2 = this.mtfFreq;
                    iArr2[i11] = iArr2[i11] + 1;
                    i7 = i10 >>> 1;
                    i6 = i12;
                }
                int i13 = i6 + 1;
                iArr[i6] = i;
                int[] iArr3 = this.mtfFreq;
                iArr3[i] = iArr3[i] + 1;
                i6 = i13;
            }
        }
        while (true) {
            int[] iArr4 = this.szptr;
            if (i7 <= 0) {
                iArr4[i6] = i4;
                int[] iArr5 = this.mtfFreq;
                iArr5[i4] = iArr5[i4] + 1;
                this.nMTF = i6 + 1;
                return;
            }
            int i14 = i7 - 1;
            int i15 = i14 & 1;
            iArr4[i6] = i15;
            int[] iArr6 = this.mtfFreq;
            iArr6[i15] = iArr6[i15] + 1;
            i7 = i14 >>> 1;
            i6++;
        }
    }

    private void hbAssignCodes(int[] iArr, byte[] bArr, int i, int i2, int i3) {
        int i4 = 0;
        while (i <= i2) {
            for (int i5 = 0; i5 < i3; i5++) {
                if ((bArr[i5] & 255) == i) {
                    iArr[i5] = i4;
                    i4++;
                }
            }
            i4 <<= 1;
            i++;
        }
    }

    protected static void hbMakeCodeLengths(byte[] bArr, int[] iArr, int i, int i2) {
        int i3;
        int i4;
        int i5 = 260;
        int[] iArr2 = new int[260];
        int[] iArr3 = new int[516];
        int[] iArr4 = new int[516];
        int i6 = 0;
        int i7 = 0;
        while (true) {
            i3 = 1;
            if (i7 >= i) {
                break;
            }
            int i8 = i7 + 1;
            if (iArr[i7] != 0) {
                i3 = iArr[i7];
            }
            iArr3[i8] = i3 << 8;
            i7 = i8;
        }
        while (true) {
            iArr2[i6] = i6;
            iArr3[i6] = i6;
            iArr4[i6] = -2;
            int i9 = i6;
            for (int i10 = i3; i10 <= i; i10++) {
                iArr4[i10] = -1;
                i9++;
                iArr2[i9] = i10;
                int i11 = iArr2[i9];
                int i12 = i9;
                while (true) {
                    int i13 = i12 >> 1;
                    if (iArr3[i11] < iArr3[iArr2[i13]]) {
                        iArr2[i12] = iArr2[i13];
                        i12 = i13;
                    }
                }
                iArr2[i12] = i11;
            }
            if (i9 >= i5) {
                throw new IllegalStateException();
            }
            int i14 = i;
            while (i9 > i3) {
                int i15 = iArr2[i3];
                iArr2[i3] = iArr2[i9];
                int i16 = i9 - 1;
                int i17 = iArr2[i3];
                int i18 = i3;
                while (true) {
                    int i19 = i18 << 1;
                    if (i19 > i16) {
                        break;
                    }
                    if (i19 < i16) {
                        int i20 = i19 + 1;
                        if (iArr3[iArr2[i20]] < iArr3[iArr2[i19]]) {
                            i19 = i20;
                        }
                    }
                    if (iArr3[i17] < iArr3[iArr2[i19]]) {
                        break;
                    }
                    iArr2[i18] = iArr2[i19];
                    i18 = i19;
                }
                iArr2[i18] = i17;
                int i21 = iArr2[i3];
                iArr2[i3] = iArr2[i16];
                int i22 = i16 - 1;
                int i23 = iArr2[i3];
                int i24 = i3;
                while (true) {
                    int i25 = i24 << 1;
                    if (i25 > i22) {
                        i4 = i3;
                        break;
                    }
                    if (i25 < i22) {
                        int i26 = i25 + 1;
                        i4 = i3;
                        if (iArr3[iArr2[i26]] < iArr3[iArr2[i25]]) {
                            i25 = i26;
                        }
                    } else {
                        i4 = i3;
                    }
                    if (iArr3[i23] < iArr3[iArr2[i25]]) {
                        break;
                    }
                    iArr2[i24] = iArr2[i25];
                    i24 = i25;
                    i3 = i4;
                }
                iArr2[i24] = i23;
                i14++;
                iArr4[i21] = i14;
                iArr4[i15] = i14;
                iArr3[i14] = ((((iArr3[i15] & 255) > (iArr3[i21] & 255) ? iArr3[i15] : iArr3[i21]) & 255) + 1) | ((iArr3[i15] & (-256)) + (iArr3[i21] & (-256)));
                iArr4[i14] = -1;
                i9 = i22 + 1;
                iArr2[i9] = i14;
                int i27 = iArr2[i9];
                int i28 = i9;
                while (true) {
                    int i29 = i28 >> 1;
                    if (iArr3[i27] < iArr3[iArr2[i29]]) {
                        iArr2[i28] = iArr2[i29];
                        i28 = i29;
                    }
                }
                iArr2[i28] = i27;
                i3 = i4;
            }
            int i30 = i3;
            if (i14 >= 516) {
                throw new IllegalStateException();
            }
            int i31 = 0;
            for (int i32 = i30; i32 <= i; i32++) {
                int i33 = i32;
                int i34 = 0;
                while (iArr4[i33] >= 0) {
                    i33 = iArr4[i33];
                    i34++;
                }
                bArr[i32 - 1] = (byte) i34;
                i31 |= i2 - i34;
            }
            if (i31 >= 0) {
                return;
            }
            for (int i35 = i30; i35 <= i; i35++) {
                iArr3[i35] = (((iArr3[i35] >> 8) / 2) + 1) << 8;
            }
            i3 = i30;
            i5 = 260;
            i6 = 0;
        }
    }

    private void initBlock() {
        this.blockCRC.initialise();
        this.count = 0;
        for (int i = 0; i < 256; i++) {
            this.inUse[i] = false;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r15v3, types: [int] */
    /* JADX WARN: Type inference failed for: r17v0 */
    /* JADX WARN: Type inference failed for: r17v1 */
    /* JADX WARN: Type inference failed for: r17v2 */
    /* JADX WARN: Type inference failed for: r17v4 */
    /* JADX WARN: Type inference failed for: r8v15, types: [int] */
    /* JADX WARN: Type inference failed for: r8v18 */
    /* JADX WARN: Type inference failed for: r8v22 */
    /* JADX WARN: Type inference failed for: r8v4 */
    private void mainSort() {
        char c;
        boolean z;
        byte[] bArr;
        int i;
        ?? r17;
        int i2;
        int[] iArr;
        int[] iArr2 = new int[256];
        int[] iArr3 = new int[256];
        boolean[] zArr = new boolean[256];
        int i3 = 0;
        int i4 = 0;
        while (true) {
            c = 20;
            z = true;
            if (i4 >= 20) {
                break;
            }
            this.blockBytes[this.count + i4 + 1] = this.blockBytes[(i4 % this.count) + 1];
            i4++;
        }
        for (int i5 = 0; i5 <= this.count + 20; i5++) {
            this.quadrantShorts[i5] = 0;
        }
        this.blockBytes[0] = this.blockBytes[this.count];
        if (this.count <= 4000) {
            for (int i6 = 0; i6 < this.count; i6++) {
                this.zptr[i6] = i6;
            }
            this.firstAttempt = false;
            this.workLimit = 0;
            this.workDone = 0;
            simpleSort(0, this.count - 1, 0);
            return;
        }
        for (int i7 = 0; i7 <= 255; i7++) {
            zArr[i7] = false;
        }
        for (int i8 = 0; i8 <= 65536; i8++) {
            this.ftab[i8] = 0;
        }
        int i9 = this.blockBytes[0] & 255;
        int i10 = 0;
        while (i10 < this.count) {
            i10++;
            int i11 = this.blockBytes[i10] & 255;
            int[] iArr4 = this.ftab;
            int i12 = (i9 << 8) + i11;
            iArr4[i12] = iArr4[i12] + 1;
            i9 = i11;
        }
        for (int i13 = 1; i13 <= 65536; i13++) {
            int[] iArr5 = this.ftab;
            iArr5[i13] = iArr5[i13] + this.ftab[i13 - 1];
        }
        int i14 = this.blockBytes[1] & 255;
        int i15 = 0;
        while (true) {
            int i16 = this.count - 1;
            bArr = this.blockBytes;
            if (i15 >= i16) {
                break;
            }
            int i17 = bArr[i15 + 2] & 255;
            int i18 = (i14 << 8) + i17;
            int[] iArr6 = this.ftab;
            iArr6[i18] = iArr6[i18] - 1;
            this.zptr[this.ftab[i18]] = i15;
            i15++;
            i14 = i17;
        }
        int i19 = ((bArr[this.count] & 255) << 8) + (this.blockBytes[1] & 255);
        int[] iArr7 = this.ftab;
        iArr7[i19] = iArr7[i19] - 1;
        this.zptr[this.ftab[i19]] = this.count - 1;
        for (int i20 = 0; i20 <= 255; i20++) {
            iArr2[i20] = i20;
        }
        int i21 = 1;
        do {
            i21 = (i21 * 3) + 1;
        } while (i21 <= 256);
        do {
            i21 /= 3;
            for (int i22 = i21; i22 <= 255; i22++) {
                int i23 = iArr2[i22];
                int i24 = i22;
                while (true) {
                    int i25 = i24 - i21;
                    if (this.ftab[(iArr2[i25] + 1) << 8] - this.ftab[iArr2[i25] << 8] <= this.ftab[(i23 + 1) << 8] - this.ftab[i23 << 8]) {
                        break;
                    }
                    iArr2[i24] = iArr2[i25];
                    if (i25 <= i21 - 1) {
                        i24 = i25;
                        break;
                    }
                    i24 = i25;
                }
                iArr2[i24] = i23;
            }
        } while (i21 != 1);
        int i26 = 0;
        while (i26 <= 255) {
            int i27 = iArr2[i26];
            int i28 = i3;
            while (true) {
                i = 2097152;
                if (i28 <= 255) {
                    int i29 = (i27 << 8) + i28;
                    if ((this.ftab[i29] & 2097152) != 2097152) {
                        int i30 = this.ftab[i29] & CLEARMASK;
                        int i31 = (CLEARMASK & this.ftab[i29 + 1]) - (z ? 1 : 0);
                        if (i31 > i30) {
                            qSort3(i30, i31, 2);
                            if (this.workDone > this.workLimit && this.firstAttempt) {
                                return;
                            }
                        }
                        int[] iArr8 = this.ftab;
                        iArr8[i29] = 2097152 | iArr8[i29];
                    }
                    i28++;
                }
            }
            zArr[i27] = z;
            if (i26 < 255) {
                int i32 = this.ftab[i27 << 8] & CLEARMASK;
                int i33 = (this.ftab[(i27 + 1) << 8] & CLEARMASK) - i32;
                int i34 = i3;
                while ((i33 >> i34) > 65534) {
                    i34++;
                }
                int i35 = 0;
                ?? r8 = z;
                while (i35 < i33) {
                    ?? r15 = this.zptr[i32 + i35] + r8;
                    ?? r18 = r8;
                    short s = (short) (i35 >> i34);
                    int i36 = i;
                    this.quadrantShorts[r15] = s;
                    if (r15 <= c) {
                        this.quadrantShorts[r15 + this.count] = s;
                    }
                    i35++;
                    r8 = r18 == true ? 1 : 0;
                    i = i36;
                    c = 20;
                }
                r17 = r8;
                i2 = i;
                if (((i33 - 1) >> i34) > 65535) {
                    throw new IllegalStateException();
                }
            } else {
                r17 = z ? 1 : 0;
                i2 = 2097152;
            }
            int i37 = 0;
            while (true) {
                iArr = this.ftab;
                if (i37 > 255) {
                    break;
                }
                iArr3[i37] = iArr[(i37 << 8) + i27] & CLEARMASK;
                i37++;
            }
            for (int i38 = iArr[i27 << 8] & CLEARMASK; i38 < (this.ftab[(i27 + 1) << 8] & CLEARMASK); i38++) {
                int i39 = this.blockBytes[this.zptr[i38]] & 255;
                if (!zArr[i39]) {
                    this.zptr[iArr3[i39]] = (this.zptr[i38] == 0 ? this.count : this.zptr[i38]) - 1;
                    iArr3[i39] = iArr3[i39] + 1;
                }
            }
            for (int i40 = 0; i40 <= 255; i40++) {
                int[] iArr9 = this.ftab;
                int i41 = (i40 << 8) + i27;
                iArr9[i41] = iArr9[i41] | i2;
            }
            i26++;
            z = r17;
            i3 = 0;
            c = 20;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:9:0x000b, code lost:
    
        if (r3 > r2) goto L10;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private int med3(int r1, int r2, int r3) {
        /*
            r0 = this;
            if (r1 <= r2) goto L8
            if (r3 >= r2) goto L5
            goto Ld
        L5:
            if (r3 <= r1) goto Lf
            goto L10
        L8:
            if (r3 >= r1) goto Lb
            goto L10
        Lb:
            if (r3 <= r2) goto Lf
        Ld:
            r1 = r2
            goto L10
        Lf:
            r1 = r3
        L10:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.apache.bzip2.CBZip2OutputStream.med3(int, int, int):int");
    }

    private void moveToFrontCodeAndSend() throws IOException {
        bsPutBits(24, this.origPtr);
        generateMTFValues();
        sendMTFValues();
    }

    private static void pushStackElem(Vector vector, int i, int i2, int i3, int i4) {
        StackElem stackElem;
        if (i < vector.size()) {
            stackElem = (StackElem) vector.elementAt(i);
        } else {
            StackElem stackElem2 = new StackElem();
            vector.addElement(stackElem2);
            stackElem = stackElem2;
        }
        stackElem.ll = i2;
        stackElem.hh = i3;
        stackElem.dd = i4;
    }

    private void qSort3(int i, int i2, int i3) {
        Vector vector = this.blocksortStack;
        int i4 = i2;
        int i5 = i3;
        int i6 = 0;
        int i7 = i;
        while (true) {
            int i8 = 1;
            if (i4 - i7 < 20 || i5 > 10) {
                simpleSort(i7, i4, i5);
                if (i6 < 1) {
                    return;
                }
                if (this.workDone > this.workLimit && this.firstAttempt) {
                    return;
                }
                i6--;
                StackElem stackElem = (StackElem) vector.elementAt(i6);
                int i9 = stackElem.ll;
                int i10 = stackElem.hh;
                i5 = stackElem.dd;
                i7 = i9;
                i4 = i10;
            } else {
                int i11 = i5 + 1;
                int iMed3 = med3(this.blockBytes[this.zptr[i7] + i11] & 255, this.blockBytes[this.zptr[i4] + i11] & 255, this.blockBytes[this.zptr[(i7 + i4) >>> 1] + i11] & 255);
                int i12 = i7;
                int i13 = i12;
                int i14 = i4;
                int i15 = i14;
                while (true) {
                    if (i12 <= i14) {
                        int i16 = this.zptr[i12];
                        int i17 = (this.blockBytes[i16 + i11] & 255) - iMed3;
                        if (i17 <= 0) {
                            if (i17 == 0) {
                                this.zptr[i12] = this.zptr[i13];
                                this.zptr[i13] = i16;
                                i13++;
                            }
                            i12++;
                        }
                    }
                    while (i12 <= i14) {
                        int i18 = this.zptr[i14];
                        int i19 = (this.blockBytes[i18 + i11] & 255) - iMed3;
                        if (i19 < 0) {
                            break;
                        }
                        if (i19 == 0) {
                            this.zptr[i14] = this.zptr[i15];
                            this.zptr[i15] = i18;
                            i15--;
                        }
                        i14--;
                    }
                    if (i12 > i14) {
                        break;
                    }
                    int i20 = this.zptr[i12];
                    this.zptr[i12] = this.zptr[i14];
                    this.zptr[i14] = i20;
                    i14--;
                    i12++;
                    i8 = 1;
                }
                if (i15 < i13) {
                    i5 = i11;
                } else {
                    int i21 = i13 - i7;
                    int i22 = i12 - i13;
                    int iMin = Math.min(i21, i22);
                    vswap(i7, i12 - iMin, iMin);
                    int i23 = i4 - i15;
                    int i24 = i15 - i14;
                    int iMin2 = Math.min(i23, i24);
                    vswap(i12, (i4 - iMin2) + i8, iMin2);
                    int i25 = i22 + i7;
                    int i26 = i4 - i24;
                    int i27 = i6 + 1;
                    pushStackElem(vector, i6, i7, i25 - 1, i5);
                    i6 = i27 + 1;
                    pushStackElem(vector, i27, i25, i26, i11);
                    i7 = i26 + 1;
                }
            }
        }
    }

    private void randomiseBlock() {
        for (int i = 0; i < 256; i++) {
            this.inUse[i] = false;
        }
        int i2 = 0;
        int i3 = 0;
        int i4 = 1;
        while (true) {
            int i5 = i2;
            if (i4 > this.count) {
                return;
            }
            if (i2 == 0) {
                short s = R_NUMS[i3];
                i3 = (i3 + 1) & 511;
                i5 = s;
            }
            int i6 = i5 - 1;
            byte[] bArr = this.blockBytes;
            bArr[i4] = (byte) (bArr[i4] ^ (i6 == 1 ? (byte) 1 : (byte) 0));
            this.inUse[this.blockBytes[i4] & 255] = true;
            i4++;
            i2 = i6;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r11v0 */
    /* JADX WARN: Type inference failed for: r11v1 */
    /* JADX WARN: Type inference failed for: r11v14, types: [int] */
    /* JADX WARN: Type inference failed for: r11v15, types: [int] */
    /* JADX WARN: Type inference failed for: r11v16 */
    /* JADX WARN: Type inference failed for: r11v17, types: [int] */
    /* JADX WARN: Type inference failed for: r11v19 */
    /* JADX WARN: Type inference failed for: r11v2, types: [int] */
    /* JADX WARN: Type inference failed for: r11v20 */
    /* JADX WARN: Type inference failed for: r11v21 */
    /* JADX WARN: Type inference failed for: r11v22 */
    /* JADX WARN: Type inference failed for: r13v2 */
    /* JADX WARN: Type inference failed for: r13v21 */
    /* JADX WARN: Type inference failed for: r13v3, types: [int] */
    /* JADX WARN: Type inference failed for: r35v0, types: [org.bouncycastle.apache.bzip2.CBZip2OutputStream] */
    private void sendMTFValues() throws IOException {
        int i;
        byte b;
        int i2;
        int[][] iArr;
        int i3;
        int i4;
        byte b2;
        int i5 = 2;
        int i6 = this.nInUse + 2;
        if (this.nMTF <= 0) {
            throw new IllegalStateException();
        }
        char c = 4;
        int i7 = 6;
        if (this.nMTF < 200) {
            i = 2;
        } else if (this.nMTF < 600) {
            i = 3;
        } else if (this.nMTF < 1200) {
            i = 4;
        } else {
            i = this.nMTF < 2400 ? 5 : 6;
        }
        int i8 = 1;
        ?? r11 = 0;
        byte[][] bArr = (byte[][]) Array.newInstance((Class<?>) Byte.TYPE, i, i6);
        int i9 = 0;
        while (true) {
            b = 15;
            if (i9 >= i) {
                break;
            }
            Arrays.fill(bArr[i9], (byte) 15);
            i9++;
        }
        int i10 = this.nMTF;
        int i11 = -1;
        int i12 = i;
        while (i12 > 0) {
            int i13 = i11 + 1;
            int i14 = i10 / i12;
            byte b3 = r11;
            ?? r12 = r11;
            while (true) {
                if (r12 >= i14) {
                    b2 = b;
                    break;
                }
                b2 = b;
                if (i11 >= i6 - 1) {
                    break;
                }
                i11++;
                b = b2;
                r12 += this.mtfFreq[i11];
            }
            if (i11 > i13 && i12 != i && i12 != 1 && (i - i12) % 2 == 1) {
                r12 -= this.mtfFreq[i11];
                i11--;
            }
            byte[] bArr2 = bArr[i12 - 1];
            for (int i15 = b3; i15 < i6; i15++) {
                if (i15 < i13 || i15 > i11) {
                    bArr2[i15] = b2;
                } else {
                    bArr2[i15] = b3;
                }
            }
            i12--;
            i10 -= r12;
            r11 = b3;
            b = b2;
        }
        boolean z = r11;
        byte b4 = b;
        int[] iArr2 = new int[2];
        iArr2[1] = 258;
        iArr2[z ? 1 : 0] = 6;
        int[][] iArr3 = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, iArr2);
        int[] iArr4 = new int[6];
        short[] sArr = new short[6];
        ?? r13 = r11;
        ?? r14 = r11;
        while (r14 < c) {
            int i16 = z ? 1 : 0;
            while (i16 < i) {
                iArr4[i16] = z ? 1 : 0;
                int[] iArr5 = iArr3[i16];
                char c2 = c;
                for (int i17 = z ? 1 : 0; i17 < i6; i17++) {
                    iArr5[i17] = z ? 1 : 0;
                }
                i16++;
                c = c2;
            }
            char c3 = c;
            int i18 = z ? 1 : 0;
            int i19 = i18;
            while (i18 < this.nMTF) {
                int iMin = Math.min((i18 + 50) - i8, this.nMTF - i8);
                if (i == i7) {
                    byte[] bArr3 = bArr[z ? 1 : 0];
                    byte[] bArr4 = bArr[i8];
                    byte[] bArr5 = bArr[i5];
                    byte[] bArr6 = bArr[3];
                    byte[] bArr7 = bArr[c3];
                    byte[] bArr8 = bArr[5];
                    int i20 = i18;
                    short s = z ? 1 : 0;
                    short s2 = s;
                    short s3 = s2;
                    short s4 = s3;
                    short s5 = s4;
                    short s6 = s5;
                    while (i20 <= iMin) {
                        int i21 = i8;
                        int i22 = this.szptr[i20];
                        int i23 = i5;
                        short s7 = (short) (s + (bArr3[i22] & 255));
                        short s8 = (short) (s2 + (bArr4[i22] & 255));
                        int[][] iArr6 = iArr3;
                        short s9 = (short) (s3 + (bArr5[i22] & 255));
                        short s10 = (short) (s4 + (bArr6[i22] & 255));
                        i20++;
                        s5 = (short) (s5 + (bArr7[i22] & 255));
                        s6 = (short) (s6 + (bArr8[i22] & 255));
                        s3 = s9;
                        s4 = s10;
                        i8 = i21;
                        iArr3 = iArr6;
                        s = s7;
                        s2 = s8;
                        i5 = i23;
                    }
                    iArr = iArr3;
                    i3 = i5;
                    i4 = i8;
                    sArr[z ? 1 : 0] = s;
                    sArr[i4] = s2;
                    sArr[i3] = s3;
                    sArr[3] = s4;
                    sArr[c3] = s5;
                    sArr[5] = s6;
                } else {
                    iArr = iArr3;
                    i3 = i5;
                    i4 = i8;
                    for (int i24 = z ? 1 : 0; i24 < i; i24++) {
                        sArr[i24] = z ? (short) 1 : (short) 0;
                    }
                    for (int i25 = i18; i25 <= iMin; i25++) {
                        int i26 = this.szptr[i25];
                        for (int i27 = z ? 1 : 0; i27 < i; i27++) {
                            sArr[i27] = (short) (sArr[i27] + (bArr[i27][i26] & 255));
                        }
                    }
                }
                short s11 = sArr[z ? 1 : 0];
                int i28 = z ? 1 : 0;
                for (int i29 = i4; i29 < i; i29++) {
                    short s12 = sArr[i29];
                    if (s12 < s11) {
                        i28 = i29;
                        s11 = s12;
                    }
                }
                iArr4[i28 == true ? 1 : 0] = iArr4[i28 == true ? 1 : 0] + 1;
                this.selectors[i19 == true ? 1 : 0] = i28 == true ? (byte) 1 : (byte) 0;
                int i30 = (i19 == true ? 1 : 0) + 1;
                int[] iArr7 = iArr[i28 == true ? 1 : 0];
                while (i18 <= iMin) {
                    int i31 = this.szptr[i18];
                    iArr7[i31] = iArr7[i31] + 1;
                    i18++;
                }
                i18 = iMin + 1;
                i8 = i4;
                i5 = i3;
                iArr3 = iArr;
                i7 = 6;
                i19 = i30;
            }
            int[][] iArr8 = iArr3;
            int i32 = i5;
            int i33 = i8;
            for (int i34 = z ? 1 : 0; i34 < i; i34++) {
                hbMakeCodeLengths(bArr[i34], iArr8[i34], i6, 17);
            }
            c = c3;
            i8 = i33;
            i5 = i32;
            iArr3 = iArr8;
            i7 = 6;
            r14++;
            r13 = i19;
        }
        int i35 = i5;
        int i36 = i8;
        if (r13 >= 32768 || r13 > 18002) {
            throw new IllegalStateException();
        }
        int[] iArr9 = new int[i35];
        iArr9[i36] = i6;
        iArr9[z ? 1 : 0] = i;
        int[][] iArr10 = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, iArr9);
        int i37 = z ? 1 : 0;
        while (i37 < i) {
            byte[] bArr9 = bArr[i37];
            int iMin2 = 32;
            int i38 = z ? 1 : 0;
            int iMax = i38;
            while (i38 < i6) {
                int i39 = bArr9[i38] & 255;
                iMax = Math.max(iMax, i39);
                iMin2 = Math.min(iMin2, i39);
                i38++;
            }
            if (((iMin2 < i36 ? true : z ? 1 : 0) | (iMax > 17 ? true : z ? 1 : 0)) != 0) {
                throw new IllegalStateException();
            }
            hbAssignCodes(iArr10[i37], bArr9, iMin2, iMax, i6);
            i37++;
            i36 = 1;
        }
        boolean[] zArr = new boolean[16];
        for (int i40 = z ? 1 : 0; i40 < 16; i40++) {
            zArr[i40] = z;
            int i41 = i40 * 16;
            for (int i42 = z ? 1 : 0; i42 < 16; i42++) {
                if (this.inUse[i41 + i42]) {
                    zArr[i40] = true;
                    break;
                }
            }
        }
        for (int i43 = z ? 1 : 0; i43 < 16; i43++) {
            bsPutBit(zArr[i43] ? 1 : 0);
        }
        for (int i44 = z ? 1 : 0; i44 < 16; i44++) {
            if (zArr[i44]) {
                int i45 = i44 * 16;
                for (int i46 = z ? 1 : 0; i46 < 16; i46++) {
                    bsPutBit(this.inUse[i45 + i46] ? 1 : 0);
                }
            }
        }
        bsPutBitsSmall(3, i);
        bsPutBits(b4, r13);
        int i47 = 6636321;
        for (int i48 = z ? 1 : 0; i48 < r13; i48++) {
            int i49 = (this.selectors[i48] & 255) << 2;
            int i50 = (i47 >>> i49) & b4;
            if (i50 != 1) {
                i47 = (i47 - (i50 << i49)) + ((8947848 & ((8947848 - i47) + (1118481 * i50))) >>> 3);
                i2 = 1;
            } else {
                i2 = 1;
            }
            bsPutBitsSmall(i50, (i2 << i50) - 2);
        }
        for (int i51 = z ? 1 : 0; i51 < i; i51++) {
            byte[] bArr10 = bArr[i51];
            int i52 = bArr10[z ? 1 : 0] & 255;
            bsPutBitsSmall(6, i52 << 1);
            int i53 = i52;
            for (int i54 = 1; i54 < i6; i54++) {
                int i55 = bArr10[i54] & 255;
                while (i53 < i55) {
                    bsPutBitsSmall(2, 2);
                    i53++;
                }
                while (i53 > i55) {
                    bsPutBitsSmall(2, 3);
                    i53--;
                }
                bsPutBit(z ? 1 : 0);
            }
        }
        int i56 = z ? 1 : 0;
        int i57 = i56;
        while (i57 < this.nMTF) {
            int iMin3 = Math.min((i57 + 50) - 1, this.nMTF - 1);
            int i58 = this.selectors[i56] & 255;
            byte[] bArr11 = bArr[i58];
            int[] iArr11 = iArr10[i58];
            while (i57 <= iMin3) {
                int i59 = this.szptr[i57];
                bsPutBits(bArr11[i59] & 255, iArr11[i59]);
                i57++;
            }
            i57 = iMin3 + 1;
            i56++;
        }
        if (i56 != r13) {
            throw new IllegalStateException();
        }
    }

    private void simpleSort(int i, int i2, int i3) {
        int i4 = (i2 - i) + 1;
        if (i4 < 2) {
            return;
        }
        int i5 = 0;
        while (INCS[i5] < i4) {
            i5++;
        }
        while (true) {
            i5--;
            if (i5 < 0) {
                return;
            }
            int i6 = INCS[i5];
            int i7 = i + i6;
            int i8 = i7;
            while (i8 <= i2) {
                int i9 = this.zptr[i8];
                int i10 = i8;
                while (true) {
                    int i11 = i10 - i6;
                    if (!fullGtU(this.zptr[i11] + i3, i9 + i3)) {
                        break;
                    }
                    this.zptr[i10] = this.zptr[i11];
                    if (i11 <= i7 - 1) {
                        i10 = i11;
                        break;
                    }
                    i10 = i11;
                }
                this.zptr[i10] = i9;
                int i12 = i8 + 1;
                if (i12 > i2) {
                    break;
                }
                int i13 = this.zptr[i12];
                int i14 = i12;
                while (true) {
                    int i15 = i14 - i6;
                    if (!fullGtU(this.zptr[i15] + i3, i13 + i3)) {
                        break;
                    }
                    this.zptr[i14] = this.zptr[i15];
                    if (i15 <= i7 - 1) {
                        i14 = i15;
                        break;
                    }
                    i14 = i15;
                }
                this.zptr[i14] = i13;
                int i16 = i12 + 1;
                if (i16 > i2) {
                    break;
                }
                int i17 = this.zptr[i16];
                int i18 = i16;
                while (true) {
                    int i19 = i18 - i6;
                    if (!fullGtU(this.zptr[i19] + i3, i17 + i3)) {
                        break;
                    }
                    this.zptr[i18] = this.zptr[i19];
                    if (i19 <= i7 - 1) {
                        i18 = i19;
                        break;
                    }
                    i18 = i19;
                }
                this.zptr[i18] = i17;
                i8 = i16 + 1;
                if (this.workDone > this.workLimit && this.firstAttempt) {
                    return;
                }
            }
        }
    }

    private void vswap(int i, int i2, int i3) {
        while (true) {
            i3--;
            if (i3 < 0) {
                return;
            }
            int i4 = this.zptr[i];
            this.zptr[i] = this.zptr[i2];
            this.zptr[i2] = i4;
            i2++;
            i++;
        }
    }

    private void writeRun() throws IOException {
        if (this.count > this.allowableBlockSize) {
            endBlock();
            initBlock();
        }
        this.inUse[this.currentByte] = true;
        switch (this.runLength) {
            case 1:
                byte[] bArr = this.blockBytes;
                int i = this.count + 1;
                this.count = i;
                bArr[i] = (byte) this.currentByte;
                this.blockCRC.update(this.currentByte);
            case 2:
                byte[] bArr2 = this.blockBytes;
                int i2 = this.count + 1;
                this.count = i2;
                bArr2[i2] = (byte) this.currentByte;
                byte[] bArr3 = this.blockBytes;
                int i3 = this.count + 1;
                this.count = i3;
                bArr3[i3] = (byte) this.currentByte;
                break;
            case 3:
                byte[] bArr4 = this.blockBytes;
                int i4 = this.count + 1;
                this.count = i4;
                bArr4[i4] = (byte) this.currentByte;
                byte[] bArr5 = this.blockBytes;
                int i5 = this.count + 1;
                this.count = i5;
                bArr5[i5] = (byte) this.currentByte;
                byte[] bArr6 = this.blockBytes;
                int i6 = this.count + 1;
                this.count = i6;
                bArr6[i6] = (byte) this.currentByte;
                this.blockCRC.update(this.currentByte);
                break;
            default:
                byte[] bArr7 = this.blockBytes;
                int i7 = this.count + 1;
                this.count = i7;
                bArr7[i7] = (byte) this.currentByte;
                byte[] bArr8 = this.blockBytes;
                int i8 = this.count + 1;
                this.count = i8;
                bArr8[i8] = (byte) this.currentByte;
                byte[] bArr9 = this.blockBytes;
                int i9 = this.count + 1;
                this.count = i9;
                bArr9[i9] = (byte) this.currentByte;
                byte[] bArr10 = this.blockBytes;
                int i10 = this.count + 1;
                this.count = i10;
                bArr10[i10] = (byte) this.currentByte;
                byte[] bArr11 = this.blockBytes;
                int i11 = this.count + 1;
                this.count = i11;
                bArr11[i11] = (byte) (this.runLength - 4);
                this.inUse[this.runLength - 4] = true;
                this.blockCRC.updateRun(this.currentByte, this.runLength);
                return;
        }
        this.blockCRC.update(this.currentByte);
        this.blockCRC.update(this.currentByte);
    }

    @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.closed) {
            return;
        }
        finish();
        this.closed = true;
        super.close();
        this.bsStream.close();
    }

    public void finish() throws IOException {
        if (this.finished) {
            return;
        }
        if (this.runLength > 0) {
            writeRun();
        }
        this.currentByte = -1;
        if (this.count > 0) {
            endBlock();
        }
        endCompression();
        this.finished = true;
        flush();
    }

    @Override // java.io.OutputStream, java.io.Flushable
    public void flush() throws IOException {
        super.flush();
        this.bsStream.flush();
    }

    @Override // java.io.OutputStream
    public void write(int i) throws IOException {
        int i2 = i & 255;
        if (this.currentByte != i2) {
            if (this.currentByte >= 0) {
                writeRun();
            }
            this.currentByte = i2;
            this.runLength = 1;
            return;
        }
        int i3 = this.runLength + 1;
        this.runLength = i3;
        if (i3 > 254) {
            writeRun();
            this.currentByte = -1;
            this.runLength = 0;
        }
    }
}
