101 this.text = unit; |
101 this.text = unit; |
102 this.amount = amount; |
102 this.amount = amount; |
103 } |
103 } |
104 } |
104 } |
105 |
105 |
106 public static String formatBytes(long bytes, String separation) { |
106 // Tjis method can't handle Long.MIN_VALUE because absolute value is negative |
107 if (bytes == 1) { |
107 private static String formatDataAmount(String formatter, long amount) { |
108 return "1 byte"; |
108 int exp = (int) (Math.log(Math.abs(amount)) / Math.log(1024)); |
109 } |
109 char unitPrefix = "kMGTPE".charAt(exp - 1); |
|
110 return String.format(formatter, amount / Math.pow(1024, exp), unitPrefix); |
|
111 } |
|
112 |
|
113 public static String formatBytesCompact(long bytes) { |
110 if (bytes < 1024) { |
114 if (bytes < 1024) { |
|
115 return String.valueOf(bytes); |
|
116 } |
|
117 return formatDataAmount("%.1f%cB", bytes); |
|
118 } |
|
119 |
|
120 public static String formatBits(long bits) { |
|
121 if (bits == 1 || bits == -1) { |
|
122 return bits + " bit"; |
|
123 } |
|
124 if (bits < 1024 && bits > -1024) { |
|
125 return bits + " bits"; |
|
126 } |
|
127 return formatDataAmount("%.1f %cbit", bits); |
|
128 } |
|
129 |
|
130 public static String formatBytes(long bytes) { |
|
131 if (bytes == 1 || bytes == -1) { |
|
132 return bytes + " byte"; |
|
133 } |
|
134 if (bytes < 1024 && bytes > -1024) { |
111 return bytes + " bytes"; |
135 return bytes + " bytes"; |
112 } |
136 } |
113 int exp = (int) (Math.log(bytes) / Math.log(1024)); |
137 return formatDataAmount("%.1f %cB", bytes); |
114 char bytePrefix = "kMGTPE".charAt(exp - 1); |
138 } |
115 return String.format("%.1f%s%cB", bytes / Math.pow(1024, exp), separation, bytePrefix); |
139 |
116 } |
140 public static String formatBytesPerSecond(long bytes) { |
117 |
141 if (bytes < 1024 && bytes > -1024) { |
|
142 return bytes + " byte/s"; |
|
143 } |
|
144 return formatDataAmount("%.1f %cB/s", bytes); |
|
145 } |
|
146 |
|
147 public static String formatBitsPerSecond(long bits) { |
|
148 if (bits < 1024 && bits > -1024) { |
|
149 return bits + " bps"; |
|
150 } |
|
151 return formatDataAmount("%.1f %cbps", bits); |
|
152 } |
118 public static String formatTimespan(Duration dValue, String separation) { |
153 public static String formatTimespan(Duration dValue, String separation) { |
119 if (dValue == null) { |
154 if (dValue == null) { |
120 return "0"; |
155 return "0"; |
121 } |
156 } |
122 long value = dValue.toNanos(); |
157 long value = dValue.toNanos(); |