Class VarInt


  • public class VarInt
    extends java.lang.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)
      Constructs a new VarInt with the value parsed from the specified offset of the given buffer.
      VarInt​(long value)
      Constructs a new VarInt with the given unsigned long value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] encode()
      Encodes the value into its minimal representation.
      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 intValue()  
      long longValue()  
      static int sizeOf​(long value)
      Returns the minimum encoded size of the given unsigned long value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • VarInt

        public VarInt​(long value)
        Constructs a new VarInt with the given unsigned long value.
        Parameters:
        value - the unsigned long value (beware widening conversion of negatives!)
      • VarInt

        public VarInt​(byte[] buf,
                      int offset)
        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
    • Method Detail

      • longValue

        public long longValue()
      • intValue

        public int intValue()
      • 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!)
      • encode

        public byte[] encode()
        Encodes the value into its minimal representation.
        Returns:
        the minimal encoded bytes of the value