package com.hierynomus.protocol.commons.buffer;

import com.hierynomus.protocol.commons.buffer.Buffer;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import p138.p139.p143.p144.C2803;
import p138.p329.p351.p352.p353.AbstractC6294;
import p463.p481.C7162;

/* JADX INFO: loaded from: classes.dex */
public class Buffer<T extends Buffer<T>> {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public byte[] f2168;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public AbstractC6294 f2169;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public int f2170;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public int f2171;

    public static class BufferException extends Exception {
        public BufferException(String str) {
            super(str);
        }
    }

    /* JADX INFO: renamed from: com.hierynomus.protocol.commons.buffer.Buffer$֏, reason: contains not printable characters */
    public static class C0346 extends Buffer<C0346> {
        public C0346(AbstractC6294 abstractC6294) {
            super(abstractC6294);
        }

        public C0346(byte[] bArr, AbstractC6294 abstractC6294) {
            super(bArr, abstractC6294);
        }
    }

    static {
        C7162.m10575((Class<?>) Buffer.class);
    }

    public Buffer(AbstractC6294 abstractC6294) {
        this.f2168 = new byte[m1459(256)];
        this.f2169 = abstractC6294;
        this.f2170 = 0;
        this.f2171 = 0;
    }

    public Buffer(byte[] bArr, AbstractC6294 abstractC6294) {
        this.f2168 = bArr;
        this.f2169 = abstractC6294;
        this.f2170 = 0;
        this.f2171 = bArr.length;
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public static int m1459(int i) {
        int i2 = 1;
        while (i2 < i) {
            i2 <<= 1;
            if (i2 <= 0) {
                throw new IllegalArgumentException(C2803.m4872("Cannot get next power of 2; ", i, " is too large"));
            }
        }
        return i2;
    }

    public String toString() {
        StringBuilder sbM4889 = C2803.m4889("Buffer [rpos=");
        sbM4889.append(this.f2170);
        sbM4889.append(", wpos=");
        sbM4889.append(this.f2171);
        sbM4889.append(", size=");
        return C2803.m4882(sbM4889, this.f2168.length, "]");
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public int m1460() {
        return this.f2171 - this.f2170;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Buffer<T> mo1461(byte b) {
        m1469(1);
        byte[] bArr = this.f2168;
        int i = this.f2171;
        this.f2171 = i + 1;
        bArr[i] = b;
        return this;
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:17:0x003a  */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Buffer<T> m1462(String str, Charset charset) {
        byte b;
        AbstractC6294 abstractC6294 = this.f2169;
        String strName = charset.name();
        switch (strName.hashCode()) {
            case -1781783509:
                b = !strName.equals("UTF-16") ? (byte) -1 : (byte) 0;
                break;
            case 81070450:
                if (strName.equals("UTF-8")) {
                    b = 3;
                    break;
                }
                break;
            case 1398001070:
                if (strName.equals("UTF-16BE")) {
                    b = 2;
                    break;
                }
                break;
            case 1398001380:
                if (strName.equals("UTF-16LE")) {
                    b = 1;
                    break;
                }
                break;
        }
        if (b == 0) {
            abstractC6294.mo9530(this, str);
            m1463(AbstractC6294.f17632);
        } else if (b == 1) {
            AbstractC6294.f17633.mo9530(this, str);
            m1463(AbstractC6294.f17632);
        } else if (b == 2) {
            AbstractC6294.f17634.mo9530(this, str);
            m1463(AbstractC6294.f17632);
        } else {
            if (b != 3) {
                throw new UnsupportedCharsetException(charset.name());
            }
            m1463(str.getBytes(charset));
            mo1461((byte) 0);
        }
        return this;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Buffer<T> m1463(byte[] bArr) {
        return mo1464(bArr, 0, bArr.length);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Buffer<T> mo1464(byte[] bArr, int i, int i2) {
        m1469(i2);
        System.arraycopy(bArr, i, this.f2168, this.f2171, i2);
        this.f2171 += i2;
        return this;
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0039  */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public String m1465(Charset charset) throws BufferException {
        byte b;
        AbstractC6294 abstractC6294 = this.f2169;
        String strName = charset.name();
        switch (strName.hashCode()) {
            case -1781783509:
                b = !strName.equals("UTF-16") ? (byte) -1 : (byte) 0;
                break;
            case 81070450:
                if (strName.equals("UTF-8")) {
                    b = 3;
                    break;
                }
                break;
            case 1398001070:
                if (strName.equals("UTF-16BE")) {
                    b = 2;
                    break;
                }
                break;
            case 1398001380:
                if (strName.equals("UTF-16LE")) {
                    b = 1;
                    break;
                }
                break;
        }
        if (b == 0) {
            return abstractC6294.mo9531(this);
        }
        if (b == 1) {
            return AbstractC6294.f17633.mo9531(this);
        }
        if (b == 2) {
            return AbstractC6294.f17634.mo9531(this);
        }
        if (b != 3) {
            throw new UnsupportedCharsetException(charset.name());
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte bM1472 = m1472();
        while (bM1472 != 0) {
            byteArrayOutputStream.write(bM1472);
            bM1472 = m1472();
        }
        return new String(byteArrayOutputStream.toByteArray(), charset);
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0039  */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public String m1466(Charset charset, int i) throws BufferException {
        byte b;
        AbstractC6294 abstractC6294 = this.f2169;
        String strName = charset.name();
        switch (strName.hashCode()) {
            case -1781783509:
                b = !strName.equals("UTF-16") ? (byte) -1 : (byte) 0;
                break;
            case 81070450:
                if (strName.equals("UTF-8")) {
                    b = 3;
                    break;
                }
                break;
            case 1398001070:
                if (strName.equals("UTF-16BE")) {
                    b = 2;
                    break;
                }
                break;
            case 1398001380:
                if (strName.equals("UTF-16LE")) {
                    b = 1;
                    break;
                }
                break;
        }
        if (b == 0) {
            return abstractC6294.mo9527((Buffer) this, i);
        }
        if (b == 1) {
            return AbstractC6294.f17633.mo9527((Buffer) this, i);
        }
        if (b == 2) {
            return AbstractC6294.f17634.mo9527((Buffer) this, i);
        }
        if (b != 3) {
            throw new UnsupportedCharsetException(charset.name());
        }
        byte[] bArr = new byte[i];
        m1470(bArr);
        return new String(bArr, charset);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public void m1467(int i) throws BufferException {
        if (m1460() < i) {
            throw new BufferException("Underflow");
        }
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Failed to restore switch over string. Please report as a decompilation issue */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0039  */
    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Buffer<T> m1468(String str, Charset charset) {
        byte b;
        AbstractC6294 abstractC6294 = this.f2169;
        String strName = charset.name();
        switch (strName.hashCode()) {
            case -1781783509:
                b = !strName.equals("UTF-16") ? (byte) -1 : (byte) 0;
                break;
            case 81070450:
                if (strName.equals("UTF-8")) {
                    b = 3;
                    break;
                }
                break;
            case 1398001070:
                if (strName.equals("UTF-16BE")) {
                    b = 2;
                    break;
                }
                break;
            case 1398001380:
                if (strName.equals("UTF-16LE")) {
                    b = 1;
                    break;
                }
                break;
        }
        if (b == 0) {
            abstractC6294.mo9530(this, str);
        } else if (b == 1) {
            AbstractC6294.f17633.mo9530(this, str);
        } else if (b == 2) {
            AbstractC6294.f17634.mo9530(this, str);
        } else {
            if (b != 3) {
                throw new UnsupportedCharsetException(charset.name());
            }
            m1463(str.getBytes(charset));
        }
        return this;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public void m1469(int i) {
        int length = this.f2168.length;
        int i2 = this.f2171;
        if (length - i2 < i) {
            byte[] bArr = new byte[m1459(i2 + i)];
            byte[] bArr2 = this.f2168;
            System.arraycopy(bArr2, 0, bArr, 0, bArr2.length);
            this.f2168 = bArr;
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public void m1470(byte[] bArr) throws BufferException {
        int length = bArr.length;
        m1467(length);
        System.arraycopy(this.f2168, this.f2170, bArr, 0, length);
        this.f2170 += length;
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public byte[] m1471() {
        int iM1460 = m1460();
        if (iM1460 <= 0) {
            return new byte[0];
        }
        byte[] bArr = new byte[iM1460];
        System.arraycopy(this.f2168, this.f2170, bArr, 0, iM1460);
        return bArr;
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public byte m1472() throws BufferException {
        m1467(1);
        byte[] bArr = this.f2168;
        int i = this.f2170;
        this.f2170 = i + 1;
        return bArr[i];
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public Buffer<T> m1473(int i) throws BufferException {
        m1467(i);
        this.f2170 += i;
        return this;
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public long m1474() {
        return this.f2169.mo9526(this);
    }

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public int m1475() {
        return this.f2169.mo9534(this);
    }

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public long m1476() {
        return this.f2169.mo9537(this);
    }

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public int m1477() {
        return (int) m1476();
    }

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public long m1478() {
        return this.f2169.mo9538(this);
    }
}
