Package org.bitcoinj.base
Class VarInt
- java.lang.Object
-
- org.bitcoinj.base.VarInt
-
public class VarInt extends java.lang.Object
A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o)
boolean
fitsInt()
Determine if the value would fit an int, i.e.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.int
getSizeInBytes()
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
longValue()
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(java.nio.ByteBuffer buf)
Constructs a new VarInt by reading from the given buffer.byte[]
serialize()
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.java.lang.String
toString()
java.nio.ByteBuffer
write(java.nio.ByteBuffer buf)
Write encoded value into the given buffer.
-
-
-
Constructor Detail
-
VarInt
@Deprecated public VarInt(long value)
Deprecated.useof(long)
-
VarInt
@Deprecated public VarInt(byte[] buf, int offset)
Deprecated.
-
-
Method Detail
-
of
public static VarInt of(long value)
Constructs a new VarInt with the given unsigned long value.- Parameters:
value
- the unsigned long value (beware widening conversion of negatives!)
-
ofBytes
public static VarInt ofBytes(byte[] buf, int offset) throws java.lang.ArrayIndexOutOfBoundsException
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:
java.lang.ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the value doesn't fit the remaining buffer
-
read
public static VarInt read(java.nio.ByteBuffer buf) throws java.nio.BufferUnderflowException
Constructs a new VarInt by reading from the given buffer.- Parameters:
buf
- buffer to read from- Throws:
java.nio.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
public int intValue() throws java.lang.ArithmeticException
Gets the value as an unsigned int in the range of0
toInteger.MAX_VALUE
.- Returns:
- value as an unsigned int
- Throws:
java.lang.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
public java.nio.ByteBuffer write(java.nio.ByteBuffer buf) throws java.nio.BufferOverflowException
Write encoded value into the given buffer.- Parameters:
buf
- buffer to write into- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException
- if the value doesn't fit the remaining buffer
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-