// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.oer;

import java.math.BigInteger;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.util.Arrays;

public class BitBuilder
{
    private static final byte[] bits;
    byte[] buf;
    int pos;
    
    public BitBuilder() {
        this.buf = new byte[1];
        this.pos = 0;
    }
    
    public BitBuilder writeBit(final int n) {
        if (this.pos / 8 >= this.buf.length) {
            final byte[] buf = new byte[this.buf.length + 4];
            System.arraycopy(this.buf, 0, buf, 0, this.pos / 8);
            Arrays.clear(this.buf);
            this.buf = buf;
        }
        if (n == 0) {
            final byte[] buf2 = this.buf;
            final int n2 = this.pos / 8;
            buf2[n2] &= (byte)~BitBuilder.bits[this.pos % 8];
        }
        else {
            final byte[] buf3 = this.buf;
            final int n3 = this.pos / 8;
            buf3[n3] |= BitBuilder.bits[this.pos % 8];
        }
        ++this.pos;
        return this;
    }
    
    public BitBuilder writeBits(final long n, final int n2) {
        for (int i = n2 - 1; i >= 0; --i) {
            this.writeBit(((n & 1L << i) > 0L) ? 1 : 0);
        }
        return this;
    }
    
    public BitBuilder writeBits(final long n, final int n2, final int n3) {
        for (int i = n2 - 1; i >= n2 - n3; --i) {
            this.writeBit(((n & 1L << i) != 0x0L) ? 1 : 0);
        }
        return this;
    }
    
    public int write(final OutputStream outputStream) throws IOException {
        final int len = (this.pos + this.pos % 8) / 8;
        outputStream.write(this.buf, 0, len);
        outputStream.flush();
        return len;
    }
    
    public int writeAndClear(final OutputStream outputStream) throws IOException {
        final int len = (this.pos + this.pos % 8) / 8;
        outputStream.write(this.buf, 0, len);
        outputStream.flush();
        this.zero();
        return len;
    }
    
    public void pad() {
        this.pos += this.pos % 8;
    }
    
    public void write7BitBytes(int n) {
        int n2 = 0;
        for (int i = 4; i >= 0; --i) {
            if (n2 == 0 && (n & 0xFE000000) != 0x0) {
                n2 = 1;
            }
            if (n2 != 0) {
                this.writeBit(i).writeBits(n, 32, 7);
            }
            n <<= 7;
        }
    }
    
    public void write7BitBytes(BigInteger shiftLeft) {
        final int n = (shiftLeft.bitLength() + shiftLeft.bitLength() % 8) / 8;
        final BigInteger shiftLeft2 = BigInteger.valueOf(254L).shiftLeft(n * 8);
        int n2 = 0;
        for (int i = n; i >= 0; --i) {
            if (n2 == 0 && shiftLeft.and(shiftLeft2).compareTo(BigInteger.ZERO) != 0) {
                n2 = 1;
            }
            if (n2 != 0) {
                this.writeBit(i).writeBits(shiftLeft.and(shiftLeft2).shiftRight(8 * n - 8).intValue(), 8, 7);
            }
            shiftLeft = shiftLeft.shiftLeft(7);
        }
    }
    
    public void zero() {
        Arrays.clear(this.buf);
        this.pos = 0;
    }
    
    static {
        bits = new byte[] { -128, 64, 32, 16, 8, 4, 2, 1 };
    }
}
