Skip to content

Commit c8e40b4

Browse files
committed
Fixed #79
1 parent 900c44b commit c8e40b4

6 files changed

Lines changed: 23 additions & 14 deletions

File tree

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

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

203203
@NotNull
204204
public static String bytesToHex(byte[] bytes) {
205-
return bytesToHex(bytes, 0, bytes.length, false);
205+
return bytesToHex(bytes, 0, bytes.length, false, -1);
206206
}
207207

208208
@NotNull
209209
public static String bytesToHex(byte[] bytes, boolean trim) {
210-
return bytesToHex(bytes, 0, bytes.length, trim);
210+
return bytesToHex(bytes, 0, bytes.length, trim, -1);
211211
}
212212

213213
@NotNull
214-
public static String bytesToHex(byte[] bytes, int offset, int length, boolean trim) {
214+
public static String bytesToHex(byte[] bytes, boolean trim, int minLength) {
215+
return bytesToHex(bytes, 0, bytes.length, trim, minLength);
216+
}
217+
218+
@NotNull
219+
public static String bytesToHex(byte[] bytes, int offset, int length, boolean trim, int minLength) {
215220
if (bytes == null) return "";
216221

217222
int newOffset = 0;
@@ -221,7 +226,11 @@ public static String bytesToHex(byte[] bytes, int offset, int length, boolean tr
221226
int v = bytes[j] & 0xFF;
222227
if (trimming) {
223228
if (v == 0) {
224-
newOffset = j + 1;
229+
newOffset = j + 1;
230+
231+
if (minLength != -1 && length - newOffset == minLength)
232+
trimming = false;
233+
225234
continue;
226235
} else {
227236
trimming = false;

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()), true));
27+
return new AlbumId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true, 16));
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()), true));
35+
return new AlbumId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true, 16));
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()), true));
27+
return new ArtistId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true, 16));
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()), true));
35+
return new ArtistId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true, 16));
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()), true));
28+
return new EpisodeId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true, 16));
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()), true));
47+
return new EpisodeId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true, 16));
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()), true));
27+
return new ShowId(Utils.bytesToHex(BASE62.decode(id.getBytes()), true, 16));
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()), true));
35+
return new ShowId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true, 16));
3636
}
3737

3838
@NotNull

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

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

3434
@NotNull
3535
public static TrackId fromBase62(@NotNull String base62) {
36-
return new TrackId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true));
36+
return new TrackId(Utils.bytesToHex(BASE62.decode(base62.getBytes()), true, 16));
3737
}
3838

3939
@NotNull

0 commit comments

Comments
 (0)