public static MemorySize parseMemorySize(String memorySizeAsStr)
  {
    if(memorySizeAsStr == null || memorySizeAsStr.length() == 0)
      return null;

    int len = memorySizeAsStr.length();

    int orderedSizeUnitIdx = 0;
    int orderedSizeUnitLen = ORDERED_SIZE_UNIT.length;

    int startDigitsIdx = 0;
    boolean expectingDigits = true;

    MemorySize ms = ZERO_BYTES;
    for(int i = 0; i < len; i++)
    {
      char c = memorySizeAsStr.charAt(i);
      if(c >= '0' && c <= '9')
      {
        expectingDigits = false;
        continue;
      }

      if(expectingDigits)
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "': found '" + c
            + "' at pos " + i + ", was expecting a digit");

      for(; orderedSizeUnitIdx < orderedSizeUnitLen; orderedSizeUnitIdx++)
      {
        SizeUnit sizeUnit = ORDERED_SIZE_UNIT[orderedSizeUnitIdx];
        String displayChar = sizeUnit.getDisplayChar();
        
        if(displayChar.length() > 0 && c == displayChar.charAt(0))
        {
          try
          {
            long size = Long.parseLong(memorySizeAsStr.substring(startDigitsIdx, i));
            ms = ms.add(new MemorySize(size, sizeUnit));
            orderedSizeUnitIdx++;
            startDigitsIdx = i + 1;
            expectingDigits = true;
            break;
          }
          catch(NumberFormatException e)
          {
            throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "'", e);
          }
        }
      }
      
      if(orderedSizeUnitIdx == orderedSizeUnitLen)
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr
            + "': found invalid character '" + c + "' at pos " + i);
    }

    if(startDigitsIdx < len)
    {
      try
      {
        long size = Long.parseLong(memorySizeAsStr.substring(startDigitsIdx, len));
        ms = ms.add(new MemorySize(size, SizeUnit.BYTE));
      }
      catch(NumberFormatException e)
      {
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "'", e);
      }
    }
    return ms;
  }
public static MemorySize parseMemorySize(String memorySizeAsStr)
  {
    if(memorySizeAsStr == null || memorySizeAsStr.length() == 0)
      return null;

    int len = memorySizeAsStr.length();

    int orderedSizeUnitIdx = 0;
    int orderedSizeUnitLen = ORDERED_SIZE_UNIT.length;

    int startDigitsIdx = 0;
    boolean expectingDigits = true;

    MemorySize ms = ZERO_BYTES;
    for(int i = 0; i < len; i++)
    {
      char c = memorySizeAsStr.charAt(i);
      if(c >= '0' && c <= '9')
      {
        expectingDigits = false;
        continue;
      }

      if(expectingDigits)
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "': found '" + c
            + "' at pos " + i + ", was expecting a digit");

      for(; orderedSizeUnitIdx < orderedSizeUnitLen; orderedSizeUnitIdx++)
      {
        SizeUnit sizeUnit = ORDERED_SIZE_UNIT[orderedSizeUnitIdx];
        String displayChar = sizeUnit.getDisplayChar();
        
        if(displayChar.length() > 0 && c == displayChar.charAt(0))
        {
          try
          {
            long size = Long.parseLong(memorySizeAsStr.substring(startDigitsIdx, i));
            ms = ms.add(new MemorySize(size, sizeUnit));
            orderedSizeUnitIdx++;
            startDigitsIdx = i + 1;
            expectingDigits = true;
            break;
          }
          catch(NumberFormatException e)
          {
            throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "'", e);
          }
        }
      }
      
      if(orderedSizeUnitIdx == orderedSizeUnitLen)
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr
            + "': found invalid character '" + c + "' at pos " + i);
    }

    if(startDigitsIdx < len)
    {
      try
      {
        long size = Long.parseLong(memorySizeAsStr.substring(startDigitsIdx, len));
        ms = ms.add(new MemorySize(size, SizeUnit.BYTE));
      }
      catch(NumberFormatException e)
      {
        throw new IllegalArgumentException("Unable to parse '" + memorySizeAsStr + "'", e);
      }
    }
    return ms;
  }
public static MemorySize parseMemorySize ( String memorySizeAsStr ) { if ( memorySizeAsStr == null || memorySizeAsStr . length ( ) == 0 ) return null ; int len = memorySizeAsStr . length ( ) ; int orderedSizeUnitIdx = 0 ; int orderedSizeUnitLen = ORDERED_SIZE_UNIT . length ; int startDigitsIdx = 0 ; boolean expectingDigits = true ; MemorySize ms = ZERO_BYTES ; for ( int i = 0 ; i < len ; i ++ ) { char c = memorySizeAsStr . charAt ( i ) ; if ( c >= ' ' && c <= ' ' ) { expectingDigits = false ; continue ; } if ( expectingDigits ) throw new IllegalArgumentException ( "Unable to parse '" + memorySizeAsStr + "': found '" + c + "' at pos " + i + ", was expecting a digit" ) ; for ( ; orderedSizeUnitIdx < orderedSizeUnitLen ; orderedSizeUnitIdx ++ ) { SizeUnit sizeUnit = ORDERED_SIZE_UNIT [ orderedSizeUnitIdx ] ; String displayChar = sizeUnit . getDisplayChar ( ) ; if ( displayChar . length ( ) > 0 && c == displayChar . charAt ( 0 ) ) { try { long size = Long . parseLong ( memorySizeAsStr . substring ( startDigitsIdx , i ) ) ; ms = ms . add ( new MemorySize ( size , sizeUnit ) ) ; orderedSizeUnitIdx ++ ; startDigitsIdx = i + 1 ; expectingDigits = true ; break ; } catch ( NumberFormatException e ) { throw new IllegalArgumentException ( "Unable to parse '" + memorySizeAsStr + "'" , e ) ; } } } if ( orderedSizeUnitIdx == orderedSizeUnitLen ) throw new IllegalArgumentException ( "Unable to parse '" + memorySizeAsStr + "': found invalid character '" + c + "' at pos " + i ) ; } if ( startDigitsIdx < len ) { try { long size = Long . parseLong ( memorySizeAsStr . substring ( startDigitsIdx , len ) ) ; ms = ms . add ( new MemorySize ( size , SizeUnit . BYTE ) ) ; } catch ( NumberFormatException e ) { throw new IllegalArgumentException ( "Unable to parse '" + memorySizeAsStr + "'" , e ) ; } } return ms ; }
Create a new instance of MemorySize by parsing the given string
of format e.g. 2g3m4k5

@param memorySizeAsStr
@return a new instance of MemorySize
Create a new instance of MemorySize by parsing the given string of format e . g . 2g3m4k5