Skip to content

Commit 183a030

Browse files
committed
Removed NPE catches + added null checks on connection
1 parent 6c5177c commit 183a030

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

lib/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private void authenticate(@NotNull Authentication.LoginCredentials credentials)
388388
* {@code true} for {@link Session#reconnect()}.
389389
*/
390390
private void authenticatePartial(@NotNull Authentication.LoginCredentials credentials, boolean removeLock) throws IOException, GeneralSecurityException, SpotifyAuthenticationException {
391-
if (cipherPair == null) throw new IllegalStateException("Connection not established!");
391+
if (conn == null || cipherPair == null) throw new IllegalStateException("Connection not established!");
392392

393393
Authentication.ClientResponseEncrypted clientResponseEncrypted = Authentication.ClientResponseEncrypted.newBuilder()
394394
.setLoginCredentials(credentials)
@@ -409,7 +409,6 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
409409

410410
receiver = new Receiver();
411411

412-
413412
byte[] bytes0x0f = new byte[20];
414413
random().nextBytes(bytes0x0f);
415414
sendUnchecked(Packet.Type.Unknown_0x0f, bytes0x0f);
@@ -515,11 +514,10 @@ public void close() throws IOException {
515514
}
516515

517516
private void sendUnchecked(Packet.Type cmd, byte[] payload) throws IOException {
518-
try {
519-
cipherPair.sendEncoded(conn.out, cmd.val, payload);
520-
} catch (NullPointerException e) {
521-
throw new IOException(e);
522-
}
517+
if (conn == null)
518+
throw new IOException("Cannot write to missing connection.");
519+
520+
cipherPair.sendEncoded(conn.out, cmd.val, payload);
523521
}
524522

525523
private void waitAuthLock() {
@@ -724,7 +722,7 @@ private void reconnect() {
724722
synchronized (reconnectionListeners) {
725723
reconnectionListeners.forEach(ReconnectionListener::onConnectionEstablished);
726724
}
727-
} catch (NullPointerException | IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
725+
} catch (IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
728726
if (closing)
729727
return;
730728

@@ -1321,8 +1319,8 @@ public void run() {
13211319
LOGGER.info("Skipping unknown command {cmd: 0x{}, payload: {}}", Integer.toHexString(packet.cmd), Utils.bytesToHex(packet.payload));
13221320
continue;
13231321
}
1324-
} catch (IOException | GeneralSecurityException | NullPointerException ex) {
1325-
if (running) {
1322+
} catch (IOException | GeneralSecurityException ex) {
1323+
if (running && !closing) {
13261324
LOGGER.error("Failed reading packet!", ex);
13271325
reconnect();
13281326
}

0 commit comments

Comments
 (0)