Package org.bitcoinj.base
Class VarInt
java.lang.Object
org.bitcoinj.base.VarInt
A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
fitsInt()
Determine if the value would fit an int, i.e.int
Returns the original number of bytes used to encode the value if it was deserialized from a byte array, or the minimum encoded size if it was not.final int
Returns the minimum encoded size of the value.int
hashCode()
int
intValue()
Gets the value as an unsigned int in the range of0
toInteger.MAX_VALUE
.long
Gets the value as a long.static VarInt
of
(long value) Constructs a new VarInt with the given unsigned long value.static VarInt
ofBytes
(byte[] buf, int offset) Constructs a new VarInt with the value parsed from the specified offset of the given buffer.static VarInt
read
(ByteBuffer buf) Constructs a new VarInt by reading from the given buffer.byte[]
Allocates a byte array and serializes the value into its minimal representation.static int
sizeOf
(long value) Returns the minimum encoded size of the given unsigned long value.toString()
write
(ByteBuffer buf) Write encoded value into the given buffer.
-
Constructor Details
-
VarInt
Deprecated.useof(long)
-
VarInt
Deprecated.
-
-
Method Details
-
of
Constructs a new VarInt with the given unsigned long value.- Parameters:
value
- the unsigned long value (beware widening conversion of negatives!)
-
ofBytes
Constructs a new VarInt with the value parsed from the specified offset of the given buffer.- Parameters:
buf
- the buffer containing the valueoffset
- the offset of the value- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the value doesn't fit the remaining buffer
-
read
Constructs a new VarInt by reading from the given buffer.- Parameters:
buf
- buffer to read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
longValue
public long longValue()Gets the value as a long. For values greater thanLong.MAX_VALUE
the returned long will be negative. It is still to be interpreted as an unsigned value.- Returns:
- value as a long
-
fitsInt
public boolean fitsInt()Determine if the value would fit an int, i.e. it is in the range of0
toInteger.MAX_VALUE
. If this is true, it's safe to callintValue()
.- Returns:
- true if the value fits an int, false otherwise
-
intValue
Gets the value as an unsigned int in the range of0
toInteger.MAX_VALUE
.- Returns:
- value as an unsigned int
- Throws:
ArithmeticException
- if the value doesn't fit an int
-
getOriginalSizeInBytes
public int getOriginalSizeInBytes()Returns the original number of bytes used to encode the value if it was deserialized from a byte array, or the minimum encoded size if it was not. -
getSizeInBytes
public final int getSizeInBytes()Returns the minimum encoded size of the value. -
sizeOf
public static int sizeOf(long value) Returns the minimum encoded size of the given unsigned long value.- Parameters:
value
- the unsigned long value (beware widening conversion of negatives!)
-
serialize
public byte[] serialize()Allocates a byte array and serializes the value into its minimal representation.- Returns:
- the minimal encoded bytes of the value
-
write
Write encoded value into the given buffer.- Parameters:
buf
- buffer to write into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
toString
-
equals
-
hashCode
public int hashCode()
-
ofBytes(byte[], int)