@ SuppressWarnings ( "fallthrough" ) public static long convertToMachineFriendlyByteSize ( String size ) { double d ; try { d = Double . parseDouble ( size . replaceAll ( "[GMK]?[B]?$" , "" ) ) ; } catch ( NumberFormatException e ) { String msg = "Size must be specified as bytes (B), " + "kilobytes (KB), megabytes (MB), gigabytes (GB). " + "E.g. 1024, 1KB, 10M, 10MB, 100G, 100GB" ; throw new NumberFormatException ( msg + " " + e . getMessage ( ) ) ; } long l = Math . round ( d * 1024 * 1024 * 1024L ) ; int index = Math . max ( 0 , size . length ( ) - ( size . endsWith ( "B" ) ? 2 : 1 ) ) ; switch ( size . charAt ( index ) ) { default : l /= 1024 ; case ' ' : l /= 1024 ; case ' ' : l /= 1024 ; case ' ' : return l ; } }
Convert byte size into machine friendly format .