Return a human-readable representation of a byte size.

        @param size: Number of bytes as an integer or string.
        @return: String of length 10 with the formatted result.
546624
def human_size(size):
    
    if isinstance(size, string_types):
        size = int(size, 10)

    if size < 0:
        return "-??? bytes"

    if size < 1024:
        return "%4d bytes" % size
    for unit in ("KiB", "MiB", "GiB"):
        size /= 1024.0
        if size < 1024:
            return "%6.1f %s" % (size, unit)

    return "%6.1f GiB" % size
495796
public static String to416HeaderRangeString(long size)
    {
        StringBuffer sb = new StringBuffer(40);
        sb.append("bytes */");
        sb.append(size);
        return sb.toString();
    }
1150748
def get_human_size(size, use_giga=True):
    '''
    '''
    size_kb = '{0:,}'.format(size)
    if size < SIZE_K:
        return ('{0} B'.format(size), size_kb)
    if size < SIZE_M:
        return ('{0:.1f} kB'.format(size / SIZE_K), size_kb)
    if size < SIZE_G or not use_giga:
        return ('{0:.1f} MB'.format(size / SIZE_M), size_kb)
    if size < SIZE_T:
        return ('{0:.1f} GB'.format(size / SIZE_G), size_kb)
    return ('{0:.1f} TB'.format(size / SIZE_T), size_kb)
1274670
def hsize(bytes):
    """"""
    sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
    if bytes == 0:
        return '0 Byte'
    i = int(math.floor(math.log(bytes) / math.log(1024)))
    r = round(bytes / math.pow(1024, i), 2)
    return str(r) + '' + sizes[i]
278423
public static String asIbi(long size) {
        if (size > GIBI) {
            return String.format("%.1f GiB", (size / GIBI));
        } else if (size > MIBI) {
            return String.format("%.1f MiB", (size / MIBI));
        } else if (size > KIBI) {
            return String.format("%.1f KiB", (size / KIBI));
        } else {
            return String.format("%d B", size);
        }
    }
1117633
def read_string(self, size):
        
        if size < 0:
            raise errors.DecodeError('Negative size %d' % size)
        s = self._input.read(size)
        if len(s) != size:
            raise errors.DecodeError('String claims to have %d bytes, but read %d' % (size, len(s)))
        self._pos += len(s)  # Only advance by the number of bytes actually read.
        return s
1340088
public static String formatSize( long size )
	{
		if (size == 0)
			return "0";
		
		StringBuilder sb = new StringBuilder();
		if (size < 0)
		{
			size = -size;
			sb.append("-");
		}
	
		long gigs = size / (1024*1024*1024);
		if (gigs > 0)
		{
			sb.append(new DecimalFormat("######.###").format((double)size/(1024*1024*1024)));
			sb.append(" GB");
			return sb.toString();
		}
		
		long megs = size / (1024*1024);
		if (megs > 0)
		{
			sb.append(new DecimalFormat("###.#").format((double)size/(1024*1024)));
			sb.append(" MB");
			return sb.toString();
		}
		
		long kbs = size / (1024);
		if (kbs > 0)
		{
			sb.append(new DecimalFormat("###.#").format((double)size/(1024)));
			sb.append(" KB");
			return sb.toString();
		}
		
		sb.append(size);
		sb.append(" B");
		
		return sb.toString();
	}
190342
def human_size(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']):
    
    return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:])
839083
def format_sizeof(num, suffix='bytes'):
    ''''''
    for unit in ['','K','M','G','T','P','E','Z']:
        if abs(num) < 1000.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1000.0
    return "%.1f%s%s" % (num, 'Y', suffix)
494910
def format_size(size):
    
    units = ['B', 'KB', 'MB', 'GB']
    unit = ''
    n = size
    old_n = n
    value = size
    for i in units:
        old_n = n
        x, y = divmod(n, 1024)
        if x == 0:
            unit = i
            value = y
            break

        n = x
        unit = i
        value = old_n
    return str(value)+unit
69464
def str_filesize(size):
    
    import bisect
    
    d = [(1024-1,'K'), (1024**2-1,'M'), (1024**3-1,'G'), (1024**4-1,'T')]
    s = [x[0] for x in d]
    
    index = bisect.bisect_left(s, size) - 1
    if index == -1:
        return str(size)
    else:
        b, u = d[index]
    return str(size / (b+1)) + u
241877
def flash_size_bytes(size):
    
    if "MB" in size:
        return int(size[:size.index("MB")]) * 1024 * 1024
    elif "KB" in size:
        return int(size[:size.index("KB")]) * 1024
    else:
        raise FatalError("Unknown size %s" % size)
301691
public String produce(int size) {
        byte bytes[] = new byte[size];
        _random.nextBytes(bytes);
        return Strings.toHexString(bytes);
    }
604298
def _readable_part_size(num_bytes):
    ""
    B = num_bytes
    KB = float(1024)
    MB = float(KB * 1024)
    GB = float(MB * 1024)
    TB = float(GB * 1024)

    if B < KB:
        return '{0} {1}'.format(B, 'bytes' if B != 1 else 'byte')
    elif KB <= B < MB:
        return '{0:.2f} KiB'.format(B/KB)
    elif MB <= B < GB:
        return '{0:.2f} MiB'.format(B/MB)
    elif GB <= B < TB:
        return '{0:.2f} GiB'.format(B/GB)
    elif TB <= B:
        return '{0:.2f} TiB'.format(B/TB)
898261
def format_size(num, format_str='{num:.1f} {unit}'):
    '''
    '''
    for unit in ('B', 'KiB', 'MiB', 'GiB'):
        if -1024 < num < 1024:
            return format_str.format(num=num, unit=unit)

        num /= 1024.0

    return format_str.format(num=num, unit='TiB')
997993
def resize(self, size):
        
        count = max(int(size), 0) - len(self)

        if count == 0:
            pass
        elif -count == len(self):
            self._value = bytes()
        elif count > 0:
            self._value += b'\x00' * count
        else:
            self._value = self._value[:count]
        size = len(self)
        self._bit_size = size * 8
        self._align_to_byte_size = size
1025239
public static long sizeToLong(String size) {
        size = size.trim();
        if (Checker.isNotEmpty(size)) {
            String num;
            if (size.contains(ValueConsts.SPACE)) {
                num = size.split(ValueConsts.SPACE)[0];
            } else {
                num = Utils.extractDigit(size);
            }
            double result;
            if (size.contains(SIZE_TB)) {
                result = stringToDouble(num) * ValueConsts.TB;
            } else if (size.contains(SIZE_GB)) {
                result = stringToDouble(num) * ValueConsts.GB;
            } else if (size.contains(SIZE_MB)) {
                result = stringToDouble(num) * ValueConsts.MB;
            } else if (size.contains(SIZE_KB)) {
                result = stringToDouble(num) * ValueConsts.KB;
            } else {
                result = stringToDouble(num);
            }
            return (long) result;
        }
        return -1;
    }