Java File Size Readable Format formatFileStats(final String label, final long fileCount, final Object rawSize)

Here you can find the source of formatFileStats(final String label, final long fileCount, final Object rawSize)

Description

Formats an object's file statistics for display.

License

Open Source License

Parameter

Parameter Description
label The label of the object (session, scan, resource, etc.)
fileCount The number of files that compose the object.
rawSize The size of the files that compose the object.

Return

A formatted display of the file statistics.

Declaration

public static String formatFileStats(final String label, final long fileCount, final Object rawSize) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w w . j av a2 s .  c  o  m*/
     * Formats an object's file statistics for display.
     *
     * @param label     The label of the object (session, scan, resource, etc.)
     * @param fileCount The number of files that compose the object.
     * @param rawSize   The size of the files that compose the object.
     * @return A formatted display of the file statistics.
     */
    public static String formatFileStats(final String label, final long fileCount, final Object rawSize) {
        long size = 0;
        if (rawSize != null) {
            if (rawSize instanceof Integer) {
                size = (Integer) rawSize;
            } else if (rawSize instanceof Long) {
                size = (Long) rawSize;
            }
        }
        if (label == null || label.equals("") || label.equalsIgnoreCase("total")) {
            return String.format("%s in %s files", formatSize(size), fileCount);
        }
        return String.format("%s: %s in %s files", label, formatSize(size), fileCount);
    }

    /**
     * Takes a size of a file or heap of memory in the form of a long and returns a formatted readable version in the
     * form of byte units. For example, 46 would become 46B, 1,024 would become 1KB, 1,048,576 would become 1MB, etc.
     *
     * @param size The size in bytes to be formatted.
     * @return A formatted string representing the byte size.
     */
    public static String formatSize(long size) {
        if (size < 1024) {
            return size + " B";
        }
        int exp = (int) (Math.log(size) / Math.log(1024));
        return String.format("%.1f %sB", size / Math.pow(1024, exp), "KMGTPE".charAt(exp - 1));
    }
}

Related

  1. formatFileSize(long size)
  2. formatFileSize(long size)
  3. formatFileSize(long size, String format)
  4. FormatFileSize(String filesize)
  5. formatFilesize(String filesize, int decimalPlaces)
  6. formatGroup(String str, int groupSize, int lineBreak)
  7. formatIndex(int index, int totalSize)
  8. formatInt(int val, int size)
  9. formatRow(String[] values, int[] sizes)