3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 23:03:04 +00:00

Use a bit of Guava for unsigned longs. Resolves issue 367.

This commit is contained in:
Mike Hearn 2013-03-29 16:41:28 +00:00
parent 4f1c7f4816
commit 7b8eab19ff
2 changed files with 6 additions and 6 deletions

View File

@ -16,7 +16,7 @@
package com.google.bitcoin.core;
import com.google.common.util.concurrent.CycleDetectingLockFactory;
import com.google.common.primitives.UnsignedLongs;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.util.encoders.Hex;
@ -202,7 +202,7 @@ public class Utils {
* Work around lack of unsigned types in Java.
*/
public static boolean isLessThanUnsigned(long n1, long n2) {
return (n1 < n2) ^ ((n1 < 0) != (n2 < 0));
return UnsignedLongs.compare(n1, n2) < 0;
}
/**

View File

@ -16,6 +16,8 @@
package com.google.bitcoin.core;
import com.google.common.primitives.UnsignedInteger;
import static com.google.bitcoin.core.Utils.isLessThanUnsigned;
/**
@ -73,7 +75,6 @@ public class VarInt {
* Gets the minimum encoded size of the given value.
*/
public static int sizeOf(int value) {
// Java doesn't have the actual value of MAX_INT, as all types in Java are signed.
if (value < 253)
return 1;
else if (value < 65536)
@ -85,12 +86,11 @@ public class VarInt {
* Gets the minimum encoded size of the given value.
*/
public static int sizeOf(long value) {
// Java doesn't have the actual value of MAX_INT, as all types in Java are signed.
if (isLessThanUnsigned(value, 253))
return 1;
else if (isLessThanUnsigned(value, 65536))
return 3; // 1 marker + 2 data bytes
else if (isLessThanUnsigned(value, 4294967296L))
else if (isLessThanUnsigned(value, UnsignedInteger.MAX_VALUE.longValue()))
return 5; // 1 marker + 4 data bytes
else
return 9; // 1 marker + 8 data bytes
@ -106,7 +106,7 @@ public class VarInt {
return new byte[]{(byte) value};
} else if (isLessThanUnsigned(value, 65536)) {
return new byte[]{(byte) 253, (byte) (value), (byte) (value >> 8)};
} else if (isLessThanUnsigned(value, 4294967295L)) {
} else if (isLessThanUnsigned(value, UnsignedInteger.MAX_VALUE.longValue())) {
byte[] bytes = new byte[5];
bytes[0] = (byte) 254;
Utils.uint32ToByteArrayLE(value, bytes, 1);