public static long parseSize ( String s ) { Matcher m = p_mem . matcher ( s ) ; if ( m . find ( ) ) { long i = Long . parseLong ( m . group ( 1 ) ) ; char multiplier = ' ' ; if ( m . group ( 2 ) != null && m . group ( 2 ) . length ( ) == 1 ) { multiplier = m . group ( 2 ) . charAt ( 0 ) ; } if ( multiplier == ' ' ) { return ( long ) i ; } if ( multiplier == ' ' ) { return ( long ) i * 1024l ; } if ( multiplier == ' ' ) { return ( long ) i * 1024l * 1024l ; } if ( multiplier == ' ' ) { return ( long ) i * 1024l * 1024l * 1024l ; } } return - 1 ; }
This methods parses the given String which should represent a size to a long . K is interpreted as 1024 M as 1024^2 and G as 1024^3 .