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

package org.bouncycastle.operator;

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

public class MacCaptureStream extends OutputStream
{
    private final OutputStream cOut;
    private final byte[] mac;
    int macIndex;
    
    public MacCaptureStream(final OutputStream cOut, final int n) {
        this.macIndex = 0;
        this.cOut = cOut;
        this.mac = new byte[n];
    }
    
    @Override
    public void write(final byte[] b, final int off, final int n) throws IOException {
        if (n >= this.mac.length) {
            this.cOut.write(this.mac, 0, this.macIndex);
            this.macIndex = this.mac.length;
            System.arraycopy(b, off + n - this.mac.length, this.mac, 0, this.mac.length);
            this.cOut.write(b, off, n - this.mac.length);
        }
        else {
            for (int i = 0; i != n; ++i) {
                this.write(b[off + i]);
            }
        }
    }
    
    @Override
    public void write(final int n) throws IOException {
        if (this.macIndex == this.mac.length) {
            final byte b = this.mac[0];
            System.arraycopy(this.mac, 1, this.mac, 0, this.mac.length - 1);
            this.mac[this.mac.length - 1] = (byte)n;
            this.cOut.write(b);
        }
        else {
            this.mac[this.macIndex++] = (byte)n;
        }
    }
    
    public byte[] getMac() {
        return Arrays.clone(this.mac);
    }
}
