code: public static final int asInt(byte[] data) {
          if (null == data) {
              return -1;
          }
  
          int start = 0;
          // skip leading whitespace
          for (; start < data.length; start++) {
              if (' ' != data[start] || '\t' == data[start]) {
                  break;
              }
          }
          // skip trailing whitespace
          int i = data.length - 1;
          for (; start <= i; i--) {
              if (' ' != data[i] && '\t' != data[i]) {
                  break;
              }
          }
          if (i <= start) {
              // empty content
              return -1;
          }
          int intVal = 0;
          int mark = 1;
          int digit;
          for (; start <= i; i--) {
              digit = data[i] - '0';
              if (0 > digit || 9 < digit) {
                  // stop on any nondigit, if it's not a dash then throw an exc
                  if ('-' != data[i]) {
                      throw new NumberFormatException("Invalid digit: " + data[i]);
                  }
                  break;
              }
              intVal += digit * mark;
              mark *= 10;
          }
          if (start < i) {
              // didn't use the whole content...
              throw new NumberFormatException("Invalid data");
          }
  
          // check for negative numbers
          if (start == i && data[i] == '-') {
              intVal = -intVal;
          }
  
          return intVal;
      }
docs: Convert a byte array to an int, trimming any whitespace. Null input
  will return a -1. Invalid numbers will throw a NumberFormatException.
  
  @param data
  @return int