Class VarInt

java.lang.Object
com.hypixel.hytale.protocol.io.VarInt

public final class VarInt extends Object
Static helpers for VarInt encoding/decoding.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    get(MemorySegment mem, int offset)
     
    static long
    getWithLength(MemorySegment mem, int offset)
    Read a varint value and its encoded byte length in a single pass.
    static int
    length(io.netty.buffer.ByteBuf buf, int index)
    Get varint byte length at index.
    static int
    length(MemorySegment mem, int offset)
     
    static int
    peek(io.netty.buffer.ByteBuf buf, int index)
    Peek varint at absolute index without moving reader.
    static int
    read(io.netty.buffer.ByteBuf buf)
    Read a varint from the buffer, advancing the reader index.
    static int
    set(MemorySegment mem, int offset, int value)
     
    static int
    size(int value)
    Compute the number of bytes required to encode a varint value.
    static void
    write(io.netty.buffer.ByteBuf buf, int value)
    Write a varint to the buffer.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • write

      public static void write(@Nonnull io.netty.buffer.ByteBuf buf, int value)
      Write a varint to the buffer.
      Throws:
      IllegalArgumentException - if value is negative
    • read

      public static int read(@Nonnull io.netty.buffer.ByteBuf buf)
      Read a varint from the buffer, advancing the reader index.
      Throws:
      ProtocolException - if the varint encoding exceeds 5 bytes
    • peek

      public static int peek(@Nonnull io.netty.buffer.ByteBuf buf, int index)
      Peek varint at absolute index without moving reader.
      Returns:
      the varint value, or -1 if invalid/incomplete
    • length

      public static int length(@Nonnull io.netty.buffer.ByteBuf buf, int index)
      Get varint byte length at index.
      Returns:
      the byte length, or -1 if invalid
    • length

      public static int length(@Nonnull MemorySegment mem, int offset)
    • set

      public static int set(@Nonnull MemorySegment mem, int offset, int value)
    • get

      public static int get(@Nonnull MemorySegment mem, int offset)
    • getWithLength

      public static long getWithLength(@Nonnull MemorySegment mem, int offset)
      Read a varint value and its encoded byte length in a single pass. Returns a packed long: lower 32 bits = varint value, bits 32-34 = byte length (1-5). Returns -1L if the varint is invalid or incomplete.

      Extract with: int value = (int) packed; and int length = (int) (packed >>> 32);

    • size

      public static int size(int value)
      Compute the number of bytes required to encode a varint value.