Class VarInt

java.lang.Object
org.bitcoinj.base.VarInt

public class VarInt extends Object
A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
  • Constructor Summary

    Constructors
    Constructor
    Description
    VarInt(byte[] buf, int offset)
    Deprecated.
    VarInt(long value)
    Deprecated.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    boolean
    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
     
    int
    Gets the value as an unsigned int in the range of 0 to Integer.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
    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.
     
    Write encoded value into the given buffer.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • 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 ArrayIndexOutOfBoundsException
      Constructs a new VarInt with the value parsed from the specified offset of the given buffer.
      Parameters:
      buf - the buffer containing the value
      offset - 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

      public static VarInt read(ByteBuffer buf) throws BufferUnderflowException
      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 than Long.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 of 0 to Integer.MAX_VALUE. If this is true, it's safe to call intValue().
      Returns:
      true if the value fits an int, false otherwise
    • intValue

      public int intValue() throws ArithmeticException
      Gets the value as an unsigned int in the range of 0 to Integer.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

      public ByteBuffer write(ByteBuffer buf) throws BufferOverflowException
      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

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object