Java Size byteCountToDisplaySize(long bytes)

Here you can find the source of byteCountToDisplaySize(long bytes)

Description

Formats the given number of bytes to a human-readable representation up to GiB units.

This method will provide higher accuracy than the same one found in commons-io, by offering 2 decimal places for GiB and MiB and 1 decimal place for KiB ranges.

License

Apache License

Parameter

Parameter Description
bytes The number of bytes to be formatted.

Return

The formatted result.

Declaration

public static String byteCountToDisplaySize(long bytes) 

Method Source Code

//package com.java2s;
/*//ww  w .  ja v  a 2  s . c o m
 * Licensed to the Indoqa Software Design und Beratung GmbH (Indoqa) under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Indoqa licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * 2<sup>10</sup> bytes (aka 1 Kibibyte).
     */
    public static final long ONE_KIB = (long) Math.pow(2, 10);
    /**
     * 2<sup>20</sup> bytes (aka 1 Mebibyte)
     */
    public static final long ONE_MIB = (long) Math.pow(2, 20);
    /**
     * 2<sup>30</sup> bytes (aka 1 Gibibyte)
     */
    public static final long ONE_GIB = (long) Math.pow(2, 30);
    private static final DecimalFormat GIBI_BYTE_FORMAT = new DecimalFormat("#,##0.00 GiB");
    private static final DecimalFormat MEBI_BYTE_FORMAT = new DecimalFormat("#,##0.00 MiB");
    private static final DecimalFormat KIBI_BYTE_FORMAT = new DecimalFormat("#,##0.0 KiB");
    private static final DecimalFormat BYTE_FORMAT = new DecimalFormat("#,##0 B");

    /**
     * Formats the given number of bytes to a human-readable representation up to GiB units.<br>
     * <br>
     * This method will provide higher accuracy than the same one found in commons-io, by offering 2 decimal places for GiB and MiB and
     * 1 decimal place for KiB ranges. <br>
     * <br>
     * The result will contain the appropriate binary unit (B, KiB, MiB or GiB)
     *
     * @param bytes The number of bytes to be formatted.
     * @return The formatted result.
     */
    public static String byteCountToDisplaySize(long bytes) {
        if (bytes / ONE_GIB > 0) {
            return GIBI_BYTE_FORMAT.format((double) bytes / ONE_GIB);
        }

        if (bytes / ONE_MIB > 0) {
            return MEBI_BYTE_FORMAT.format((double) bytes / ONE_MIB);
        }

        if (bytes / ONE_KIB > 0) {
            return KIBI_BYTE_FORMAT.format((double) bytes / ONE_KIB);
        }

        return BYTE_FORMAT.format(bytes);
    }
}

Related

  1. byteCountToDisplaySize(long size)
  2. byteCountToDisplaySize(long size)
  3. byteCountToDisplaySize(long size)
  4. byteSizeString(long bytes)