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

package org.bouncycastle.cms;

import org.bouncycastle.util.Arrays;
import java.io.IOException;
import java.io.InputStream;

public final class InputStreamWithMAC extends InputStream
{
    private final InputStream base;
    private MACProvider macProvider;
    private byte[] mac;
    private boolean baseFinished;
    private int index;
    
    InputStreamWithMAC(final InputStream base, final MACProvider macProvider) {
        this.base = base;
        this.macProvider = macProvider;
        this.baseFinished = false;
        this.index = 0;
    }
    
    public InputStreamWithMAC(final InputStream base, final byte[] mac) {
        this.base = base;
        this.mac = mac;
        this.baseFinished = false;
        this.index = 0;
    }
    
    @Override
    public int read() throws IOException {
        if (!this.baseFinished) {
            final int read = this.base.read();
            if (read < 0) {
                this.baseFinished = true;
                if (this.macProvider != null) {
                    this.macProvider.init();
                    this.mac = this.macProvider.getMAC();
                }
                return this.mac[this.index++] & 0xFF;
            }
            return read;
        }
        else {
            if (this.index >= this.mac.length) {
                return -1;
            }
            return this.mac[this.index++] & 0xFF;
        }
    }
    
    public byte[] getMAC() {
        if (!this.baseFinished) {
            throw new IllegalStateException("input stream not fully processed");
        }
        return Arrays.clone(this.mac);
    }
    
    @Override
    public int read(final byte[] b, final int n, final int n2) throws IOException {
        if (b == null) {
            throw new NullPointerException("input array is null");
        }
        if (n < 0 || b.length < n + n2) {
            throw new IndexOutOfBoundsException("invalid off(" + n + ") and len(" + n2 + ")");
        }
        if (!this.baseFinished) {
            final int read = this.base.read(b, n, n2);
            if (read >= 0) {
                return read;
            }
            this.baseFinished = true;
            if (this.macProvider != null) {
                this.macProvider.init();
                this.mac = this.macProvider.getMAC();
            }
            if (n2 >= this.mac.length) {
                System.arraycopy(this.mac, 0, b, n, this.mac.length);
                this.index = this.mac.length;
                return this.mac.length;
            }
            System.arraycopy(this.mac, 0, b, n, n2);
            this.index += n2;
            return n2;
        }
        else {
            if (this.index >= this.mac.length) {
                return -1;
            }
            if (n2 >= this.mac.length - this.index) {
                System.arraycopy(this.mac, this.index, b, n, this.mac.length - this.index);
                final int n3 = this.mac.length - this.index;
                this.index = this.mac.length;
                return n3;
            }
            System.arraycopy(this.mac, this.index, b, n, n2);
            this.index += n2;
            return n2;
        }
    }
}
