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. 随时查找中位数——pat1057

    http://pat.zju.edu.cn/contests/pat-a-practise/1057 题目的意思是可以在一个可以任意添加于删除整数的集合里随时查找该集合的中位数 每次查找用nlogn的 ...

  2. C# byte数组转成Bitmap对象

    方法一: /// <summary> /// 将数组转换成彩色图片 /// </summary> /// <param name="rawValues" ...

  3. [PHP] - Laravel - CSRF token禁用方法与排除验证csrf_token的url设置

    laravel5.1排除验证csrf_token的url设置 <?php namespace App\Http\Middleware; use Illuminate\Foundation\Htt ...

  4. MariaDB存在的问题

    MySQL与MariaDB对嵌套的查询语句当中的order by的处理方法不同.MySQL会忠实执行内层查询的排序子句,但是MariaDB会将这个order by去掉,理论依据就是关系理论 --- 一 ...

  5. linux命令echo和cat比较

    当前主要比较echo 和 cat的重定向功能 1.echo 1 > /proc/xxx 解析: echo 进行重定向的时候,仅仅是将字符"1" 输出到 /proc/xxx文件 ...

  6. 笔记:Tid设计思维

    Tid设计思维 2017年7月19日TiD大会创新设计论坛 一.     设计思维实践: 为什么他能觉察你看不到的机会 1.    原因一:[觉察]与[看] 他(们)能“觉察”你“看”不到的机会,他们 ...

  7. Angular2快速入门-3.多个组件(分离新闻列表页和详细页)

    上篇(Angular2快速入门-2.创建一个新闻列表)已经完成新闻列表的展示,并且点击新闻列表的时候,下面可以展示出新闻的详细信息,这节我们把新闻详细和新闻列表页面分离出来 新闻详细单独一个compo ...

  8. centos 系统使用verdaccio搭建npm私库

    .安装nodejs yum install -y nodejs 2.安装verdaccio npm install -g verdaccio --unsafe-perm 3.配置 a.修改配置文件 c ...

  9. babel-preset-latest使用介绍

    本文介绍babel中的babel-preset-latest插件 简介 该插件的作用是包含了每年的js代码转换规则 默认包含es2015,es2016,es2017,默认对这些代码都会进行转码,从而方 ...

  10. windows下配置非安装版的MySQL5.6

    Installing MySQL on Microsoft Windows Using a noinstall Zip Archive,在Windows上使用非安装压缩包安装MySQL.安装步骤如下: ...