Skip to content

Commit 0d3799b

Browse files
committed
#73 Change code to be compatible with Android #31
1 parent 6e6b59b commit 0d3799b

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/main/java/org/unicitylabs/sdk/api/jsonrpc/JsonRpcHttpTransport.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import okhttp3.*;
55
import org.unicitylabs.sdk.serializer.UnicityObjectMapper;
66

7+
import java.io.ByteArrayOutputStream;
78
import java.io.IOException;
89
import java.io.InputStream;
910
import java.nio.charset.StandardCharsets;
@@ -158,13 +159,20 @@ private String readBounded(ResponseBody body) throws IOException {
158159
}
159160

160161
try (InputStream in = body.byteStream()) {
161-
// Read one byte past the limit: if that many bytes are available the body is too large.
162-
byte[] bytes = in.readNBytes(this.maxResponseBytes + 1);
163-
if (bytes.length > this.maxResponseBytes) {
164-
throw new IOException("JSON-RPC response exceeds the maximum allowed size.");
162+
ByteArrayOutputStream out = new ByteArrayOutputStream();
163+
byte[] buffer = new byte[8192];
164+
int total = 0;
165+
int read;
166+
while ((read = in.read(buffer)) != -1) {
167+
total += read;
168+
if (total > this.maxResponseBytes) {
169+
throw new IOException("JSON-RPC response exceeds the maximum allowed size.");
170+
}
171+
out.write(buffer, 0, read);
165172
}
166173

167-
return new String(bytes, StandardCharsets.UTF_8);
174+
// ByteArrayOutputStream.toString(Charset) is post-API-31; construct the String directly.
175+
return new String(out.toByteArray(), StandardCharsets.UTF_8);
168176
}
169177
}
170178
}

src/main/java/org/unicitylabs/sdk/payment/asset/PaymentAssetCollection.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,17 @@ public static PaymentAssetCollection fromCbor(byte[] bytes) {
6868
* order of the raw id bytes, a shorter id ordered before a longer one that it is a prefix of.
6969
*/
7070
private static int compareAssets(Asset a, Asset b) {
71-
return Arrays.compareUnsigned(a.getId().getBytes(), b.getId().getBytes());
71+
byte[] x = a.getId().getBytes();
72+
byte[] y = b.getId().getBytes();
73+
int length = Math.min(x.length, y.length);
74+
for (int i = 0; i < length; i++) {
75+
int diff = (x[i] & 0xFF) - (y[i] & 0xFF);
76+
if (diff != 0) {
77+
return diff;
78+
}
79+
}
80+
81+
return x.length - y.length;
7282
}
7383

7484
private static PaymentAssetCollection fromList(List<Asset> assets) {

0 commit comments

Comments
 (0)