Changeset 9274 in josm
- Timestamp:
- 2016-01-03T12:13:44+01:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/StreamProgressUpdater.java
r9197 r9274 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.util.Locale; 5 4 6 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 5 7 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 8 import org.openstreetmap.josm.tools.Utils; 6 9 7 10 final class StreamProgressUpdater { … … 45 48 progressMonitor.setTicks(soFar); 46 49 } 47 progressMonitor.setExtraText( soFar / 1024 + " KB");50 progressMonitor.setExtraText(Utils.getSizeString(soFar, Locale.getDefault())); 48 51 } 49 52 } -
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9255 r9274 13 13 import java.net.URL; 14 14 import java.util.List; 15 import java.util.Locale; 15 16 import java.util.Map; 16 17 import java.util.Scanner; … … 99 100 100 101 if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) { 101 Main.info("{0} {1} ({2} kB) ...", requestMethod, url, requestBody.length / 1024);102 Main.info("{0} {1} ({2}) ...", requestMethod, url, Utils.getSizeString(requestBody.length, Locale.getDefault())); 102 103 headers.put("Content-Length", String.valueOf(requestBody.length)); 103 104 connection.setDoOutput(true); … … 116 117 requestMethod, url, hasReason ? " (" + reasonForRequest + ")" : "", 117 118 connection.getResponseCode(), 118 connection.getContentLengthLong() > 0 ? " (" + connection.getContentLengthLong() / 1024 + "KB)" : "" 119 connection.getContentLengthLong() > 0 120 ? " (" + Utils.getSizeString(connection.getContentLengthLong(), Locale.getDefault()) + ")" 121 : "" 119 122 ); 120 123 if (Main.isDebugEnabled()) { -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r9246 r9274 1146 1146 1147 1147 /** 1148 * Returns a human readable representation (B, kB, MB, ...) for the given number of byes. 1149 * @param bytes the number of bytes 1150 * @param locale the locale used for formatting 1151 * @return a human readable representation 1152 * @since 9274 1153 */ 1154 public static String getSizeString(long bytes, Locale locale) { 1155 if (bytes < 0) { 1156 throw new IllegalArgumentException("bytes must be >= 0"); 1157 } 1158 final String[] units = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; 1159 int unitIndex = 0; 1160 double value = bytes; 1161 while (value >= 1024 && unitIndex < units.length) { 1162 value /= 1024; 1163 unitIndex++; 1164 } 1165 if (value > 100 || unitIndex == 0) { 1166 return String.format(locale, "%.0f %s", value, units[unitIndex]); 1167 } else if (value > 10) { 1168 return String.format(locale, "%.1f %s", value, units[unitIndex]); 1169 } else { 1170 return String.format(locale, "%.2f %s", value, units[unitIndex]); 1171 } 1172 } 1173 1174 /** 1148 1175 * Returns a human readable representation of a list of positions. 1149 1176 * <p> -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r9174 r9274 8 8 import java.net.URL; 9 9 import java.util.Arrays; 10 import java.util.Locale; 10 11 11 12 import org.junit.Assert; … … 163 164 assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4)); 164 165 } 166 167 /** 168 * Test of {@link Utils#getSizeString} method. 169 */ 170 @Test 171 public void testSizeString() throws Exception { 172 assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH)); 173 assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH)); 174 assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH)); 175 assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH)); 176 assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH)); 177 assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH)); 178 } 179 180 /** 181 * Test of {@link Utils#getSizeString} method. 182 */ 183 @Test(expected = IllegalArgumentException.class) 184 public void testSizeStringNegative() throws Exception { 185 Utils.getSizeString(-1, Locale.ENGLISH); 186 } 187 165 188 }
Note:
See TracChangeset
for help on using the changeset viewer.