package kotlin.reflect.w.internal.y0.i;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import kotlin.reflect.w.internal.y0.i.s;

/* JADX INFO: loaded from: classes2.dex */
public abstract class c implements Iterable<Byte> {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final c f8234f = new o(new byte[0]);

    public interface a extends Iterator<Byte> {
        byte a();
    }

    public static final class b extends OutputStream {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public static final byte[] f8235f = new byte[0];

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final int f8236m;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public final ArrayList<c> f8237n;

        /* JADX INFO: renamed from: o, reason: collision with root package name */
        public int f8238o;

        /* JADX INFO: renamed from: p, reason: collision with root package name */
        public byte[] f8239p;

        /* JADX INFO: renamed from: q, reason: collision with root package name */
        public int f8240q;

        public b(int i2) {
            if (i2 < 0) {
                throw new IllegalArgumentException("Buffer size < 0");
            }
            this.f8236m = i2;
            this.f8237n = new ArrayList<>();
            this.f8239p = new byte[i2];
        }

        public final void a(int i2) {
            this.f8237n.add(new o(this.f8239p));
            int length = this.f8238o + this.f8239p.length;
            this.f8238o = length;
            this.f8239p = new byte[Math.max(this.f8236m, Math.max(i2, length >>> 1))];
            this.f8240q = 0;
        }

        public final void c() {
            int i2 = this.f8240q;
            byte[] bArr = this.f8239p;
            if (i2 >= bArr.length) {
                this.f8237n.add(new o(this.f8239p));
                this.f8239p = f8235f;
            } else if (i2 > 0) {
                byte[] bArr2 = new byte[i2];
                System.arraycopy(bArr, 0, bArr2, 0, Math.min(bArr.length, i2));
                this.f8237n.add(new o(bArr2));
            }
            this.f8238o += this.f8240q;
            this.f8240q = 0;
        }

        public synchronized c d() {
            ArrayList<c> arrayList;
            c();
            arrayList = this.f8237n;
            if (!(arrayList instanceof Collection)) {
                ArrayList<c> arrayList2 = new ArrayList<>();
                Iterator<T> it = arrayList.iterator();
                while (it.hasNext()) {
                    arrayList2.add((c) it.next());
                }
                arrayList = arrayList2;
            }
            return arrayList.isEmpty() ? c.f8234f : c.a(arrayList.iterator(), arrayList.size());
        }

        public String toString() {
            int i2;
            Object[] objArr = new Object[2];
            objArr[0] = Integer.toHexString(System.identityHashCode(this));
            synchronized (this) {
                i2 = this.f8238o + this.f8240q;
            }
            objArr[1] = Integer.valueOf(i2);
            return String.format("<ByteString.Output@%s size=%d>", objArr);
        }

        @Override // java.io.OutputStream
        public synchronized void write(int i2) {
            if (this.f8240q == this.f8239p.length) {
                a(1);
            }
            byte[] bArr = this.f8239p;
            int i3 = this.f8240q;
            this.f8240q = i3 + 1;
            bArr[i3] = (byte) i2;
        }

        @Override // java.io.OutputStream
        public synchronized void write(byte[] bArr, int i2, int i3) {
            byte[] bArr2 = this.f8239p;
            int length = bArr2.length;
            int i4 = this.f8240q;
            if (i3 <= length - i4) {
                System.arraycopy(bArr, i2, bArr2, i4, i3);
                this.f8240q += i3;
            } else {
                int length2 = bArr2.length - i4;
                System.arraycopy(bArr, i2, bArr2, i4, length2);
                int i5 = i3 - length2;
                a(i5);
                System.arraycopy(bArr, i2 + length2, this.f8239p, 0, i5);
                this.f8240q = i5;
            }
        }
    }

    public static c a(Iterator<c> it, int i2) {
        if (i2 == 1) {
            return it.next();
        }
        int i3 = i2 >>> 1;
        return a(it, i3).b(a(it, i2 - i3));
    }

    public static c c(String str) {
        try {
            return new o(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e2) {
            throw new RuntimeException("UTF-8 not supported?", e2);
        }
    }

    public static b n() {
        return new b(128);
    }

    public c b(c cVar) {
        int size = size();
        int size2 = cVar.size();
        if (((long) size) + ((long) size2) >= 2147483647L) {
            StringBuilder sb = new StringBuilder(53);
            sb.append("ByteString would be too long: ");
            sb.append(size);
            sb.append("+");
            sb.append(size2);
            throw new IllegalArgumentException(sb.toString());
        }
        int[] iArr = s.f8286m;
        s sVar = this instanceof s ? (s) this : null;
        if (cVar.size() == 0) {
            return this;
        }
        if (size() == 0) {
            return cVar;
        }
        int size3 = cVar.size() + size();
        if (size3 < 128) {
            return s.w(this, cVar);
        }
        if (sVar != null) {
            if (cVar.size() + sVar.f8289p.size() < 128) {
                return new s(sVar.f8288o, s.w(sVar.f8289p, cVar));
            }
        }
        if (sVar != null && sVar.f8288o.f() > sVar.f8289p.f() && sVar.f8291r > cVar.f()) {
            return new s(sVar.f8288o, new s(sVar.f8289p, cVar));
        }
        if (size3 >= s.f8286m[Math.max(f(), cVar.f()) + 1]) {
            return new s(this, cVar);
        }
        s.b bVar = new s.b(null);
        bVar.a(this);
        bVar.a(cVar);
        c cVarPop = bVar.a.pop();
        while (!bVar.a.isEmpty()) {
            cVarPop = new s(bVar.a.pop(), cVarPop);
        }
        return cVarPop;
    }

    public void d(byte[] bArr, int i2, int i3, int i4) {
        if (i2 < 0) {
            StringBuilder sb = new StringBuilder(30);
            sb.append("Source offset < 0: ");
            sb.append(i2);
            throw new IndexOutOfBoundsException(sb.toString());
        }
        if (i3 < 0) {
            StringBuilder sb2 = new StringBuilder(30);
            sb2.append("Target offset < 0: ");
            sb2.append(i3);
            throw new IndexOutOfBoundsException(sb2.toString());
        }
        if (i4 < 0) {
            StringBuilder sb3 = new StringBuilder(23);
            sb3.append("Length < 0: ");
            sb3.append(i4);
            throw new IndexOutOfBoundsException(sb3.toString());
        }
        int i5 = i2 + i4;
        if (i5 > size()) {
            StringBuilder sb4 = new StringBuilder(34);
            sb4.append("Source end offset < 0: ");
            sb4.append(i5);
            throw new IndexOutOfBoundsException(sb4.toString());
        }
        int i6 = i3 + i4;
        if (i6 <= bArr.length) {
            if (i4 > 0) {
                e(bArr, i2, i3, i4);
            }
        } else {
            StringBuilder sb5 = new StringBuilder(34);
            sb5.append("Target end offset < 0: ");
            sb5.append(i6);
            throw new IndexOutOfBoundsException(sb5.toString());
        }
    }

    public abstract void e(byte[] bArr, int i2, int i3, int i4);

    public abstract int f();

    public abstract boolean h();

    public abstract boolean i();

    @Override // java.lang.Iterable
    /* JADX INFO: renamed from: k */
    public abstract a iterator();

    public abstract int p(int i2, int i3, int i4);

    public abstract int r(int i2, int i3, int i4);

    public abstract int s();

    public abstract int size();

    public abstract String t(String str);

    public String toString() {
        return String.format("<ByteString@%s size=%d>", Integer.toHexString(System.identityHashCode(this)), Integer.valueOf(size()));
    }

    public String u() {
        try {
            return t("UTF-8");
        } catch (UnsupportedEncodingException e2) {
            throw new RuntimeException("UTF-8 not supported?", e2);
        }
    }

    public abstract void v(OutputStream outputStream, int i2, int i3);
}
