public static int findTrailingSpaces(byte[] bytes, int start, int length) {
    int numSpaces;
    for (numSpaces = 0; numSpaces < length; ++numSpaces) {
      int curPos = start + (length - (numSpaces + 1));
      if (isAscii(bytes[curPos]) && Character.isWhitespace(bytes[curPos])) {
        continue;
      } else {
        break; // non-space character
      }
    }
    return numSpaces;
  }
Returns the number of trailing whitespace characters in the utf-8 string
common/src/java/org/apache/hive/common/util/HiveStringUtils.java