ByteUnit
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的更多相关文章
- UDP的使用
// // 该类管理所有的UDP发送 #import <Foundation/Foundation.h> #import "AsyncUdpSocket.h" @pr ...
- 3G数据请求
// 该类负责发送2G/3G Http请求的数据 #import <Foundation/Foundation.h> #import "ASIHTTPRequest.h&quo ...
- 【原创】大数据基础之Spark(2)Spark on Yarn:container memory allocation容器内存分配
spark 2.1.1 最近spark任务(spark on yarn)有一个报错 Diagnostics: Container [pid=5901,containerID=container_154 ...
随机推荐
- NSArray倒序输出的方法
NSMutableArray *array = [NSMutableArray arrayWithObjects:",nil]; NSArray* reversedArray = [[arr ...
- 提示“load System.Core failed”
Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec8 ...
- SpringMVC解决跨域问题及CROS
CORS 首先因为最近在做一个前后端分离的项目,分开就意味着可能不在一个域中,所以不可避免的遇到CORS的问题.试过几个方法: Spring MVC 4.2.5以后新增的支持跨域的注解@CrossOr ...
- (转)C# Socket异步通信
本文转载自:http://www.cnblogs.com/llllll/archive/2009/05/13/1455703.html 服务器端 TCPServer 1.使用的通讯通道:socket ...
- 让你的 Nginx 的 RTMP 直播具有统计某频道在线观看用户数量的功能
你的 Nginx 已经有了 RTMP 直播功能的话,如果你还想统计某直播频道当前观看用户量的话,可以加入 with-http_xslt_module 模块.具体步骤如下: 1.查看原来的 ...
- 【转】Jmeter基础之——jmeter基础概念
JMeter 介绍:一个非常优秀的开源的性能测试工具. 优点:你用着用着就会发现它的重多优点,当然不足点也会呈现出来. 从性能工具的原理划分: Jmeter工具和其他性能工具在原理上完全一致,工具包含 ...
- Python 列表笔记
列表解析 squares = []for i in range(1,11): square = i**2 squares.append(square)print squares ########## ...
- mysql索引之八:myisam压缩(前缀压缩)索引
myisam使用前缀压缩来减少索引的大小,从而让更多的索引可以放入内存中,默认只压缩字符串,但通过参数配置也可以对整数做压缩,myisam压缩每个索引块的方法是,先完全保存索引块中的第一个值,然后将其 ...
- vs2017发布web项目
1.webform项目发布 1.右键选择项目. 2.选择自定义. 3.“配置文件名称”,发布后生成在项目中记录此次发布选择的配置信息文件名,下次发布默认为此次选择的配置信息,可以删除,随便填一个. 4 ...
- vue-cli中的ESlint配置文件eslintrc.js详解
本文讲解vue-cli脚手架根目录文件.eslintrc.js eslint简介 eslint是用来管理和检测js代码风格的工具,可以和编辑器搭配使用,如vscode的eslint插件 当有不符合配置 ...