Fix nullness checks for UdpDataSource

PiperOrigin-RevId: 402765571
This commit is contained in:
olly 2021-10-13 09:45:05 +01:00 committed by Oliver Woodman
parent b75d68782e
commit 91da6a3434

View File

@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.upstream; package com.google.android.exoplayer2.upstream;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static java.lang.Math.min; import static java.lang.Math.min;
import android.net.Uri; import android.net.Uri;
@ -63,7 +64,6 @@ public final class UdpDataSource extends BaseDataSource {
@Nullable private DatagramSocket socket; @Nullable private DatagramSocket socket;
@Nullable private MulticastSocket multicastSocket; @Nullable private MulticastSocket multicastSocket;
@Nullable private InetAddress address; @Nullable private InetAddress address;
@Nullable private InetSocketAddress socketAddress;
private boolean opened; private boolean opened;
private int packetRemaining; private int packetRemaining;
@ -98,12 +98,12 @@ public final class UdpDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws UdpDataSourceException { public long open(DataSpec dataSpec) throws UdpDataSourceException {
uri = dataSpec.uri; uri = dataSpec.uri;
String host = uri.getHost(); String host = checkNotNull(uri.getHost());
int port = uri.getPort(); int port = uri.getPort();
transferInitializing(dataSpec); transferInitializing(dataSpec);
try { try {
address = InetAddress.getByName(host); address = InetAddress.getByName(host);
socketAddress = new InetSocketAddress(address, port); InetSocketAddress socketAddress = new InetSocketAddress(address, port);
if (address.isMulticastAddress()) { if (address.isMulticastAddress()) {
multicastSocket = new MulticastSocket(socketAddress); multicastSocket = new MulticastSocket(socketAddress);
multicastSocket.joinGroup(address); multicastSocket.joinGroup(address);
@ -133,7 +133,7 @@ public final class UdpDataSource extends BaseDataSource {
if (packetRemaining == 0) { if (packetRemaining == 0) {
// We've read all of the data from the current packet. Get another. // We've read all of the data from the current packet. Get another.
try { try {
socket.receive(packet); checkNotNull(socket).receive(packet);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
throw new UdpDataSourceException( throw new UdpDataSourceException(
e, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT); e, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT);
@ -163,7 +163,7 @@ public final class UdpDataSource extends BaseDataSource {
uri = null; uri = null;
if (multicastSocket != null) { if (multicastSocket != null) {
try { try {
multicastSocket.leaveGroup(address); multicastSocket.leaveGroup(checkNotNull(address));
} catch (IOException e) { } catch (IOException e) {
// Do nothing. // Do nothing.
} }
@ -174,7 +174,6 @@ public final class UdpDataSource extends BaseDataSource {
socket = null; socket = null;
} }
address = null; address = null;
socketAddress = null;
packetRemaining = 0; packetRemaining = 0;
if (opened) { if (opened) {
opened = false; opened = false;