package a.c.b;

import com.google.protobuf.GeneratedMessageLite;
import java.nio.ByteBuffer;

public class s {

    /* renamed from: a  reason: collision with root package name */
    private final h f81a;
    private ByteBuffer b;
    private int c;
    private final boolean d;
    private final int e;

    public s(h hVar, ByteBuffer byteBuffer) {
        this(hVar, byteBuffer, byteBuffer.remaining());
    }

    public s(h hVar, ByteBuffer byteBuffer, int i) {
        if (byteBuffer.remaining() == i) {
            this.f81a = hVar;
            this.d = false;
            this.b = byteBuffer;
            this.c = i;
            this.e = -1;
            return;
        }
        throw new l("Create message from ByteBuffer error: body len invalid");
    }

    public s(h hVar, byte[] bArr) {
        this(hVar, bArr, bArr.length);
    }

    public s(h hVar, byte[] bArr, int i) {
        j.b(hVar, "header");
        j.b(bArr, "body");
        this.f81a = hVar;
        this.d = false;
        this.c = i;
        this.b = ByteBuffer.wrap(bArr);
        this.e = -1;
    }

    private s(boolean z) {
        this.f81a = h.i();
        this.d = z;
        this.e = z ? m() : -1;
    }

    public static s a(int i) {
        s s = s();
        s.g().a(20, f.RAW, i.RES, c.ACK, 0).e(i);
        s.b(g.b, 0);
        return s;
    }

    public static s a(boolean z) {
        return new s(z);
    }

    public static s a(byte[] bArr) {
        j.b(bArr, "rawMessageBytes");
        if (bArr.length >= 20) {
            h b2 = h.b(bArr);
            if (b2.d() == bArr.length) {
                int length = bArr.length - 20;
                return new s(b2, ByteBuffer.wrap(bArr, 20, length), length);
            }
            throw new l("Message invalid header length not correct:" + b2.d());
        }
        throw new l("message too short: " + bArr.length);
    }

    public static boolean a(s sVar) {
        return sVar != null && sVar.i() == i.SEND_SYNC;
    }

    private int b(int i) {
        int i2 = i - 1;
        int i3 = i2 | (i2 >>> 1);
        int i4 = i3 | (i3 >>> 2);
        int i5 = i4 | (i4 >>> 4);
        int i6 = i5 | (i5 >>> 8);
        int i7 = i6 | (i6 >>> 16);
        if (i7 < 0) {
            return 1;
        }
        return 1 + i7;
    }

    public static t r() {
        return new t();
    }

    public static s s() {
        return u.a().b();
    }

    public s a(i iVar) {
        this.f81a.a(iVar);
        return this;
    }

    public <T extends GeneratedMessageLite<T, BuilderType>, BuilderType extends GeneratedMessageLite.Builder<T, BuilderType>> T a(Class<T> cls) {
        f b2 = this.f81a.c().b();
        if (b2 == f.PB3) {
            try {
                return (T) ((GeneratedMessageLite) cls.getDeclaredMethod("parseFrom", ByteBuffer.class).invoke(cls, b()));
            } catch (Exception e2) {
                throw new l("Parse message " + m() + " protobuf data error: " + e2.getMessage());
            }
        } else {
            throw new l("Parse protobuf data error: dataFormat is " + b2);
        }
    }

    public String a() {
        String str;
        StringBuilder sb = new StringBuilder(this.f81a.toString());
        if (this.f81a.c().b() == f.PB3) {
            str = "Protobuf Data(";
        } else {
            if (this.f81a.c().b() == f.RAW) {
                str = "RAW Data(";
            }
            sb.append("--------------------------------\n");
            return sb.toString();
        }
        sb.append(str).append(this.c).append(")\n");
        sb.append("--------------------------------\n");
        return sb.toString();
    }

    public void a(ByteBuffer byteBuffer) {
        this.b.mark();
        byteBuffer.put(this.b);
        this.b.reset();
    }

    public void a(ByteBuffer byteBuffer, int i) {
        if (byteBuffer == null && i != 0) {
            throw new l("null body expect 0 body length, but get: " + i);
        } else if (byteBuffer == null || byteBuffer.remaining() == i) {
            this.c = i;
            if (byteBuffer == null || i == 0) {
                this.b = g.b;
                return;
            }
            ByteBuffer byteBuffer2 = this.b;
            if (byteBuffer2 != null && byteBuffer2.capacity() > 131072) {
                this.b = null;
            }
            ByteBuffer byteBuffer3 = this.b;
            if (byteBuffer3 == null || byteBuffer3.capacity() < i) {
                int b2 = b(i);
                if (b2 >= i) {
                    i = b2;
                }
                this.b = ByteBuffer.allocate(i);
            } else {
                this.b.clear();
            }
            this.b.put(byteBuffer);
            this.b.flip();
        } else {
            throw new l("invalid param, expect body length: " + byteBuffer.remaining() + ", but get:" + i);
        }
    }

    public ByteBuffer b() {
        return this.b;
    }

    public void b(ByteBuffer byteBuffer) {
        if (byteBuffer == null) {
            byteBuffer = g.b;
        }
        a(byteBuffer, byteBuffer.remaining());
    }

    public void b(ByteBuffer byteBuffer, int i) {
        this.b = byteBuffer;
        this.c = i;
    }

    public void b(byte[] bArr) {
        this.f81a.c(bArr);
        this.b.mark();
        this.b.get(bArr, 20, e());
        this.b.reset();
    }

    public ByteBuffer c() {
        return this.b;
    }

    public final void c(int i) {
        g().c(i);
        this.c = i;
    }

    public void c(ByteBuffer byteBuffer) {
        b(byteBuffer, byteBuffer.remaining());
    }

    public s d(int i) {
        this.f81a.e(i);
        return this;
    }

    public byte[] d() {
        if (!p() && this.b.hasArray() && this.b.array().length == this.c) {
            return this.b.array();
        }
        byte[] bArr = new byte[this.c];
        this.b.mark();
        this.b.get(bArr, 0, this.c);
        this.b.reset();
        return bArr;
    }

    public int e() {
        return this.c;
    }

    public c f() {
        return this.f81a.c().a();
    }

    public h g() {
        return this.f81a;
    }

    public int h() {
        return this.f81a.d();
    }

    public i i() {
        return this.f81a.c().c();
    }

    public int j() {
        return this.f81a.c().d();
    }

    public int k() {
        return this.e;
    }

    public short l() {
        return this.f81a.e();
    }

    public int m() {
        return this.f81a.f();
    }

    public byte[] n() {
        if (this.b.hasArray()) {
            return this.b.array();
        }
        byte[] a2 = g.a(e());
        this.b.get(a2, 0, e());
        this.b.position(0);
        return a2;
    }

    public boolean o() {
        d c2 = g().c();
        j.a((Object) c2, "header.cmdDescription");
        return c2.b() == f.PB3;
    }

    public boolean p() {
        return this.d;
    }

    public boolean q() {
        d c2 = g().c();
        j.a((Object) c2, "header.cmdDescription");
        return c2.b() == f.RAW;
    }

    public synchronized void t() {
        if (p()) {
            u.a().a(this);
        }
    }

    public String toString() {
        return a();
    }

    public byte[] u() {
        byte[] bArr = new byte[h()];
        b(bArr);
        return bArr;
    }
}
