Changeset 9274 in josm for trunk


Ignore:
Timestamp:
2016-01-03T12:13:44+01:00 (9 years ago)
Author:
simon04
Message:

Uniform display of sizes (B, kB, MB, ...)

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/StreamProgressUpdater.java

    r9197 r9274  
    22package org.openstreetmap.josm.io;
    33
     4import java.util.Locale;
     5
    46import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    57import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     8import org.openstreetmap.josm.tools.Utils;
    69
    710final class StreamProgressUpdater {
     
    4548                progressMonitor.setTicks(soFar);
    4649            }
    47             progressMonitor.setExtraText(soFar / 1024 + " KB");
     50            progressMonitor.setExtraText(Utils.getSizeString(soFar, Locale.getDefault()));
    4851        }
    4952    }
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9255 r9274  
    1313import java.net.URL;
    1414import java.util.List;
     15import java.util.Locale;
    1516import java.util.Map;
    1617import java.util.Scanner;
     
    99100
    100101        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()));
    102103            headers.put("Content-Length", String.valueOf(requestBody.length));
    103104            connection.setDoOutput(true);
     
    116117                        requestMethod, url, hasReason ? " (" + reasonForRequest + ")" : "",
    117118                        connection.getResponseCode(),
    118                         connection.getContentLengthLong() > 0 ? " (" + connection.getContentLengthLong() / 1024 + "KB)" : ""
     119                        connection.getContentLengthLong() > 0
     120                                ? " (" + Utils.getSizeString(connection.getContentLengthLong(), Locale.getDefault()) + ")"
     121                                : ""
    119122                );
    120123                if (Main.isDebugEnabled()) {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9246 r9274  
    11461146
    11471147    /**
     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    /**
    11481175     * Returns a human readable representation of a list of positions.
    11491176     * <p>
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r9174 r9274  
    88import java.net.URL;
    99import java.util.Arrays;
     10import java.util.Locale;
    1011
    1112import org.junit.Assert;
     
    163164        assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
    164165    }
     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
    165188}
Note: See TracChangeset for help on using the changeset viewer.