From 7b8eab19ff1d25f94d2f37f2bb32243970be69e0 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 29 Mar 2013 16:41:28 +0000 Subject: [PATCH] Use a bit of Guava for unsigned longs. Resolves issue 367. --- core/src/main/java/com/google/bitcoin/core/Utils.java | 4 ++-- core/src/main/java/com/google/bitcoin/core/VarInt.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Utils.java b/core/src/main/java/com/google/bitcoin/core/Utils.java index 7f8d68a9..f1a06ea2 100644 --- a/core/src/main/java/com/google/bitcoin/core/Utils.java +++ b/core/src/main/java/com/google/bitcoin/core/Utils.java @@ -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; } /** diff --git a/core/src/main/java/com/google/bitcoin/core/VarInt.java b/core/src/main/java/com/google/bitcoin/core/VarInt.java index 2b1b4b66..e315dde1 100644 --- a/core/src/main/java/com/google/bitcoin/core/VarInt.java +++ b/core/src/main/java/com/google/bitcoin/core/VarInt.java @@ -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);