VarLong.java

net.minecraft.network.VarLong

信息

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

    TODO

字段/常量

  • MAX_VARLONG_SIZE

    • 类型: int
    • 修饰符: private 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(long value) @ L11

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

参数:

  • value: long

说明:

TODO

public static boolean hasContinuationBit(byte in) @ L21

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

参数:

  • in: byte

说明:

TODO

public static long read(ByteBuf input) @ L25

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

参数:

  • input: ByteBuf

说明:

TODO

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

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

参数:

  • output: ByteBuf
  • value: long

说明:

TODO

代码

public class VarLong {
    private static final int MAX_VARLONG_SIZE = 10;
    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(long value) {
        for (int i = 1; i < 10; i++) {
            if ((value & -1L << i * 7) == 0L) {
                return i;
            }
        }
 
        return 10;
    }
 
    public static boolean hasContinuationBit(byte in) {
        return (in & 128) == 128;
    }
 
    public static long read(ByteBuf input) {
        long out = 0L;
        int bytes = 0;
 
        byte in;
        do {
            in = input.readByte();
            out |= (long)(in & 127) << bytes++ * 7;
            if (bytes > 10) {
                throw new RuntimeException("VarLong too big");
            }
        } while (hasContinuationBit(in));
 
        return out;
    }
 
    public static ByteBuf write(ByteBuf output, long value) {
        while ((value & -128L) != 0L) {
            output.writeByte((int)(value & 127L) | 128);
            value >>>= 7;
        }
 
        output.writeByte((int)value);
        return output;
    }
}

引用的其他类