JDK里面有TimeUnit,看spark源码有个ByteUnit。这个类还是挺不错的。

public enum ByteUnit {
BYTE (1),
KiB (1024L),
MiB ((long) Math.pow(1024L, 2L)),
GiB ((long) Math.pow(1024L, 3L)),
TiB ((long) Math.pow(1024L, 4L)),
PiB ((long) Math.pow(1024L, 5L)); private ByteUnit(long multiplier) {
this.multiplier = multiplier;
} // Interpret the provided number (d) with suffix (u) as this unit type.
// E.g. KiB.interpret(1, MiB) interprets 1MiB as its KiB representation = 1024k
public long convertFrom(long d, ByteUnit u) {
return u.convertTo(d, this);
} // Convert the provided number (d) interpreted as this unit type to unit type (u).
public long convertTo(long d, ByteUnit u) {
if (multiplier > u.multiplier) {
long ratio = multiplier / u.multiplier;
if (Long.MAX_VALUE / ratio < d) {
throw new IllegalArgumentException("Conversion of " + d + " exceeds Long.MAX_VALUE in "
+ name() + ". Try a larger unit (e.g. MiB instead of KiB)");
}
return d * ratio;
} else {
// Perform operations in this order to avoid potential overflow
// when computing d * multiplier
return d / (u.multiplier / multiplier);
}
} public double toBytes(long d) {
if (d < 0) {
throw new IllegalArgumentException("Negative size value. Size must be positive: " + d);
}
return d * multiplier;
} public long toKiB(long d) { return convertTo(d, KiB); }
public long toMiB(long d) { return convertTo(d, MiB); }
public long toGiB(long d) { return convertTo(d, GiB); }
public long toTiB(long d) { return convertTo(d, TiB); }
public long toPiB(long d) { return convertTo(d, PiB); } private final long multiplier;
}

ByteUnit的更多相关文章

  1. UDP的使用

    // //  该类管理所有的UDP发送 #import <Foundation/Foundation.h> #import "AsyncUdpSocket.h" @pr ...

  2. 3G数据请求

    //  该类负责发送2G/3G Http请求的数据 #import <Foundation/Foundation.h> #import "ASIHTTPRequest.h&quo ...

  3. 【原创】大数据基础之Spark(2)Spark on Yarn:container memory allocation容器内存分配

    spark 2.1.1 最近spark任务(spark on yarn)有一个报错 Diagnostics: Container [pid=5901,containerID=container_154 ...

随机推荐

  1. ajax同步异步

    test.html <a href="javascript:void(0)" onmouseover="testAsync()"> asy.js f ...

  2. Nginx httpS server配置

    Nginx httpS 配置 配置同时支持http和httpS协议: server { listen ; #backlog:每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包 ...

  3. 【转】关于一个Jmeter interface testing的实例

    目标:测试某个保险系统的费率接口 准备:a 请求方式:Http b 接口地址://10.1.1.223:9090/rulesEngine/executeRateRule.do Jmeter 设置: a ...

  4. Vue.js:自定义指令

    ylbtech-Vue.js:自定义指令 1.返回顶部 1. Vue.js 自定义指令 除了默认设置的核心指令( v-model 和 v-show ), Vue 也允许注册自定义指令. 下面我们注册一 ...

  5. 1120 Friend Numbers

    题意:略. 思路:水题,略. 代码: #include <iostream> #include <string> using namespace std; ; }; int m ...

  6. PL/SQL 训练09--面向对象

    ---对象基本声明.实现.使用--对象类型,类似与JAVA中的类,通俗的讲,就是捆绑了相关函数和过程的记录类型. ---对象声明 --create type 创建一个对象类型的规范部分 create ...

  7. mysql ,show slave status详解

    ===想确认sql_thread线程是否应用完了io_thread接收到的了relay log,看 Master_Log_File=Relay_Master_Log_File , Read_Maste ...

  8. 012. MVC5中Razor引擎使用模板页

    1.文件→新建项目→框架选择.NET Framework 4.5 2.确定后选择ASP.NET 4.5 模板→MVC→为以下项添加文件夹和核心引用→MVC, 在vs 2015中默认就使用的Razor引 ...

  9. spring-cloud配置高可用eureka服务端

    spring-cloud配置eureka服务端 eureka用来发现其他程序 依赖 <?xml version="1.0" encoding="UTF-8" ...

  10. Tornado 高并发源码分析之四--- HTTPServer 与 TCPServer 对象

    主要工作: 服务器启动的时候做的事: 1.把包含了各种配置信息的 application 对象封装到了 HttpServer 对象的 request_callback 字段中,等待被调用 2.TCPS ...