1 packagecom.herman.utils;2
3 /***4 *@see存储大小(单位)转换器.5 *@authorHerman.Xiong6 * @date 2014年5月27日 13:27:407 *@versionV1.08 */
9 public enumSizeConverter {10 /**转换任意单位的大小, 返回结果会包含两位小数但不包含单位.*/
11 Arbitrary {12 @Override13 public String convert(floatsize) {14 while (size > 1024) {15 size /= 1024;16 }17 returnString.format(FORMAT_F, size);18 }19 },20
21 //-----------------------------------------------------------------------22 //有单位
23 /**转换单位为B的大小, 返回结果会包含两位小数以及单位. 如: 1024B->1KB, (1024*1024)B->1MB*/
24 B {25 @Override26 public String convert(floatB) {27 return converter(0, B);28 }29 },30 /**转换单位为B的大小, 返回结果会包含两位小数以及单位.*/
31 KB {32 @Override33 public String convert(floatKB) {34 return converter(1, KB);35 }36 },37 /**转换单位为MB的大小, 返回结果会包含两位小数以及单位.*/
38 MB {39 @Override40 public String convert(floatMB) {41 return converter(2, MB);42 }43 },44 /**转换单位为GB的大小, 返回结果会包含两位小数以及单位.*/
45 GB {46 @Override47 public String convert(floatGB) {48 return converter(3, GB);49 }50 },51 /**转换单位为TB的大小, 返回结果会包含两位小数以及单位.*/
52 TB {53 @Override54 public String convert(floatTB) {55 return converter(4, TB);56 }57 },58
59 //-----------------------------------------------------------------------60 //trim没单位
61 /**转换任意单位的大小, 返回结果小数部分为0时将去除两位小数, 不包含单位.*/
62 ArbitraryTrim {63 @Override64 public String convert(floatsize) {65 while (size > 1024) {66 size /= 1024;67 }68
69 int sizeInt = (int) size;70 boolean isfloat = size - sizeInt > 0.0F;71 if(isfloat) {72 returnString.format(FORMAT_F, size);73 }74 returnString.format(FORMAT_D, sizeInt);75 }76 },77
78 //-----------------------------------------------------------------------79 //trim有单位
80 /**转换单位为B的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位.*/
81 BTrim {82 @Override83 public String convert(floatB) {84 return trimConverter(0, B);85 }86 },87 /**转换单位为KB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位.*/
88 KBTrim {89 @Override90 public String convert(floatKB) {91 return trimConverter(1, KB);92 }93 },94 /**转换单位为MB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位.*/
95 MBTrim {96 @Override97 public String convert(floatMB) {98 return trimConverter(2, MB);99 }100 },101 /**转换单位为GB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位.*/
102 GBTrim {103 @Override104 public String convert(floatGB) {105 return trimConverter(3, GB);106 }107 },108 /**转换单位为TB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位.*/
109 TBTrim {110 @Override111 public String convert(floatTB) {112 return trimConverter(4, TB);113 }114 };115 /***116 *
将指定的大小转换到1024范围内的大小. 注意该方法的最大单位为PB, 最小单位为B,117 * 任何超出该范围的单位最终会显示为**.
118 *119 *@paramsize 要转换的大小, 注意是浮点数, 不要以整形的方式传入, 容易造成溢出.120 * (如: 1024*1024*1024*1024*1024会溢出, 使结果为0, 因为它先将结果以int相乘后再转换为float;121 * 而1024.0F*1024.0F*1024.0F*1024.0F*1024.0F就不会溢出)122 *@return123 */
124 abstract public String convert(floatsize);125
126 //-----------------------------------------------------------------------127 //单位转换
128
129 private static final String[] UNITS = newString[] {130 "B", "KB", "MB", "GB", "TB", "PB", "**"
131 };132
133 private static final int LAST_IDX = UNITS.length-1;134
135 private static final String FORMAT_F = "%1$-1.2f";136 private static final String FORMAT_F_UNIT = "%1$-1.2f%2$s";137
138 private static final String FORMAT_D = "%1$-1d";139 private static final String FORMAT_D_UNIT = "%1$-1d%2$s";140
141 //-----------------------------------------------------------------------
142 private static String converter(int unit, floatsize) {143 int unitIdx =unit;144 while (size > 1024) {145 unitIdx++;146 size /= 1024;147 }148 int idx = unitIdx < LAST_IDX ?unitIdx : LAST_IDX;149 returnString.format(FORMAT_F_UNIT, size, UNITS[idx]);150 }151
152 private static String trimConverter(int unit, floatsize) {153 int unitIdx =unit;154 while (size > 1024) {155 unitIdx++;156 size /= 1024;157 }158
159 int sizeInt = (int) size;160 boolean isfloat = size - sizeInt > 0.0F;161 int idx = unitIdx < LAST_IDX ?unitIdx : LAST_IDX;162 if(isfloat) {163 returnString.format(FORMAT_F_UNIT, size, UNITS[idx]);164 }165 returnString.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);166 }167
168 //-----------------------------------------------------------------------
169 public static String convertBytes(float B, booleantrim) {170 return trim ? trimConvert(0, B, true) : convert(0, B, true);171 }172
173 public static String convertKB(float KB, booleantrim) {174 return trim ? trimConvert(1, KB, true) : convert(1, KB, true);175 }176
177 public static String convertMB(float MB, booleantrim) {178 return trim ? trimConvert(2, MB, true) : convert(2, MB, true);179 }180
181 /***182 *
存储大小单位间的转换. 注意该方法的最大单位为PB, 最小单位为B,183 * 任何超出该范围的单位最终会显示为**.
184 *185 *@paramunit 从哪个单位开始186 *@paramsize 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,187 * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,188 * 所以这么写1024.0F*1024.0F)189 *@paramwithUnit 返回的结果字符串是否带有对应的单位190 *@return191 */
192 private static String convert(int unit, float size, booleanwithUnit) {193 int unitIdx =unit;194 while (size > 1024) {195 unitIdx++;196 size /= 1024;197 }198 if(withUnit) {199 int idx = unitIdx < LAST_IDX ?unitIdx : LAST_IDX;200 returnString.format(FORMAT_F_UNIT, size, UNITS[idx]);201 }202 returnString.format(FORMAT_F, size);203 }204
205 /***206 *
存储大小单位间的转换, 如果转换后小数部分为0, 则去除小数部分.207 * 注意该方法的最大单位为PB, 最小单位为B, 任何超出该范围的单位最终会显示为**.
208 *209 *@paramunit 从哪个单位开始210 *@paramsize 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,211 * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,212 * 所以这么写1024.0F*1024.0F)213 *@paramwithUnit 返回的结果字符串是否带有对应的单位214 *@return215 */
216 private static String trimConvert(int unit, float size, booleanwithUnit) {217 int unitIdx =unit;218 while (size > 1024) {219 unitIdx++;220 size /= 1024;221 }222
223 int sizeInt = (int) size;224 boolean isfloat = size - sizeInt > 0.0F;225 if(withUnit) {226 int idx = unitIdx < LAST_IDX ?unitIdx : LAST_IDX;227 if(isfloat) {228 returnString.format(FORMAT_F_UNIT, size, UNITS[idx]);229 }230 returnString.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);231 }232
233 if(isfloat) {234 returnString.format(FORMAT_F, size);235 }236 returnString.format(FORMAT_D, sizeInt);237 }238 }