All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.shared.utilities.StringUtils Maven / Gradle / Ivy

There is a newer version: 6.0.5
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.shared.utilities;

import java.util.Locale;

public final class StringUtils {

    private StringUtils() {
    }

    public static String f(String format, Object... args) {
        return String.format(Locale.ENGLISH, format, args);
    }

    public static String humanReadableByteCount(final long bytes)
    {
        final String[] units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
        final int base = 1024;

        // When using the smallest unit no decimal point is needed, because it's the exact number.
        if (bytes < base) {
            return bytes + " " + units[0];
        }

        final int exponent = (int) (Math.log(bytes) / Math.log(base));
        final String unit = units[exponent];
        return StringUtils.f("%.1f %s", bytes / Math.pow(base, exponent), unit);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy