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

package com.google.crypto.tink.streamingaead;

import java.nio.ByteBuffer;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.util.ArrayDeque;
import java.util.List;
import com.google.crypto.tink.StreamingAead;
import java.util.Deque;
import com.google.crypto.tink.subtle.RewindableReadableByteChannel;
import javax.annotation.concurrent.GuardedBy;
import java.nio.channels.ReadableByteChannel;

final class ReadableByteChannelDecrypter implements ReadableByteChannel
{
    @GuardedBy("this")
    ReadableByteChannel attemptingChannel;
    @GuardedBy("this")
    ReadableByteChannel matchingChannel;
    @GuardedBy("this")
    RewindableReadableByteChannel ciphertextChannel;
    Deque<StreamingAead> remainingPrimitives;
    byte[] associatedData;
    
    public ReadableByteChannelDecrypter(final List<StreamingAead> allPrimitives, final ReadableByteChannel ciphertextChannel, final byte[] associatedData) {
        this.attemptingChannel = null;
        this.matchingChannel = null;
        this.remainingPrimitives = new ArrayDeque<StreamingAead>();
        for (final StreamingAead primitive : allPrimitives) {
            this.remainingPrimitives.add(primitive);
        }
        this.ciphertextChannel = new RewindableReadableByteChannel(ciphertextChannel);
        this.associatedData = associatedData.clone();
    }
    
    @GuardedBy("this")
    private synchronized ReadableByteChannel nextAttemptingChannel() throws IOException {
        while (!this.remainingPrimitives.isEmpty()) {
            final StreamingAead streamingAead = this.remainingPrimitives.removeFirst();
            try {
                final ReadableByteChannel decChannel = streamingAead.newDecryptingChannel(this.ciphertextChannel, this.associatedData);
                return decChannel;
            }
            catch (final GeneralSecurityException e) {
                this.ciphertextChannel.rewind();
                continue;
            }
            break;
        }
        throw new IOException("No matching key found for the ciphertext in the stream.");
    }
    
    @Override
    public synchronized int read(final ByteBuffer dst) throws IOException {
        if (dst.remaining() == 0) {
            return 0;
        }
        if (this.matchingChannel != null) {
            return this.matchingChannel.read(dst);
        }
        if (this.attemptingChannel == null) {
            this.attemptingChannel = this.nextAttemptingChannel();
        }
        while (true) {
            try {
                final int retValue = this.attemptingChannel.read(dst);
                if (retValue == 0) {
                    return 0;
                }
                this.matchingChannel = this.attemptingChannel;
                this.attemptingChannel = null;
                this.ciphertextChannel.disableRewinding();
                return retValue;
            }
            catch (final IOException e) {
                this.ciphertextChannel.rewind();
                this.attemptingChannel = this.nextAttemptingChannel();
                continue;
            }
            break;
        }
    }
    
    @Override
    public synchronized void close() throws IOException {
        this.ciphertextChannel.close();
    }
    
    @Override
    public synchronized boolean isOpen() {
        return this.ciphertextChannel.isOpen();
    }
}
