Skip to content

Commit 4faab36

Browse files
committed
Clean up
1 parent 0d65693 commit 4faab36

14 files changed

Lines changed: 45 additions & 46 deletions

File tree

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>xyz.gianlu.librespot</groupId>
77
<artifactId>librespot-java</artifactId>
88
<version>1.5.3-SNAPSHOT</version>
9-
<relativePath>../</relativePath>
9+
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

1212
<artifactId>librespot-api</artifactId>

api/src/main/java/xyz/gianlu/librespot/api/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static Map<String, Deque<String>> readParameters(@NotNull HttpServerExcha
3030
byte[] buffer = new byte[1024];
3131
int count;
3232
while ((count = in.read(buffer)) > 0) out.write(buffer, 0, count);
33-
body = new String(out.toByteArray());
33+
body = out.toString();
3434
}
3535

3636
return QueryParameterUtils.mergeQueryParametersWithNewQueryString(map, body, "UTF-8");

api/src/main/java/xyz/gianlu/librespot/api/handlers/PlayerHandler.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,11 @@ private static void setVolume(HttpServerExchange exchange, @NotNull Player playe
5454
return;
5555
}
5656

57-
if (val > 0) {
58-
player.volumeUp(val);
59-
} else if (val < 0) {
60-
player.volumeDown(Math.abs(val));
61-
} else {
62-
Utils.invalidParameter(exchange, "step", "Must be non zero");
63-
return;
64-
}
57+
if (val > 0) player.volumeUp(val);
58+
else if (val < 0) player.volumeDown(Math.abs(val));
59+
else Utils.invalidParameter(exchange, "step", "Must be non zero");
6560
} else {
6661
Utils.invalidParameter(exchange, "volume");
67-
return;
6862
}
6963
}
7064

lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>xyz.gianlu.librespot</groupId>
77
<artifactId>librespot-java</artifactId>
88
<version>1.5.3-SNAPSHOT</version>
9-
<relativePath>../</relativePath>
9+
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

1212
<artifactId>librespot-lib</artifactId>

lib/src/main/java/xyz/gianlu/librespot/audio/decrypt/AesAudioDecrypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AesAudioDecrypt(byte[] key) {
3737

3838
@Override
3939
public synchronized void decryptChunk(int chunkIndex, @NotNull byte[] buffer) throws IOException {
40-
BigInteger iv = IV_INT.add(BigInteger.valueOf(CHUNK_SIZE * chunkIndex / 16));
40+
BigInteger iv = IV_INT.add(BigInteger.valueOf((long) CHUNK_SIZE * chunkIndex / 16));
4141
try {
4242
long start = System.nanoTime();
4343
for (int i = 0; i < buffer.length; i += 4096) {

lib/src/main/java/xyz/gianlu/librespot/cache/CacheJournal.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ List<String> getEntries() throws IOException {
138138

139139
int i = 0;
140140
while (true) {
141-
io.seek(i * JOURNAL_ENTRY_SIZE);
141+
io.seek((long) i * JOURNAL_ENTRY_SIZE);
142142

143143
int first = io.read();
144144
if (first == -1) // EOF
@@ -178,7 +178,7 @@ private Entry find(@NotNull String id) throws IOException {
178178

179179
int i = 0;
180180
while (true) {
181-
io.seek(i * JOURNAL_ENTRY_SIZE);
181+
io.seek((long) i * JOURNAL_ENTRY_SIZE);
182182

183183
int first = io.read();
184184
if (first == -1) // EOF
@@ -208,7 +208,7 @@ void createIfNeeded(@NotNull String id) throws IOException {
208208

209209
int i = 0;
210210
while (true) {
211-
io.seek(i * JOURNAL_ENTRY_SIZE);
211+
io.seek((long) i * JOURNAL_ENTRY_SIZE);
212212

213213
int first = io.read();
214214
if (first == 0 || first == -1) { // First empty spot or EOF
@@ -280,7 +280,7 @@ void setHeader(int id, @NotNull String value) throws IOException {
280280
if (index == -1) throw new IllegalStateException();
281281
}
282282

283-
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + index * (MAX_HEADER_LENGTH + 1));
283+
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + (long) index * (MAX_HEADER_LENGTH + 1));
284284
io.write(id);
285285
io.write(value.getBytes(StandardCharsets.US_ASCII));
286286
}
@@ -308,7 +308,7 @@ JournalHeader getHeader(int id) throws IOException {
308308
int index = findHeader(id);
309309
if (index == -1) return null;
310310

311-
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + index * (MAX_HEADER_LENGTH + 1) + 1);
311+
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + (long) index * (MAX_HEADER_LENGTH + 1) + 1);
312312
byte[] read = new byte[MAX_HEADER_LENGTH];
313313
io.read(read);
314314

lib/src/main/java/xyz/gianlu/librespot/cache/CacheManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public boolean hasChunk(int index) throws IOException {
197197
updateTimestamp();
198198

199199
synchronized (io) {
200-
if (io.length() < (index + 1) * CHUNK_SIZE)
200+
if (io.length() < (long) (index + 1) * CHUNK_SIZE)
201201
return false;
202202
}
203203

@@ -219,7 +219,7 @@ public byte[] readChunk(int index) throws IOException, BadChunkHashException {
219219
updateTimestamp();
220220

221221
synchronized (io) {
222-
io.seek(index * CHUNK_SIZE);
222+
io.seek((long) index * CHUNK_SIZE);
223223

224224
byte[] buffer = new byte[CHUNK_SIZE];
225225
int read = io.read(buffer);
@@ -248,7 +248,7 @@ public byte[] readChunk(int index) throws IOException, BadChunkHashException {
248248

249249
public void writeChunk(byte[] buffer, int index) throws IOException {
250250
synchronized (io) {
251-
io.seek(index * CHUNK_SIZE);
251+
io.seek((long) index * CHUNK_SIZE);
252252
io.write(buffer);
253253
}
254254

lib/src/main/java/xyz/gianlu/librespot/common/ProtoUtils.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ public static Player.PlayOrigin convertPlayOrigin(@Nullable PlayOrigin po) {
165165

166166
Player.PlayOrigin.Builder builder = Player.PlayOrigin.newBuilder();
167167

168-
Optional.ofNullable(po.getFeatureIdentifier()).ifPresent(builder::setFeatureIdentifier);
169-
Optional.ofNullable(po.getFeatureVersion()).ifPresent(builder::setFeatureVersion);
170-
Optional.ofNullable(po.getViewUri()).ifPresent(builder::setViewUri);
171-
Optional.ofNullable(po.getExternalReferrer()).ifPresent(builder::setExternalReferrer);
172-
Optional.ofNullable(po.getReferrerIdentifier()).ifPresent(builder::setReferrerIdentifier);
173-
Optional.ofNullable(po.getDeviceIdentifier()).ifPresent(builder::setDeviceIdentifier);
168+
if (po.hasFeatureIdentifier()) builder.setFeatureIdentifier(po.getFeatureIdentifier());
169+
if (po.hasFeatureVersion()) builder.setFeatureVersion(po.getFeatureVersion());
170+
if (po.hasViewUri()) builder.setViewUri(po.getViewUri());
171+
if (po.hasExternalReferrer()) builder.setExternalReferrer(po.getExternalReferrer());
172+
if (po.hasReferrerIdentifier()) builder.setReferrerIdentifier(po.getReferrerIdentifier());
173+
if (po.hasDeviceIdentifier()) builder.setDeviceIdentifier(po.getDeviceIdentifier());
174174

175175
if (po.getFeatureClassesCount() > 0)
176176
for (String feature : po.getFeatureClassesList())
@@ -254,9 +254,11 @@ public static int indexOfTrackByUri(@NotNull List<ContextTrack> tracks, @NotNull
254254
}
255255

256256
public static boolean isQueued(@NotNull ContextTrack track) {
257-
String value = track.getMetadataOrDefault("is_queued", null);
258-
if (value == null) return false;
259-
else return Boolean.parseBoolean(value);
257+
try {
258+
return Boolean.parseBoolean(track.getMetadataOrThrow("is_queued"));
259+
} catch (IllegalArgumentException ex) {
260+
return false;
261+
}
260262
}
261263

262264
public static void enrichTrack(@NotNull ContextTrack.Builder subject, @NotNull ContextTrack track) {
@@ -283,10 +285,18 @@ public static Player.ProvidedTrack convertToProvidedTrack(@Nullable ContextTrack
283285

284286
Player.ProvidedTrack.Builder builder = Player.ProvidedTrack.newBuilder();
285287
builder.setProvider("context");
286-
Optional.ofNullable(track.getUri()).ifPresent(builder::setUri);
287-
Optional.ofNullable(track.getUid()).ifPresent(builder::setUid);
288-
Optional.ofNullable(track.getMetadataOrDefault("album_uri", null)).ifPresent(builder::setAlbumUri);
289-
Optional.ofNullable(track.getMetadataOrDefault("artist_uri", null)).ifPresent(builder::setArtistUri);
288+
if (track.hasUri()) builder.setUri(track.getUri());
289+
if (track.hasUid()) builder.setUid(track.getUid());
290+
291+
try {
292+
builder.setAlbumUri(track.getMetadataOrThrow("album_uri"));
293+
} catch (IllegalArgumentException ignored) {
294+
}
295+
296+
try {
297+
builder.setArtistUri(track.getMetadataOrThrow("artist_uri"));
298+
} catch (IllegalArgumentException ignored) {
299+
}
290300

291301
builder.putAllMetadata(track.getMetadataMap());
292302

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public final class Session implements Closeable, SubListener, DealerClient.Messa
8686
private final DiffieHellman keys;
8787
private final Inner inner;
8888
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NameThreadFactory(r -> "session-scheduler-" + r.hashCode()));
89-
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "handle-packet-" + r.hashCode()));
9089
private final AtomicBoolean authLock = new AtomicBoolean(false);
9190
private final OkHttpClient client;
9291
private final List<CloseListener> closeListeners = Collections.synchronizedList(new ArrayList<>());
@@ -450,8 +449,6 @@ public void close() throws IOException {
450449
receiver = null;
451450
}
452451

453-
executorService.shutdown();
454-
455452
client.dispatcher().executorService().shutdownNow();
456453
client.connectionPool().evictAll();
457454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private StoredToken(@NotNull JsonObject obj) {
8181
}
8282

8383
public boolean expired() {
84-
return timestamp + (expiresIn - TOKEN_EXPIRE_THRESHOLD) * 1000 < TimeProvider.currentTimeMillis();
84+
return timestamp + (expiresIn - TOKEN_EXPIRE_THRESHOLD) * 1000L < TimeProvider.currentTimeMillis();
8585
}
8686

8787
@Override

0 commit comments

Comments
 (0)