Skip to content

Commit 02d2089

Browse files
committed
Fixed issue where gid where 17 (not 16) bytes long (closes #72)
1 parent e2da7a6 commit 02d2089

6 files changed

Lines changed: 30 additions & 17 deletions

File tree

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,37 @@ public static String bytesToHex(@NotNull ByteString bytes) {
202202

203203
@NotNull
204204
public static String bytesToHex(byte[] bytes) {
205-
if (bytes == null) return "";
206-
return bytesToHex(bytes, 0, bytes.length);
205+
return bytesToHex(bytes, 0, bytes.length, false);
206+
}
207+
208+
@NotNull
209+
public static String bytesToHex(byte[] bytes, boolean trim) {
210+
return bytesToHex(bytes, 0, bytes.length, trim);
207211
}
208212

209213
@NotNull
210-
public static String bytesToHex(byte[] bytes, int offset, int length) {
214+
public static String bytesToHex(byte[] bytes, int offset, int length, boolean trim) {
211215
if (bytes == null) return "";
212216

217+
int newOffset = 0;
218+
boolean trimming = trim;
213219
char[] hexChars = new char[length * 2];
214220
for (int j = offset; j < length; j++) {
215221
int v = bytes[j] & 0xFF;
222+
if (trimming) {
223+
if (v == 0) {
224+
newOffset = j + 1;
225+
continue;
226+
} else {
227+
trimming = false;
228+
}
229+
}
230+
216231
hexChars[j * 2] = hexArray[v >>> 4];
217232
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
218233
}
219234

220-
return new String(hexChars);
235+
return new String(hexChars, newOffset * 2, hexChars.length - newOffset * 2);
221236
}
222237

223238
@Contract("_, _, !null -> !null")

core/src/main/java/xyz/gianlu/librespot/mercury/model/AlbumId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public static AlbumId fromUri(@NotNull String uri) {
2424
Matcher matcher = PATTERN.matcher(uri);
2525
if (matcher.find()) {
2626
String id = matcher.group(1);
27-
return new AlbumId(Utils.bytesToHex(BASE62.decode(id.getBytes())));
27+
return new AlbumId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true));
2828
} else {
2929
throw new IllegalArgumentException("Not a Spotify album ID: " + uri);
3030
}
3131
}
3232

3333
@NotNull
3434
public static AlbumId fromBase62(@NotNull String base62) {
35-
return new AlbumId(Utils.bytesToHex(BASE62.decode(base62.getBytes())));
35+
return new AlbumId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
3636
}
3737

3838
@NotNull

core/src/main/java/xyz/gianlu/librespot/mercury/model/ArtistId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public static ArtistId fromUri(@NotNull String uri) {
2424
Matcher matcher = PATTERN.matcher(uri);
2525
if (matcher.find()) {
2626
String id = matcher.group(1);
27-
return new ArtistId(Utils.bytesToHex(BASE62.decode(id.getBytes())));
27+
return new ArtistId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true));
2828
} else {
2929
throw new IllegalArgumentException("Not a Spotify artist ID: " + uri);
3030
}
3131
}
3232

3333
@NotNull
3434
public static ArtistId fromBase62(@NotNull String base62) {
35-
return new ArtistId(Utils.bytesToHex(BASE62.decode(base62.getBytes())));
35+
return new ArtistId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
3636
}
3737

3838
@NotNull

core/src/main/java/xyz/gianlu/librespot/mercury/model/EpisodeId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static EpisodeId fromUri(@NotNull String uri) {
2525
Matcher matcher = PATTERN.matcher(uri);
2626
if (matcher.find()) {
2727
String id = matcher.group(1);
28-
return new EpisodeId(Utils.bytesToHex(BASE62.decode(id.getBytes())));
28+
return new EpisodeId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true));
2929
} else {
3030
throw new IllegalArgumentException("Not a Spotify episode ID: " + uri);
3131
}
@@ -44,7 +44,7 @@ public static EpisodeId fromTrackRef(@NotNull Spirc.TrackRef ref) {
4444

4545
@NotNull
4646
public static EpisodeId fromBase62(@NotNull String base62) {
47-
return new EpisodeId(Utils.bytesToHex(BASE62.decode(base62.getBytes())));
47+
return new EpisodeId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
4848
}
4949

5050
@NotNull

core/src/main/java/xyz/gianlu/librespot/mercury/model/ShowId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public static ShowId fromUri(@NotNull String uri) {
2424
Matcher matcher = PATTERN.matcher(uri);
2525
if (matcher.find()) {
2626
String id = matcher.group(1);
27-
return new ShowId(Utils.bytesToHex(BASE62.decode(id.getBytes())));
27+
return new ShowId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true));
2828
} else {
2929
throw new IllegalArgumentException("Not a Spotify show ID: " + uri);
3030
}
3131
}
3232

3333
@NotNull
3434
public static ShowId fromBase62(@NotNull String base62) {
35-
return new ShowId(Utils.bytesToHex(BASE62.decode(base62.getBytes())));
35+
return new ShowId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
3636
}
3737

3838
@NotNull

core/src/main/java/xyz/gianlu/librespot/mercury/model/TrackId.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,23 @@ public final class TrackId implements SpotifyId, PlayableId {
1717
private final String hexId;
1818

1919
private TrackId(@NotNull String hex) {
20-
if (hex.length() == 32) this.hexId = hex.toLowerCase();
21-
else if (hex.length() == 34 && hex.startsWith("00")) this.hexId = hex.substring(2).toLowerCase();
22-
else throw new IllegalArgumentException("Illegal track id: " + hex);
20+
this.hexId = hex.toLowerCase();
2321
}
2422

2523
@NotNull
2624
public static TrackId fromUri(@NotNull String uri) {
2725
Matcher matcher = PATTERN.matcher(uri);
2826
if (matcher.find()) {
2927
String id = matcher.group(1);
30-
return new TrackId(Utils.bytesToHex(BASE62.decode(id.getBytes())));
28+
return new TrackId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true));
3129
} else {
3230
throw new IllegalArgumentException("Not a Spotify track ID: " + uri);
3331
}
3432
}
3533

3634
@NotNull
3735
public static TrackId fromBase62(@NotNull String base62) {
38-
return new TrackId(Utils.bytesToHex(BASE62.decode(base62.getBytes())));
36+
return new TrackId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
3937
}
4038

4139
@NotNull

0 commit comments

Comments
 (0)