FormatUtil

Utility class with static helper methods for formatting sizes, dates, time intervals, and strings; sanitizing and escaping HTML; and validating common data types. All methods are static.

Size formatting

formatSize

Description:

MethodReturn valuesDescription

formatSize(long bytes)

String

Formats a byte count as a human-readable size string (e.g. 1.5 MB). Units: B, KB, MB, GB, TB, PB, EB.

bytes: The size in bytes.

Example:

package com.openkm;

import java.io.File;
import com.openkm.util.FormatUtil;

public class Test {

    public static void main(String[] args) {
        try {
            File file = new File("/home/openkm/test.png");
            System.out.println(FormatUtil.formatSize(file.length())); // e.g. "2.3 MB"
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

parseSize

Description:

MethodReturn valuesDescription

parseSize(String text)

long

Parses a human-readable size string (e.g. "10MB", "2GB") into bytes. Supports KB, MB, GB suffixes.

text: The size string to parse.

Example:

long bytes = FormatUtil.parseSize("10MB");
System.out.println(bytes); // 10485760

Date formatting

formatDate (Calendar)

Description:

MethodReturn valuesDescription

formatDate(Calendar cal)

String

Formats a Calendar value as dd-MM-yyyy HH:mm:ss in the calendar's own time zone. Returns null if cal is null.

Example:

import java.util.Calendar;
import com.openkm.util.FormatUtil;

System.out.println(FormatUtil.formatDate(Calendar.getInstance())); // "15-03-2024 14:30:00"

formatDate (Date)

Description:

MethodReturn valuesDescription

formatDate(Date date)

String

Formats a Date value as dd-MM-yyyy HH:mm:ss.

formatDate (Unix epoch)

Description:

MethodReturn valuesDescription

formatDate(long unixEpoch)

String

Formats a Unix epoch value (milliseconds since 1970-01-01) as dd-MM-yyyy HH:mm:ss.

Example:

System.out.println(FormatUtil.formatDate(System.currentTimeMillis())); // "15-03-2024 14:30:00"

Time interval formatting

formatSeconds

Description:

MethodReturn valuesDescription

formatSeconds(long time)

String

Formats a duration in milliseconds as HH:MM:SS.

formatSecondsSince

Description:

MethodReturn valuesDescription

formatSecondsSince(long time)

String

Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS. Pass the result of System.currentTimeMillis() at the start of the operation.

formatMilliSeconds

Description:

MethodReturn valuesDescription

formatMilliSeconds(long time)

String

Formats a duration in milliseconds as HH:MM:SS.mmm.

formatMilliSecondsSince

Description:

MethodReturn valuesDescription

formatMilliSecondsSince(long time)

String

Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS.mmm. Pass the result of System.currentTimeMillis() at the start of the operation.

Example:

long start = System.currentTimeMillis();
// ... long operation ...
System.out.println("Elapsed: " + FormatUtil.formatMilliSecondsSince(start)); // "00:00:01.234"

String formatting

formatArray

Description:

MethodReturn valuesDescription

formatArray(String[] values)

String

Converts a string array to a readable string. Returns the single element directly for one-element arrays, and the Apache Commons ArrayUtils.toString representation for multi-element arrays. Returns null for null input.

formatObject

Description:

MethodReturn valuesDescription

formatObject(Object value)

String

Converts an object to a string. For Object[] uses ArrayUtils.toString; otherwise calls toString(). Returns null for null input.

HTML and input sanitization

escapeHtml

Description:

MethodReturn valuesDescription

escapeHtml(String str)

String

Escapes < and > as HTML entities, and converts newlines to <br/>. Returns null for null input.

sanitizeInput

Description:

MethodReturn valuesDescription

sanitizeInput(String string)

String

Sanitizes an HTML string to remove potentially dangerous content (XSS). Uses either the AntiSamy library or a legacy regex approach depending on the Config.SECURITY_INPUT_SANITIZER setting. Returns null for null input.

cleanXSS

Description:

MethodReturn valuesDescription

cleanXSS(String value)

String

Cleans a string by escaping HTML angle brackets and parentheses, removing eval() expressions and inline JavaScript, and stripping script keywords.

sanitizeEmailString

Description:

MethodReturn valuesDescription

sanitizeEmailString(String value)

String

Removes any characters that are not letters, digits, punctuation, or spaces from the given string. Useful for sanitizing email address strings.

Example:

String safe = FormatUtil.escapeHtml("<b>Hello</b>\nWorld");
// "&lt;b&gt;Hello&lt;/b&gt;<br/>World"

String sanitized = FormatUtil.sanitizeInput("<script>alert('xss')</script>Hello");
System.out.println(sanitized); // "Hello"

String and encoding validation

isValidUUID

Description:

MethodReturn valuesDescription

isValidUUID(String str)

boolean

Returns true if the given string is a valid UUID.

Example:

boolean valid = FormatUtil.isValidUUID("d50133e3-dbfa-4d01-a109-28785cd48f40"); // true
boolean invalid = FormatUtil.isValidUUID("not-a-uuid"); // false

validUTF8

Description:

MethodReturn valuesDescription

validUTF8(byte[] input)

boolean

Returns true if the given byte array is a valid UTF-8 sequence.

fixUTF8 (byte array)

Description:

MethodReturn valuesDescription

fixUTF8(byte[] input)

byte[]

Replaces null bytes (0x00) with spaces (0x20) in a byte array.

fixUTF8 (String)

Description:

MethodReturn valuesDescription

fixUTF8(String input)

String

Replaces null Unicode characters (\u0000) with spaces and trims the result. Returns null for null input.

trimUnicodeSurrogates

Description:

MethodReturn valuesDescription

trimUnicodeSurrogates(String text)

String

Removes Unicode surrogate characters from the given string and trims it. Returns null for null input. Useful for preparing text before indexing or XML serialization.

stripNonValidXMLCharacters

Description:

MethodReturn valuesDescription

stripNonValidXMLCharacters(String in)

String

Removes all characters that are not valid in XML 1.0. Returns an empty string for null or empty input. Use this before inserting arbitrary text into an XML document.

Example:

String cleaned = FormatUtil.stripNonValidXMLCharacters("Hello\u0008World");
System.out.println(cleaned); // "HelloWorld"