package org.snmp4j.security;

import java.util.LinkedList;
import javax.crypto.Cipher;

/* JADX INFO: loaded from: classes.dex */
public class CipherPool {
    private LinkedList<Cipher> availableCiphers;
    private int currentPoolSize;
    private int maxPoolSize;

    public CipherPool() {
        this(Runtime.getRuntime().availableProcessors());
    }

    public int getMaxPoolSize() {
        return this.maxPoolSize;
    }

    public synchronized void offerCipher(Cipher cipher) {
        if (this.currentPoolSize < this.maxPoolSize) {
            this.currentPoolSize++;
            this.availableCiphers.offer(cipher);
        }
    }

    public synchronized Cipher reuseCipher() {
        Cipher cipherPoll;
        cipherPoll = this.availableCiphers.poll();
        if (cipherPoll == null) {
            this.currentPoolSize = 0;
        } else {
            this.currentPoolSize--;
        }
        return cipherPoll;
    }

    public CipherPool(int i2) {
        this.currentPoolSize = 0;
        if (i2 < 0) {
            throw new IllegalArgumentException("Pool size must be >= 0");
        }
        this.maxPoolSize = i2;
        this.availableCiphers = new LinkedList<>();
    }
}
