package z0;

import android.net.Uri;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.SocketTimeoutException;

/* JADX INFO: renamed from: z0.A, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1776A extends AbstractC1779c {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public InetAddress f19102A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public boolean f19103B;

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

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

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final byte[] f19106v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final DatagramPacket f19107w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public Uri f19108x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public DatagramSocket f19109y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public MulticastSocket f19110z;

    public C1776A() {
        super(true);
        this.f19105u = 8000;
        byte[] bArr = new byte[2000];
        this.f19106v = bArr;
        this.f19107w = new DatagramPacket(bArr, 0, 2000);
    }

    @Override // z0.h
    public final void close() {
        this.f19108x = null;
        MulticastSocket multicastSocket = this.f19110z;
        if (multicastSocket != null) {
            try {
                InetAddress inetAddress = this.f19102A;
                inetAddress.getClass();
                multicastSocket.leaveGroup(inetAddress);
            } catch (IOException unused) {
            }
            this.f19110z = null;
        }
        DatagramSocket datagramSocket = this.f19109y;
        if (datagramSocket != null) {
            datagramSocket.close();
            this.f19109y = null;
        }
        this.f19102A = null;
        this.f19104C = 0;
        if (this.f19103B) {
            this.f19103B = false;
            j();
        }
    }

    @Override // z0.h
    public final Uri e() {
        return this.f19108x;
    }

    @Override // z0.h
    public final long g(k kVar) throws z {
        Uri uri = kVar.f19138a;
        this.f19108x = uri;
        String host = uri.getHost();
        host.getClass();
        int port = this.f19108x.getPort();
        l();
        try {
            this.f19102A = InetAddress.getByName(host);
            InetSocketAddress inetSocketAddress = new InetSocketAddress(this.f19102A, port);
            if (this.f19102A.isMulticastAddress()) {
                MulticastSocket multicastSocket = new MulticastSocket(inetSocketAddress);
                this.f19110z = multicastSocket;
                multicastSocket.joinGroup(this.f19102A);
                this.f19109y = this.f19110z;
            } else {
                this.f19109y = new DatagramSocket(inetSocketAddress);
            }
            this.f19109y.setSoTimeout(this.f19105u);
            this.f19103B = true;
            r(kVar);
            return -1L;
        } catch (IOException e8) {
            throw new z(e8, 2001);
        } catch (SecurityException e9) {
            throw new z(e9, 2006);
        }
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) throws z {
        if (i5 == 0) {
            return 0;
        }
        int i8 = this.f19104C;
        DatagramPacket datagramPacket = this.f19107w;
        if (i8 == 0) {
            try {
                DatagramSocket datagramSocket = this.f19109y;
                datagramSocket.getClass();
                datagramSocket.receive(datagramPacket);
                int length = datagramPacket.getLength();
                this.f19104C = length;
                h(length);
            } catch (SocketTimeoutException e8) {
                throw new z(e8, 2002);
            } catch (IOException e9) {
                throw new z(e9, 2001);
            }
        }
        int length2 = datagramPacket.getLength();
        int i9 = this.f19104C;
        int iMin = Math.min(i9, i5);
        System.arraycopy(this.f19106v, length2 - i9, bArr, i, iMin);
        this.f19104C -= iMin;
        return iMin;
    }
}
