published: 2018-01-04
started: 2018-01-04
last change: 2018-01-04
started: 2018-01-04
last change: 2018-01-04
Java Data Size Formatter (kB, MB, GB, ...)
Format a number of bytes to kB, MB, and so on in order to make it human readable. It's public domain. You can copy-paste it directly into your project.
It supports all three formats:
- the classic one with 1024 as conversion factor, but seemingly metric unit symbols kB/MB/...
- the "correct" metric format with conversion factor 1000 and the same unit symbols as for the classic format
- newest, or neo format with conversion factor 1024 and distinguished units KiB/MiB/...
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | /* * DataSizeFormatter 2018-01-01 * * This is free and unencumbered software released into the public domain. * * Anyone is free to copy, modify, publish, use, compile, sell, or distribute * this software, either in source code form or as a compiled binary, for any * purpose, commercial or non-commercial, and by any means. * * In jurisdictions that recognize copyright laws, the author or authors of this * software dedicate any and all copyright interest in the software to the * public domain. We make this dedication for the benefit of the public at large * and to the detriment of our heirs and successors. We intend this dedication * to be an overt act of relinquishment in perpetuity of all present and future * rights to this software under copyright law. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * For more information, please refer to <http://unlicense.org/> */ /** * A formatter for data sizes, e.g. formats the number 213425535 to "204 MiB". * If you are not sure which of the three formats to use, then use the newest * and clearly defined formatNeo(...). */ public class DataSizeFormatter { private static final long K = 1000; private static final long M = K * K; private static final long G = M * K; private static final long T = G * K; private static final long P = T * K; private static final long E = P * K; private static final long Z = E * K; private static final long Y = Z * K; private static final long[] METRIC_INTERVAL_LIST = { 1, K, M, G, T, P, E, Z, Y }; private static final String[] METRIC_UNIT_LIST = { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; private static final long Ki = 1024; private static final long Mi = Ki * Ki; private static final long Gi = Mi * Ki; private static final long Ti = Gi * Ki; private static final long Pi = Ti * Ki; private static final long Ei = Pi * Ki; private static final long Zi = Ei * Ki; private static final long Yi = Zi * Ki; private static final long[] NEO_INTERVAL_LIST = { 1, Ki, Mi, Gi, Ti, Pi, Ei, Zi, Yi }; private static final String[] NEO_UNIT_LIST = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }; /** * Formatting with neo unit symbols KiB, MiB, and so on, which are clearly * defined to use 1024 as conversion factor for different units. */ public static String formatNeo(long size) { return format(size, NEO_INTERVAL_LIST, NEO_UNIT_LIST); } /** * Correct metric formatting with 1000 as conversion factor and metric units kB, * MB, and so on, which is however too easy to confuse with classic formatting, * which uses the same unit symbols. Many storage device producers use this * metric format to make their devices look bigger. */ public static String formatMetric(long size) { return format(size, METRIC_INTERVAL_LIST, METRIC_UNIT_LIST); } /** * Classic, but outdated fashion to format with seemingly metric units kB, MB, * and so on, but with an conversion rate of 1024 instead of the metric 1000. */ public static String formatClassic(long size) { return format(size, NEO_INTERVAL_LIST, METRIC_UNIT_LIST); } private static String format(long size, long[] intervalList, String[] unitList) { String signStr; if (size < 0) { size = -size; signStr = "-"; } else { signStr = ""; } int interval = getInterval(size, intervalList); double converted = ((double) size) / intervalList[interval]; String unitStr = unitList[interval]; String convertedStr = toRoundedString(converted); return signStr + convertedStr + " " + unitStr; } private static int getInterval(long size, long[] intervals) { if (size < intervals[1]) { return 0; } else if (size < intervals[2]) { return 1; } else if (size < intervals[3]) { return 2; } else if (size < intervals[4]) { return 3; } else if (size < intervals[5]) { return 4; } else if (size < intervals[6]) { return 5; } else if (size < intervals[7]) { return 6; } else if (size < intervals[8]) { return 7; } else { return 8; } } private static String toRoundedString(double converted) { String convertedStr; if (converted < 10) { convertedStr = Double.toString(Math.round(converted * 10.0D) / 10.0D); } else { convertedStr = Long.toString(Math.round(converted)); } return convertedStr; } } |