Commons IO
  1. Commons IO
  2. IO-294

Adding FileUtils.byteCountToDisplaySize(long size, boolean useSiUnits)

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.1
    • Fix Version/s: None
    • Component/s: Utilities
    • Labels:
      None

      Description

      I have written a little Utility method that might benefit Commons IO:

      public class FileUtils {
      
          /**
           * Returns a human-readable version of the file size (original is in bytes). The implementation has the following features:
           * <ul>
           * <li>Supports the SI or IEC units.</li>
           * <li>Supports I18n</li>
           * <li>Display a one digit remainder (rounded down if less than 5, rounded up otherwise)</li>
           * <li>Once the main unit is >= 100, drops the remainder which would be over precision.</li>
           * </ul>
           * 
           * @param size The number of bytes.
           * @param useSiUnits if false, uses the IEC (International Electrotechnical Commission) units (powers of 2), else uses SI (International System of Units)
           *            units (powers of 10).
           * @return A human-readable display value (includes units).
           */
          public static String byteCountToDisplaySize(long size, boolean useSiUnits) {
      
      1. FileUtils.java
        2 kB
        Jean-Noel Rouvignac
      2. FileUtilsTest.java
        4 kB
        Jean-Noel Rouvignac

        Activity

        Hide
        Aaron Digulla added a comment -

        It would probably belong to a new class in the package org.apache.commons.lang3.text but I fear that nobody will ever find it there.

        byteCountToDisplaySize() is a method that is strongly related to I/O - I can't think that we'd use that to print the size of String labels in a UI, for example.

        So it should go into commons IO somewhere but maybe into IOUtils since it's not especially related to files.

        And if you support I18n, you must allow to specify a locale. Otherwise, this code can't be used in a multi-language web app, for example.

        Show
        Aaron Digulla added a comment - It would probably belong to a new class in the package org.apache.commons.lang3.text but I fear that nobody will ever find it there. byteCountToDisplaySize() is a method that is strongly related to I/O - I can't think that we'd use that to print the size of String labels in a UI, for example. So it should go into commons IO somewhere but maybe into IOUtils since it's not especially related to files. And if you support I18n, you must allow to specify a locale. Otherwise, this code can't be used in a multi-language web app, for example.
        Hide
        Jean-Noel Rouvignac added a comment -

        @Sebb: FileUtils already has this other byteCountToDisplaySize() method, so I thought it was the right place to add it.

        Do you have a class you can suggest for adding this in Commons Lang?

        Cheers.

        Show
        Jean-Noel Rouvignac added a comment - @Sebb: FileUtils already has this other byteCountToDisplaySize() method, so I thought it was the right place to add it. Do you have a class you can suggest for adding this in Commons Lang? Cheers.
        Hide
        Jean-Noel Rouvignac added a comment -

        Also, FileUtils.byteCountToDisplaySize(long size) could also be changed to use this new method if you are not too worried about the compatibility of its output compared to previous versions.

        Show
        Jean-Noel Rouvignac added a comment - Also, FileUtils.byteCountToDisplaySize(long size) could also be changed to use this new method if you are not too worried about the compatibility of its output compared to previous versions.
        Hide
        Sebb added a comment -

        Looks useful; however I'm not sure FileUtils is the correct place for it, as bytes are also used for network sizes, etc.

        The main part of the code purely deals with numbers, and is not specifically related to IO - perhaps it belongs in Lang?

        Show
        Sebb added a comment - Looks useful; however I'm not sure FileUtils is the correct place for it, as bytes are also used for network sizes, etc. The main part of the code purely deals with numbers, and is not specifically related to IO - perhaps it belongs in Lang?

          People

          • Assignee:
            Unassigned
            Reporter:
            Jean-Noel Rouvignac
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development