public static int findTrimmedLength(byte[] bytes, int start, int length, int leadingSpaces) {
    int trailingSpaces = findTrailingSpaces(bytes, start, length);
    length = length - leadingSpaces;
    // If string is entirely whitespace, no need to apply trailingSpaces.
    if (length > 0) {
      length = length - trailingSpaces;
    }
    return length;
  }
Finds trimmed length of utf-8 string
common/src/java/org/apache/hive/common/util/HiveStringUtils.java