package TX_OtherClass;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;

import okhttp3.internal.http2.ErrorCode;

public final class d {
    protected String a;
    private ByteBuffer b;

    private d() {
        this.a = "GBK";
        this.b = ByteBuffer.allocate(128);
    }

    public d(byte b) {
        this();
    }

    public final byte[] a() {
        Object obj = new byte[this.b.position()];
        System.arraycopy(this.b.array(), 0, obj, 0, this.b.position());
        return (byte[]) obj;
    }

    private void a(int i) {
        if (this.b.remaining() < i) {
            try {
                ByteBuffer allocate = ByteBuffer.allocate((this.b.capacity() + i) * 2);
                allocate.put(this.b.array(), 0, this.b.position());
                this.b = allocate;
            } catch (IllegalArgumentException e) {
                throw e;
            }
        }
    }

    private void b(byte b, int i) {
        if (i < 15) {
            this.b.put((byte) ((i << 4) | b));
        } else if (i < 256) {
            this.b.put((byte) (b | 240));
            this.b.put((byte) i);
        } else {
            //throw new JceEncodeException("tag is too large: " + i);
        }
    }

    public final void a(boolean z, int i) {
        a((byte) (z ? 1 : 0), i);
    }

    public final void a(byte b, int i) {
        a(3);
        if (b == (byte) 0) {
            b((byte) 12, i);
            return;
        }
        b((byte) 0, i);
        this.b.put(b);
    }

    public final void a(short s, int i) {
        a(4);
        if (s < (short) -128 || s > (short) 127) {
            b((byte) 1, i);
            this.b.putShort(s);
            return;
        }
        a((byte) s, i);
    }

    public final void a(int i, int i2) {
        a(6);
        if (i < -32768 || i > 32767) {
            b((byte) 2, i2);
            this.b.putInt(i);
            return;
        }
        a((short) i, i2);
    }

    public final void a(long j, int i) {
        a(10);
        if (j < -2147483648L || j > 2147483647L) {
            b((byte) 3, i);
            this.b.putLong(j);
            return;
        }
        a((int) j, i);
    }

    public final void a(float f, int i) {
        a(6);
        b((byte) 4, i);
        this.b.putFloat(f);
    }

    public final void a(double d, int i) {
        a(10);
        b((byte) 5, i);
        this.b.putDouble(d);
    }

    public final void a(String str, int i) {
        byte[] bytes;
        try {
            bytes = str.getBytes(this.a);
        } catch (UnsupportedEncodingException e) {
            bytes = str.getBytes();
        }
        a(bytes.length + 10);
        if (bytes.length > 255) {
            b((byte) 7, i);
            this.b.putInt(bytes.length);
            this.b.put(bytes);
            return;
        }
        b((byte) 6, i);
        this.b.put((byte) bytes.length);
        this.b.put(bytes);
    }

    public final <K, V> void a(Map<K, V> map, int i) {
        a(8);
        b((byte) 8, i);
        a(map == null ? 0 : map.size(), 0);
        if (map != null) {
            for (Entry entry : map.entrySet()) {
                a(entry.getKey(), 0);
                a(entry.getValue(), 1);
            }
        }
    }

    public final void a(byte[] bArr, int i) {
        a(bArr.length + 8);
        b((byte) 13, i);
        b((byte) 0, 0);
        a(bArr.length, 0);
        b.put(bArr);
    }

    public final <T> void a(Collection<T> collection, int i) {
        a(8);
        b((byte) 9, i);
        a(collection == null ? 0 : collection.size(), 0);
        if (collection != null) {
            for (T a : collection) {
                a((Object) a, 0);
            }
        }
    }

    public final void a(JceStruct jceStruct, int i) {
        a(2);
        b((byte) 10, i);
        jceStruct.writeTo(this);
        a(2);
        b((byte) 11, 0);
    }

    public void a(Object obj, int i) {
        if (obj instanceof Byte) {
            a(((Byte) obj).byteValue(), i);
        } else if (obj instanceof Boolean) {
            a(((Boolean) obj).booleanValue(), i);
        } else if (obj instanceof Short) {
            a(((Short) obj).shortValue(), i);
        } else if (obj instanceof Integer) {
            a(((Integer) obj).intValue(), i);
        } else if (obj instanceof Long) {
            a(((Long) obj).longValue(), i);
        } else if (obj instanceof Float) {
            a(((Float) obj).floatValue(), i);
        } else if (obj instanceof Double) {
            a(((Double) obj).doubleValue(), i);
        } else if (obj instanceof String) {
            a((String) obj, i);
        } else if (obj instanceof Map) {
            a((Map) obj, i);
        } else if (obj instanceof List) {
            a((List) obj, i);
        } else if (obj instanceof JceStruct) {
            a((JceStruct) obj, i);
        } else if (obj instanceof byte[]) {
            a((byte[]) obj, i);
        } else if (obj instanceof boolean[]) {
            boolean[] zArr = (boolean[]) obj;
            a(8);
            b((byte) 9, i);
            a(zArr.length, 0);
            for (boolean a : zArr) {
                a(a, 0);
            }
        } else if (obj instanceof short[]) {
            short[] sArr = (short[]) obj;
            a(8);
            b((byte) 9, i);
            a(sArr.length, 0);
            for (short a2 : sArr) {
                a(a2, 0);
            }
        } else if (obj instanceof int[]) {
            int[] iArr = (int[]) obj;
            a(8);
            b((byte) 9, i);
            a(iArr.length, 0);
            for (int a3 : iArr) {
                a(a3, 0);
            }
        } else if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            a(8);
            b((byte) 9, i);
            a(jArr.length, 0);
            for (long a4 : jArr) {
                a(a4, 0);
            }
        } else if (obj instanceof float[]) {
            float[] fArr = (float[]) obj;
            a(8);
            b((byte) 9, i);
            a(fArr.length, 0);
            for (float a5 : fArr) {
                a(a5, 0);
            }
        } else if (obj instanceof double[]) {
            double[] dArr = (double[]) obj;
            a(8);
            b((byte) 9, i);
            a(dArr.length, 0);
            for (double a6 : dArr) {
                a(a6, 0);
            }
        } else if (obj.getClass().isArray()) {
            Object[] objArr = (Object[]) obj;
            a(8);
            b((byte) 9, i);
            a(objArr.length, 0);
            for (Object a7 : objArr) {
                a(a7, 0);
            }
        } else if (obj instanceof Collection) {
            a((Collection) obj, i);
        } else {
           // throw new JceEncodeException("write object error: unsupport type. " + obj.getClass());
        }
    }

    public final int a(String str) {
        this.a = str;
        return 0;
    }
}
