package fh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/* loaded from: classes4.dex */
public class c {

    /* renamed from: e, reason: collision with root package name */
    protected static final Comparator f18475e = new a();

    /* renamed from: a, reason: collision with root package name */
    private final List f18476a = new ArrayList();

    /* renamed from: b, reason: collision with root package name */
    private final List f18477b = new ArrayList(64);

    /* renamed from: c, reason: collision with root package name */
    private int f18478c = 0;

    /* renamed from: d, reason: collision with root package name */
    private final int f18479d;

    /* loaded from: classes4.dex */
    class a implements Comparator {
        a() {
        }

        @Override // java.util.Comparator
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(byte[] bArr, byte[] bArr2) {
            return bArr.length - bArr2.length;
        }
    }

    public c(int i10) {
        this.f18479d = i10;
    }

    private synchronized void c() {
        while (this.f18478c > this.f18479d) {
            byte[] bArr = (byte[]) this.f18476a.remove(0);
            this.f18477b.remove(bArr);
            this.f18478c -= bArr.length;
        }
    }

    public synchronized byte[] a(int i10) {
        for (int i11 = 0; i11 < this.f18477b.size(); i11++) {
            byte[] bArr = (byte[]) this.f18477b.get(i11);
            if (bArr.length >= i10) {
                this.f18478c -= bArr.length;
                this.f18477b.remove(i11);
                this.f18476a.remove(bArr);
                return bArr;
            }
        }
        return new byte[i10];
    }

    public synchronized void b(byte[] bArr) {
        if (bArr != null) {
            if (bArr.length <= this.f18479d) {
                this.f18476a.add(bArr);
                int binarySearch = Collections.binarySearch(this.f18477b, bArr, f18475e);
                if (binarySearch < 0) {
                    binarySearch = (-binarySearch) - 1;
                }
                this.f18477b.add(binarySearch, bArr);
                this.f18478c += bArr.length;
                c();
            }
        }
    }
}
