public static String toHumanSize(long sizeInBytes){
  if (sizeInBytes < 0) {
    throw new IllegalArgumentException(String.format("Negative size: %d",sizeInBytes));
  }
  if (sizeInBytes < K) {
    return String.format("%d B",sizeInBytes);
  }
  float size=0.0f;
  String suffix="PB";
  for (int i=0, l=SIZE_UNITS.length; i < l; i++) {
    Pair<Long,String> sizeUnit=SIZE_UNITS[i];
    if (sizeInBytes >= sizeUnit.first) {
      size=(float)sizeInBytes / sizeUnit.first;
      suffix=sizeUnit.second;
      break;
    }
  }
  return String.format((size % 1.0f == 0) ? "%.0f %s" : "%.1f %s",size,suffix);
}

public static String toHumanSize ( long sizeInBytes ) { if ( sizeInBytes < 0 ) { throw new IllegalArgumentException ( String . format ( "Negative size: %d" , sizeInBytes ) ) ; } if ( sizeInBytes < K ) { return String . format ( "%d B" , sizeInBytes ) ; } float size = 0.0f ; String suffix = "PB" ; for ( int i = 0 , l = SIZE_UNITS . length ; i < l ; i ++ ) { Pair < Long , String > sizeUnit = SIZE_UNITS [ i ] ; if ( sizeInBytes >= sizeUnit . first ) { size = ( float ) sizeInBytes / sizeUnit . first ; suffix = sizeUnit . second ; break ; } } return String . format ( ( size % 1.0f == 0 ) ? "%.0f %s" : "%.1f %s" , size , suffix ) ; }
convert memory to human readable representation , e . g . 1 kb
