001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.text.DecimalFormatSymbols;
020
021/**
022 * Unit utils.
023 */
024public final class UnitUtils {
025
026    private UnitUtils() {
027    }
028
029    /**
030     * If having a size in bytes and wanting to print this in human friendly\ format with xx kB, xx MB, xx GB instead of
031     * a large byte number.
032     *
033     * @param bytes the value in bytes
034     */
035    public static String printUnitFromBytes(long bytes) {
036        if (bytes < 0) {
037            return "";
038        }
039
040        // http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
041        int unit = 1000;
042        if (bytes < unit) {
043            return bytes + " B";
044        }
045        int exp = (int) (Math.log(bytes) / Math.log(unit));
046        String pre = "" + "kMGTPE".charAt(exp - 1);
047        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
048    }
049
050    /**
051     * If having a size in bytes and wanting to print this in human friendly\ format with xx kB, xx MB, xx GB instead of
052     * a large byte number, using dot as decimal separator.
053     *
054     * @param bytes the value in bytes
055     */
056    public static String printUnitFromBytesDot(long bytes) {
057        return printUnitFromBytes(bytes, '.');
058    }
059
060    /**
061     * If having a size in bytes and wanting to print this in human friendly\ format with xx kB, xx MB, xx GB instead of
062     * a large byte number.
063     *
064     * @param bytes   the value in bytes
065     * @param decimal the decimal separator char to use
066     */
067    public static String printUnitFromBytes(long bytes, char decimal) {
068        if (bytes < 0) {
069            return "";
070        }
071
072        // http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
073        int unit = 1000;
074        if (bytes < unit) {
075            return bytes + " B";
076        }
077        int exp = (int) (Math.log(bytes) / Math.log(unit));
078        String pre = "" + "kMGTPE".charAt(exp - 1);
079        String answer = String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
080
081        char sep = DecimalFormatSymbols.getInstance().getDecimalSeparator();
082        if (decimal != sep) {
083            answer = answer.replace(sep, decimal);
084        }
085        return answer;
086    }
087
088}