VarInt.java

net.minecraft.network.VarInt

信息

  • 全限定名:net.minecraft.network.VarInt
  • 类型:public class
  • 包:net.minecraft.network
  • 源码路径:src/main/java/net/minecraft/network/VarInt.java
  • 起始行号:L5
  • 职责:

    TODO

字段/常量

  • MAX_VARINT_SIZE

    • 类型: int
    • 修饰符: public static final
    • 源码定位: L6
    • 说明:

      TODO

  • DATA_BITS_MASK

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L7
    • 说明:

      TODO

  • CONTINUATION_BIT_MASK

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L8
    • 说明:

      TODO

  • DATA_BITS_PER_BYTE

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L9
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static int getByteSize(int value) @ L11

  • 方法名:getByteSize
  • 源码定位:L11
  • 返回类型:int
  • 修饰符:public static

参数:

  • value: int

说明:

TODO

public static boolean hasContinuationBit(byte in) @ L21

  • 方法名:hasContinuationBit
  • 源码定位:L21
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • in: byte

说明:

TODO

public static int read(ByteBuf input) @ L25

  • 方法名:read
  • 源码定位:L25
  • 返回类型:int
  • 修饰符:public static

参数:

  • input: ByteBuf

说明:

TODO

public static ByteBuf write(ByteBuf output, int value) @ L41

  • 方法名:write
  • 源码定位:L41
  • 返回类型:ByteBuf
  • 修饰符:public static

参数:

  • output: ByteBuf
  • value: int

说明:

TODO

代码

public class VarInt {
    public static final int MAX_VARINT_SIZE = 5;
    private static final int DATA_BITS_MASK = 127;
    private static final int CONTINUATION_BIT_MASK = 128;
    private static final int DATA_BITS_PER_BYTE = 7;
 
    public static int getByteSize(int value) {
        for (int i = 1; i < 5; i++) {
            if ((value & -1 << i * 7) == 0) {
                return i;
            }
        }
 
        return 5;
    }
 
    public static boolean hasContinuationBit(byte in) {
        return (in & 128) == 128;
    }
 
    public static int read(ByteBuf input) {
        int out = 0;
        int bytes = 0;
 
        byte in;
        do {
            in = input.readByte();
            out |= (in & 127) << bytes++ * 7;
            if (bytes > 5) {
                throw new RuntimeException("VarInt too big");
            }
        } while (hasContinuationBit(in));
 
        return out;
    }
 
    public static ByteBuf write(ByteBuf output, int value) {
        while ((value & -128) != 0) {
            output.writeByte(value & 127 | 128);
            value >>>= 7;
        }
 
        output.writeByte(value);
        return output;
    }
}

引用的其他类