Unit.java

com.mojang.realmsclient.Unit

信息

  • 全限定名:com.mojang.realmsclient.Unit
  • 类型:public enum
  • 包:com.mojang.realmsclient
  • 源码路径:src/main/java/com/mojang/realmsclient/Unit.java
  • 起始行号:L8
  • 职责:

    TODO

字段/常量

  • B, KB, MB, GB

    • 类型: Unit
    • 修饰符: package-private
    • 源码定位: L9
    • 说明:

      TODO

  • BASE_UNIT

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

      TODO

内部类/嵌套类型

构造器

方法

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

public static Unit getLargest(long bytes) @ L16

  • 方法名:getLargest
  • 源码定位:L16
  • 返回类型:Unit
  • 修饰符:public static

参数:

  • bytes: long

说明:

TODO

public static double convertTo(long bytes, Unit unit) @ L30

  • 方法名:convertTo
  • 源码定位:L30
  • 返回类型:double
  • 修饰符:public static

参数:

  • bytes: long
  • unit: Unit

说明:

TODO

public static String humanReadable(long bytes) @ L34

  • 方法名:humanReadable
  • 源码定位:L34
  • 返回类型:String
  • 修饰符:public static

参数:

  • bytes: long

说明:

TODO

public static String humanReadable(long bytes, Unit unit) @ L45

  • 方法名:humanReadable
  • 源码定位:L45
  • 返回类型:String
  • 修饰符:public static

参数:

  • bytes: long
  • unit: Unit

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public enum Unit {
    B,
    KB,
    MB,
    GB;
 
    private static final int BASE_UNIT = 1024;
 
    public static Unit getLargest(long bytes) {
        if (bytes < 1024L) {
            return B;
        } else {
            try {
                int exp = (int)(Math.log(bytes) / Math.log(1024.0));
                String pre = String.valueOf("KMGTPE".charAt(exp - 1));
                return valueOf(pre + "B");
            } catch (Exception var4) {
                return GB;
            }
        }
    }
 
    public static double convertTo(long bytes, Unit unit) {
        return unit == B ? bytes : bytes / Math.pow(1024.0, unit.ordinal());
    }
 
    public static String humanReadable(long bytes) {
        int unit = 1024;
        if (bytes < 1024L) {
            return bytes + " B";
        } else {
            int exp = (int)(Math.log(bytes) / Math.log(1024.0));
            String pre = "KMGTPE".charAt(exp - 1) + "";
            return String.format(Locale.ROOT, "%.1f %sB", bytes / Math.pow(1024.0, exp), pre);
        }
    }
 
    public static String humanReadable(long bytes, Unit unit) {
        return String.format(Locale.ROOT, "%." + (unit == GB ? "1" : "0") + "f %s", convertTo(bytes, unit), unit.name());
    }
}

引用的其他类