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. 列表推导式,两个for循环的例子

    [a for a in alist for b in blist if a>b] for i in alist,blist: print(i) >>  alist[] >> ...

  2. Weblogic配置SSl使用Https

    一 .可以开启自带的SSL连接 启动weblogic,进入左侧菜单,点击左侧的安全领域-->点击myrealm-->点击角色和策略-->点击服务器AdminServer 点击保存,w ...

  3. Python 函数 -next()

    next() next() 返回迭代器的下一个项目. iterator - 可迭代对象.  default - 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 St ...

  4. Hadoop体系结构之 Yarn

    1.1 YARN 基本架构 YARN是Hadoop 2.0中的资源管理系统,它的基本设计思想是将MRv1中的JobTracker拆分成了两个独立的服务:一个全局的资源管理器ResourceManage ...

  5. 基于ThinkPHP的开发笔记3-登录功能(转)

    1.前台登录用的form ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <for ...

  6. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  7. PHP生成唯一固定长度邀请码

    function create_invite_code() { $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand = $code[rand(0,25)] .str ...

  8. 在rac集群上开启OEM

    由于安装rac的时候没有开启oem,这里开启oem,方便管理 [oracle@rac01 ~]$ emca -config dbcontrol db -repos create -cluster ST ...

  9. PHP 简单实现webSocket

    费话少说,用源代码说话 1)客户端实现 1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 <tit ...

  10. AJAX中的跨域问题:什么是跨域?如何解决跨域问题?

    域不一样的,即为跨域,包括(协议,域名,端口号) 1. 指定允许其他域名访问 header('Access-Control-Allow-Origin:*'); 2.使用jsonp