Class FileSizeFormatter
java.lang.Object
com.alibaba.cloud.ai.dashscope.rag.util.FileSizeFormatter
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 TypeMethodDescriptionstatic Stringformat(long bytes) Formats file size to human-readable string with 2 decimal placesstatic Stringformat(long bytes, int decimalPlaces) Formats file size to human-readable string with specified decimal placesstatic StringformatCompact(long bytes) Formats file size in a compact format (no decimal places for small sizes)static longParses human-readable file size string to bytesstatic doubletoGigabytes(long bytes) Converts bytes to gigabytesstatic doubletoKilobytes(long bytes) Converts bytes to kilobytesstatic doubletoMegabytes(long bytes) Converts bytes to megabytesstatic doubletoTerabytes(long bytes) Converts bytes to terabytes
-
Method Details
-
format
Formats file size to human-readable string with 2 decimal placesAutomatically 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
Formats file size to human-readable string with specified decimal places- Parameters:
bytes- file size in bytesdecimalPlaces- number of decimal places (0-10)- Returns:
- formatted string
- Throws:
IllegalArgumentException- if decimalPlaces is out of range
-
parse
Parses human-readable file size string to bytesSupports 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
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
-