Class 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
      byte[] encode()
      Encodes the value into its minimal representation.
      boolean equals​(java.lang.Object o)  
      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()  
      long longValue()  
      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.
      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.
      • Methods inherited from class java.lang.Object

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

      • VarInt

        @Deprecated
        public VarInt​(long value)
        Deprecated.
      • 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 value
        offset - 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()
      • 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
      • 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 class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object