package com.mbridge.msdk.foundation.same.net; import android.text.TextUtils; import com.mbridge.msdk.foundation.same.net.e.c; import com.mbridge.msdk.foundation.same.net.f.d; import com.mbridge.msdk.foundation.tools.w; import com.mbridge.msdk.playercommon.exoplayer2.DefaultLoadControl; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import kotlin.UByte; import org.json.JSONObject; /* compiled from: SocketManager */ public final class m { private static volatile AtomicInteger a = new AtomicInteger(1); private ExecutorService b; /* access modifiers changed from: private */ /* compiled from: SocketManager */ public static final class b { private static m a = new m(); } private m() { this.b = new ThreadPoolExecutor(5, 10, 5, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() { /* class com.mbridge.msdk.foundation.same.net.m.AnonymousClass1 */ public final Thread newThread(Runnable runnable) { Thread newThread = Executors.defaultThreadFactory().newThread(runnable); newThread.setName("SocketThreadPool"); return newThread; } }, new ThreadPoolExecutor.DiscardPolicy()); } public static m a() { return b.a; } public final synchronized void a(String str, int i, String str2, e eVar) { this.b.execute(new a(str, i, str2, eVar)); } public final synchronized void a(String str, int i, String str2, boolean z, e eVar) { this.b.execute(new a(str, i, str2, z, eVar)); } /* access modifiers changed from: private */ /* compiled from: SocketManager */ public class a implements Runnable { private String b; private ByteBuffer c; private OutputStream d; private boolean e; private e f; private Socket g; private String h; private int i; a(String str, int i2, String str2, e eVar) { this.e = false; this.h = str; this.i = i2; this.b = str2; this.f = eVar; } a(String str, int i2, String str2, boolean z, e eVar) { this.h = str; this.i = i2; this.b = str2; this.e = z; this.f = eVar; } /* JADX WARNING: Removed duplicated region for block: B:60:0x01ec */ /* JADX WARNING: Removed duplicated region for block: B:64:0x01ff */ /* JADX WARNING: Removed duplicated region for block: B:70:0x0233 A[SYNTHETIC, Splitter:B:70:0x0233] */ public final void run() { IOException e2; int i2; Socket socket; JSONObject jSONObject; Throwable th; String str; try { Socket socket2 = new Socket(this.h, this.i); this.g = socket2; socket2.setSoTimeout(DefaultLoadControl.DEFAULT_MIN_BUFFER_MS); boolean contains = this.h.contains(d.a().e); w.d("SocketManager", "Socket connect : " + this.h + " : " + this.i + " isAnalytics : " + contains); this.d = this.g.getOutputStream(); ByteBuffer wrap = ByteBuffer.wrap(new byte[8]); this.c = wrap; wrap.order(ByteOrder.BIG_ENDIAN); this.c.put((byte) 2); if (TextUtils.isEmpty(this.b)) { this.c.put((byte) 1); } else { this.c.put(this.e ? (byte) 3 : 2); } this.c.putShort((short) m.a.getAndIncrement()); if (TextUtils.isEmpty(this.b)) { this.c.putInt(0); this.d.write(this.c.array()); i2 = 0; } else { byte[] a2 = this.e ? m.this.a(this.b) : this.b.getBytes(); i2 = a2.length; this.c.putInt(i2); this.d.write(this.c.array()); this.d.write(a2); } this.d.flush(); w.a("SocketManager", "Socket Request : header : " + Arrays.toString(this.c.array()) + " length : " + i2); InputStream inputStream = this.g.getInputStream(); byte[] bArr = new byte[8]; inputStream.read(bArr, 0, 8); ByteBuffer wrap2 = ByteBuffer.wrap(bArr); this.c = wrap2; wrap2.order(ByteOrder.BIG_ENDIAN); int i3 = this.c.getInt(4); boolean z = bArr[1] == 3; w.d("SocketManager", "Socket Response : header : " + Arrays.toString(bArr) + " length : " + i3 + " isGzip : " + z); byte[] bArr2 = new byte[i3]; new DataInputStream(this.g.getInputStream()).readFully(bArr2); if (!z && i3 > 2 && ((bArr2[0] << 8) | (bArr2[1] & UByte.MAX_VALUE)) == 8075) { z = true; } if (i3 >= 1) { e eVar = this.f; if (eVar != null) { if (contains) { if (bArr2[0] == 1) { eVar.a(k.a(null, new c(200, null, null))); } else { eVar.a(new com.mbridge.msdk.foundation.same.net.a.a(2, new c(404, null, null))); } Socket socket3 = this.g; if (socket3 != null) { try { socket3.close(); } catch (IOException e3) { e3.printStackTrace(); } } this.f = null; return; } if (z) { try { str = m.this.a(bArr2); } catch (Throwable th2) { th = th2; jSONObject = null; w.d("SocketManager", th.getMessage()); if (jSONObject == null) { } this.g.close(); this.c = null; inputStream.close(); this.d.close(); socket = this.g; if (socket != null) { } this.f = null; } } else { str = new String(bArr2); } jSONObject = !TextUtils.isEmpty(str) ? new JSONObject(str) : null; try { w.d("SocketManager", "Socket Response length : " + i3 + " " + str.length()); } catch (Throwable th3) { th = th3; } if (jSONObject == null) { e eVar2 = this.f; if (jSONObject == null) { jSONObject = null; } eVar2.a(k.a(jSONObject, new c(200, null, null))); } else { this.f.a(new com.mbridge.msdk.foundation.same.net.a.a(2, new c(404, null, null))); } } } else { e eVar3 = this.f; if (eVar3 != null) { eVar3.a(new com.mbridge.msdk.foundation.same.net.a.a(2, new c(404, null, null))); } } this.g.close(); this.c = null; inputStream.close(); this.d.close(); socket = this.g; if (socket != null) { try { socket.close(); } catch (IOException e4) { e2 = e4; } } } catch (Throwable th4) { Socket socket4 = this.g; if (socket4 != null) { try { socket4.close(); } catch (IOException e5) { e5.printStackTrace(); } } this.f = null; throw th4; } this.f = null; e2.printStackTrace(); this.f = null; } } public final byte[] a(String str) { if (TextUtils.isEmpty(str)) { return null; } try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream); gZIPOutputStream.write(str.getBytes()); gZIPOutputStream.close(); return byteArrayOutputStream.toByteArray(); } catch (Throwable th) { w.d("SocketManager", th.getMessage()); return null; } } public final String a(byte[] bArr) { if (!(bArr == null || bArr.length == 0)) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr); GZIPInputStream gZIPInputStream = new GZIPInputStream(byteArrayInputStream); byte[] bArr2 = new byte[1024]; while (true) { int read = gZIPInputStream.read(bArr2, 0, 1024); if (read <= 0) { break; } byteArrayOutputStream.write(bArr2, 0, read); } gZIPInputStream.close(); byteArrayInputStream.close(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); } catch (Throwable th) { w.d("SocketManager", th.getMessage()); } return byteArrayOutputStream.toString(); } catch (Throwable th2) { w.d("SocketManager", th2.getMessage()); } } return null; } }