mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-11 17:55:53 +00:00
Improve VarInt: simplify, fix some minor bugs and increase test coverage.
This commit is contained in:
parent
debd034c71
commit
a504328044
@ -16,6 +16,8 @@
|
||||
|
||||
package com.google.bitcoin.core;
|
||||
|
||||
import static com.google.bitcoin.core.Utils.isLessThanUnsigned;
|
||||
|
||||
public class VarInt {
|
||||
public final long value;
|
||||
|
||||
@ -44,13 +46,12 @@ public class VarInt {
|
||||
}
|
||||
|
||||
public int getSizeInBytes() {
|
||||
// Java doesn't have the actual value of MAX_INT, as all types in Java
|
||||
// are signed *headsmash*.
|
||||
if (value < 253)
|
||||
// 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 (value <= 65536)
|
||||
else if (isLessThanUnsigned(value, 65536))
|
||||
return 3; // 1 marker + 2 data bytes
|
||||
else if (value <= 4294967295L)
|
||||
else if (isLessThanUnsigned(value, 4294967296L))
|
||||
return 5; // 1 marker + 4 data bytes
|
||||
else
|
||||
return 9; // 1 marker + 8 data bytes
|
||||
@ -63,11 +64,11 @@ public class VarInt {
|
||||
|
||||
|
||||
public byte[] encodeBE() {
|
||||
if (Utils.isLessThanUnsigned(value, 253)) {
|
||||
if (isLessThanUnsigned(value, 253)) {
|
||||
return new byte[] { (byte)value };
|
||||
} else if (Utils.isLessThanUnsigned(value, 65536)) {
|
||||
} else if (isLessThanUnsigned(value, 65536)) {
|
||||
return new byte[] { (byte) 253, (byte) (value), (byte) (value >> 8) };
|
||||
} else if (Utils.isLessThanUnsigned(value, 4294967295L)) {
|
||||
} else if (isLessThanUnsigned(value, 4294967295L)) {
|
||||
byte[] bytes = new byte[5];
|
||||
bytes[0] = (byte) 254;
|
||||
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
||||
@ -75,8 +76,8 @@ public class VarInt {
|
||||
} else {
|
||||
byte[] bytes = new byte[9];
|
||||
bytes[0] = (byte) 255;
|
||||
Utils.uint32ToByteArrayLE(value & 0xFFFFFFFF, bytes, 1);
|
||||
Utils.uint32ToByteArrayLE(value >> 32, bytes, 5);
|
||||
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
||||
Utils.uint32ToByteArrayLE(value >>> 32, bytes, 5);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
@ -19,25 +19,33 @@ package com.google.bitcoin.core;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class VarIntTest extends TestCase {
|
||||
public void testVarInts() throws Exception {
|
||||
VarInt a;
|
||||
|
||||
// Bytes
|
||||
a = new VarInt(10);
|
||||
public void testBytes() throws Exception {
|
||||
VarInt a = new VarInt(10);
|
||||
assertEquals(1, a.getSizeInBytes());
|
||||
assertEquals(1, a.encode().length);
|
||||
assertEquals(10, new VarInt(a.encode(), 0).value);
|
||||
}
|
||||
|
||||
// Shorts
|
||||
a = new VarInt(64000);
|
||||
public void testShorts() throws Exception {
|
||||
VarInt a = new VarInt(64000);
|
||||
assertEquals(3, a.getSizeInBytes());
|
||||
assertEquals(3, a.encode().length);
|
||||
assertEquals(64000, new VarInt(a.encode(), 0).value);
|
||||
}
|
||||
|
||||
a = new VarInt(0xAABBCCDDL);
|
||||
public void testInts() throws Exception {
|
||||
VarInt a = new VarInt(0xAABBCCDDL);
|
||||
assertEquals(5, a.getSizeInBytes());
|
||||
assertEquals(5, a.encode().length);
|
||||
byte[] bytes = a.encode();
|
||||
assertEquals(0xAABBCCDDL, 0xFFFFFFFFL & new VarInt(bytes, 0).value);
|
||||
}
|
||||
|
||||
public void testLong() throws Exception {
|
||||
VarInt a = new VarInt(0xCAFEBABEDEADBEEFL);
|
||||
assertEquals(9, a.getSizeInBytes());
|
||||
assertEquals(9, a.encode().length);
|
||||
byte[] bytes = a.encode();
|
||||
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user