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

package org.bouncycastle.asn1;

import java.io.IOException;
import java.io.InputStream;

class ConstructedBitStream extends InputStream
{
    private final ASN1StreamParser _parser;
    private final boolean _octetAligned;
    private boolean _first;
    private int _padBits;
    private ASN1BitStringParser _currentParser;
    private InputStream _currentStream;
    
    ConstructedBitStream(final ASN1StreamParser parser, final boolean octetAligned) {
        this._first = true;
        this._padBits = 0;
        this._parser = parser;
        this._octetAligned = octetAligned;
    }
    
    int getPadBits() {
        return this._padBits;
    }
    
    @Override
    public int read(final byte[] b, final int n, final int n2) throws IOException {
        if (this._currentStream == null) {
            if (!this._first) {
                return -1;
            }
            this._currentParser = this.getNextParser();
            if (this._currentParser == null) {
                return -1;
            }
            this._first = false;
            this._currentStream = this._currentParser.getBitStream();
        }
        int n3 = 0;
        while (true) {
            final int read = this._currentStream.read(b, n + n3, n2 - n3);
            if (read >= 0) {
                n3 += read;
                if (n3 == n2) {
                    return n3;
                }
                continue;
            }
            else {
                this._padBits = this._currentParser.getPadBits();
                this._currentParser = this.getNextParser();
                if (this._currentParser == null) {
                    this._currentStream = null;
                    return (n3 < 1) ? -1 : n3;
                }
                this._currentStream = this._currentParser.getBitStream();
            }
        }
    }
    
    @Override
    public int read() throws IOException {
        if (this._currentStream == null) {
            if (!this._first) {
                return -1;
            }
            this._currentParser = this.getNextParser();
            if (this._currentParser == null) {
                return -1;
            }
            this._first = false;
            this._currentStream = this._currentParser.getBitStream();
        }
        while (true) {
            final int read = this._currentStream.read();
            if (read >= 0) {
                return read;
            }
            this._padBits = this._currentParser.getPadBits();
            this._currentParser = this.getNextParser();
            if (this._currentParser == null) {
                this._currentStream = null;
                return -1;
            }
            this._currentStream = this._currentParser.getBitStream();
        }
    }
    
    private ASN1BitStringParser getNextParser() throws IOException {
        final ASN1Encodable object = this._parser.readObject();
        if (object == null) {
            if (this._octetAligned && this._padBits != 0) {
                throw new IOException("expected octet-aligned bitstring, but found padBits: " + this._padBits);
            }
            return null;
        }
        else {
            if (!(object instanceof ASN1BitStringParser)) {
                throw new IOException("unknown object encountered: " + ((ASN1BitStringParser)object).getClass());
            }
            if (this._padBits != 0) {
                throw new IOException("only the last nested bitstring can have padding");
            }
            return (ASN1BitStringParser)object;
        }
    }
}
