Skip to content

Commit c035b0c

Browse files
committed
Utils#hexToBytes
1 parent 52c7d99 commit c035b0c

5 files changed

Lines changed: 15 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ public static String[] split(@NotNull String str, char c) {
151151
return split;
152152
}
153153

154+
public static byte[] hexToBytes(@NotNull String str) {
155+
int len = str.length();
156+
byte[] data = new byte[len / 2];
157+
for (int i = 0; i < len; i += 2)
158+
data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character.digit(str.charAt(i + 1), 16));
159+
return data;
160+
}
161+
154162
@NotNull
155163
public static byte[] toByteArray(@NotNull BigInteger i) {
156164
byte[] array = i.toByteArray();
@@ -166,7 +174,7 @@ public static byte[] toByteArray(int i) {
166174
}
167175

168176
@NotNull
169-
public static String bytesToHex(ByteString bytes) {
177+
public static String bytesToHex(@NotNull ByteString bytes) {
170178
return bytesToHex(bytes.toByteArray());
171179
}
172180

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.jetbrains.annotations.NotNull;
55
import xyz.gianlu.librespot.common.Utils;
66

7-
import java.math.BigInteger;
87
import java.util.regex.Matcher;
98
import java.util.regex.Pattern;
109

@@ -48,6 +47,6 @@ public static AlbumId fromHex(@NotNull String hex) {
4847

4948
@Override
5049
public @NotNull String toSpotifyUri() {
51-
return "spotify:album:" + new String(BASE62.encode(new BigInteger(hexId, 16).toByteArray()));
50+
return "spotify:album:" + new String(BASE62.encode(Utils.hexToBytes(hexId)));
5251
}
5352
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.jetbrains.annotations.NotNull;
55
import xyz.gianlu.librespot.common.Utils;
66

7-
import java.math.BigInteger;
87
import java.util.regex.Matcher;
98
import java.util.regex.Pattern;
109

@@ -48,6 +47,6 @@ public static ArtistId fromHex(@NotNull String hex) {
4847

4948
@Override
5049
public @NotNull String toSpotifyUri() {
51-
return "spotify:artist:" + new String(BASE62.encode(new BigInteger(hexId, 16).toByteArray()));
50+
return "spotify:artist:" + new String(BASE62.encode(Utils.hexToBytes(hexId)));
5251
}
5352
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import xyz.gianlu.librespot.common.Utils;
66
import xyz.gianlu.librespot.common.proto.Spirc;
77

8-
import java.math.BigInteger;
98
import java.util.regex.Matcher;
109
import java.util.regex.Pattern;
1110

@@ -40,7 +39,7 @@ public static TrackId fromBase62(@NotNull String base62) {
4039
@NotNull
4140
public static TrackId fromTrackRef(@NotNull Spirc.TrackRef ref) {
4241
if (ref.hasGid()) {
43-
return new TrackId(Utils.bytesToHex(ref.getGid().toByteArray()));
42+
return new TrackId(Utils.bytesToHex(ref.getGid()));
4443
} else if (ref.hasUri()) {
4544
return fromUri(ref.getUri());
4645
} else {
@@ -60,10 +59,10 @@ public static TrackId fromHex(@NotNull String hex) {
6059

6160
@Override
6261
public @NotNull String toSpotifyUri() {
63-
return "spotify:track:" + new String(BASE62.encode(new BigInteger(hexId, 16).toByteArray()));
62+
return "spotify:track:" + new String(BASE62.encode(Utils.hexToBytes(hexId)));
6463
}
6564

6665
public byte[] getGid() {
67-
return Utils.toByteArray(new BigInteger(hexId, 16));
66+
return Utils.hexToBytes(hexId);
6867
}
6968
}

core/src/main/java/xyz/gianlu/librespot/player/CacheManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private class CacheEntry {
178178

179179
CacheEntry(@NotNull String hexId, byte[] headersId, byte[][] headersData, short chunksSize) {
180180
this.hexId = hexId;
181-
this.gid = ByteString.copyFrom(new BigInteger(hexId, 16).toByteArray());
181+
this.gid = ByteString.copyFrom(Utils.hexToBytes(hexId));
182182
this.headersId = headersId;
183183
this.headersData = headersData;
184184
this.chunks = new boolean[chunksSize];

0 commit comments

Comments
 (0)