Class FileSizeFormatter

java.lang.Object
com.alibaba.cloud.ai.dashscope.rag.util.FileSizeFormatter

public final class FileSizeFormatter extends Object
Utility class for formatting file sizes to human-readable strings

Converts byte sizes to appropriate units (B, KB, MB, GB, TB) with proper formatting.

Examples:

  • 512 bytes → "512 B"
  • 1536 bytes → "1.50 KB"
  • 1048576 bytes → "1.00 MB"
  • 1073741824 bytes → "1.00 GB"
Since:
2025/11/27
Author:
kevin
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    format(long bytes)
    Formats file size to human-readable string with 2 decimal places
    static String
    format(long bytes, int decimalPlaces)
    Formats file size to human-readable string with specified decimal places
    static String
    formatCompact(long bytes)
    Formats file size in a compact format (no decimal places for small sizes)
    static long
    parse(String sizeStr)
    Parses human-readable file size string to bytes
    static double
    toGigabytes(long bytes)
    Converts bytes to gigabytes
    static double
    toKilobytes(long bytes)
    Converts bytes to kilobytes
    static double
    toMegabytes(long bytes)
    Converts bytes to megabytes
    static double
    toTerabytes(long bytes)
    Converts bytes to terabytes

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • format

      public static String format(long bytes)
      Formats file size to human-readable string with 2 decimal places

      Automatically selects the most appropriate unit (B, KB, MB, GB, TB) based on the size.

      Parameters:
      bytes - file size in bytes
      Returns:
      formatted string (e.g., "10.50 MB", "1.23 GB")
    • format

      public static String format(long bytes, int decimalPlaces)
      Formats file size to human-readable string with specified decimal places
      Parameters:
      bytes - file size in bytes
      decimalPlaces - number of decimal places (0-10)
      Returns:
      formatted string
      Throws:
      IllegalArgumentException - if decimalPlaces is out of range
    • parse

      public static long parse(String sizeStr)
      Parses human-readable file size string to bytes

      Supports formats like:

      • "100" or "100B" → 100 bytes
      • "1.5KB" or "1.5 KB" → 1536 bytes
      • "10MB" or "10 MB" → 10485760 bytes
      • "1GB" or "1 GB" → 1073741824 bytes
      Parameters:
      sizeStr - size string to parse
      Returns:
      size in bytes
      Throws:
      IllegalArgumentException - if format is invalid
    • formatCompact

      public static String formatCompact(long bytes)
      Formats file size in a compact format (no decimal places for small sizes)
      Parameters:
      bytes - file size in bytes
      Returns:
      compact formatted string
    • toKilobytes

      public static double toKilobytes(long bytes)
      Converts bytes to kilobytes
      Parameters:
      bytes - size in bytes
      Returns:
      size in kilobytes
    • toMegabytes

      public static double toMegabytes(long bytes)
      Converts bytes to megabytes
      Parameters:
      bytes - size in bytes
      Returns:
      size in megabytes
    • toGigabytes

      public static double toGigabytes(long bytes)
      Converts bytes to gigabytes
      Parameters:
      bytes - size in bytes
      Returns:
      size in gigabytes
    • toTerabytes

      public static double toTerabytes(long bytes)
      Converts bytes to terabytes
      Parameters:
      bytes - size in bytes
      Returns:
      size in terabytes