1 18 package net.sf.drftpd; 19 20 import java.text.DecimalFormat ; 21 import java.text.DecimalFormatSymbols ; 22 23 29 public class Bytes { 30 private static class Multiple { 31 private long _binaryMultiple; 32 private long _multiple; 33 34 private char _suffix; 35 public Multiple(char suffix, long multiple, long binarymultiple) { 36 _suffix = suffix; 37 _multiple = multiple; 38 _binaryMultiple = binarymultiple; 39 } 40 41 public long getBinaryMultiple() { 42 return _binaryMultiple; 43 } 44 45 public long getMultiple() { 46 return _multiple; 47 } 48 49 public char getSuffix() { 50 return _suffix; 51 } 52 53 } 54 private static final DecimalFormat FORMAT; 55 static { 56 DecimalFormatSymbols formatsymbols = new DecimalFormatSymbols (); 57 FORMAT = new DecimalFormat ("0.0", formatsymbols); 59 FORMAT.setDecimalSeparatorAlwaysShown(true); 60 } 61 62 public static final long GIBI = 1073741824L; 63 public static final long GIGA = 1000000000L; 64 public static final long KIBI = 1024L; 65 public static final long KILO = 1000L; 66 public static final long MEBI = 1048576L; 67 public static final long MEGA = 1000000L; 68 69 private static final Multiple[] MULTIPLES = 70 new Multiple[] { 71 new Multiple('E', 1000000000000000000L, 1152921504606846976L), 72 new Multiple('P', 1000000000000000L, 1125899906842624L), 73 new Multiple('T', 1000000000000L, 1099511627776L), 74 new Multiple('G', 1000000000L, 1073741824L), 75 new Multiple('M', 1000000L, 1048576L), 76 new Multiple('K', 1000L, 1024L)}; 77 78 public static final long PETA = 1000000000000000L; 79 public static final long TEBI = 1099511627776L; 80 public static final long TERRA = 1000000000000L; 81 82 public static String formatBytes(long bytes) { 83 return formatBytes(bytes, Boolean.getBoolean(System.getProperty("bytes.binary", "false"))); 84 } 85 86 public static String formatBytes(long bytes, boolean binary) { 87 long absbytes = Math.abs(bytes); 88 for (int i = 0; i < MULTIPLES.length; i++) { 89 Multiple multiple = MULTIPLES[i]; 90 long multipleVal = 91 binary ? multiple.getBinaryMultiple() : multiple.getMultiple(); 92 if (absbytes >= multipleVal) { 93 return Bytes.FORMAT.format((float) bytes / multipleVal) 94 + multiple.getSuffix() 95 + (binary ? "i" : "") 96 + "B"; 97 } 98 } 99 return bytes+"B"; 100 } 101 113 public static long parseBytes(String str) throws NumberFormatException { 114 str = str.toUpperCase(); 115 if (str.endsWith("B")) 116 str = str.substring(0, str.length() - 1); 117 118 boolean binary = false; 119 if (str.endsWith("I")) { 120 str = str.substring(0, str.length() - 1); 121 binary = true; 122 } 123 124 char suffix = Character.toUpperCase(str.charAt(str.length() - 1)); 125 if (Character.isDigit(suffix)) { 126 return Long.parseLong(str); 127 } 128 str = str.substring(0, str.length() - 1); 129 130 for (int i = 0; i < MULTIPLES.length; i++) { 131 Multiple multiple = MULTIPLES[i]; 132 if (suffix == multiple.getSuffix()) { 134 return Long.parseLong(str) 135 * (binary 136 ? multiple.getBinaryMultiple() 137 : multiple.getMultiple()); 138 } 139 } 140 throw new IllegalArgumentException ("Unknown suffix " + suffix); 141 } 142 } 143 | Popular Tags |