@Test(dataProvider = "truncateKeyData") public void truncateKey(String key, String expectedResult) { assertEquals(KeyUtils.truncateKey(key), expectedResult == null ? key : expectedResult); }
public static String truncateKey(String key) { byte[] keyBytes = key.getBytes(Charsets.UTF_8); if (keyBytes.length < MAX_KEY_BYTES || (keyBytes.length == MAX_KEY_BYTES && keyBytes[keyBytes.length - 1] != LAST_HASH_CHAR)) { return key; } byte[] keyHash = generateKeyHash(keyBytes); assert keyHash.length == HASH_LENGTH; assert keyHash[keyHash.length - 1] == LAST_HASH_CHAR; int lastCharIndex = KEY_TRUNCATE_BYTES - 1; while ((keyBytes[lastCharIndex] & 0xC0) == 0x80) { lastCharIndex -= 1; } if (utf8CharLen(keyBytes[lastCharIndex]) > (KEY_TRUNCATE_BYTES - lastCharIndex)) { while (lastCharIndex < KEY_TRUNCATE_BYTES) { keyBytes[lastCharIndex++] = '+'; } } System.arraycopy(keyHash, 0, keyBytes, KEY_TRUNCATE_BYTES, keyHash.length); return new String(keyBytes, 0, MAX_KEY_BYTES, Charsets.UTF_8); }
KeyUtils { public static String truncateKey(String key) { byte[] keyBytes = key.getBytes(Charsets.UTF_8); if (keyBytes.length < MAX_KEY_BYTES || (keyBytes.length == MAX_KEY_BYTES && keyBytes[keyBytes.length - 1] != LAST_HASH_CHAR)) { return key; } byte[] keyHash = generateKeyHash(keyBytes); assert keyHash.length == HASH_LENGTH; assert keyHash[keyHash.length - 1] == LAST_HASH_CHAR; int lastCharIndex = KEY_TRUNCATE_BYTES - 1; while ((keyBytes[lastCharIndex] & 0xC0) == 0x80) { lastCharIndex -= 1; } if (utf8CharLen(keyBytes[lastCharIndex]) > (KEY_TRUNCATE_BYTES - lastCharIndex)) { while (lastCharIndex < KEY_TRUNCATE_BYTES) { keyBytes[lastCharIndex++] = '+'; } } System.arraycopy(keyHash, 0, keyBytes, KEY_TRUNCATE_BYTES, keyHash.length); return new String(keyBytes, 0, MAX_KEY_BYTES, Charsets.UTF_8); } }
KeyUtils { public static String truncateKey(String key) { byte[] keyBytes = key.getBytes(Charsets.UTF_8); if (keyBytes.length < MAX_KEY_BYTES || (keyBytes.length == MAX_KEY_BYTES && keyBytes[keyBytes.length - 1] != LAST_HASH_CHAR)) { return key; } byte[] keyHash = generateKeyHash(keyBytes); assert keyHash.length == HASH_LENGTH; assert keyHash[keyHash.length - 1] == LAST_HASH_CHAR; int lastCharIndex = KEY_TRUNCATE_BYTES - 1; while ((keyBytes[lastCharIndex] & 0xC0) == 0x80) { lastCharIndex -= 1; } if (utf8CharLen(keyBytes[lastCharIndex]) > (KEY_TRUNCATE_BYTES - lastCharIndex)) { while (lastCharIndex < KEY_TRUNCATE_BYTES) { keyBytes[lastCharIndex++] = '+'; } } System.arraycopy(keyHash, 0, keyBytes, KEY_TRUNCATE_BYTES, keyHash.length); return new String(keyBytes, 0, MAX_KEY_BYTES, Charsets.UTF_8); }  }
KeyUtils { public static String truncateKey(String key) { byte[] keyBytes = key.getBytes(Charsets.UTF_8); if (keyBytes.length < MAX_KEY_BYTES || (keyBytes.length == MAX_KEY_BYTES && keyBytes[keyBytes.length - 1] != LAST_HASH_CHAR)) { return key; } byte[] keyHash = generateKeyHash(keyBytes); assert keyHash.length == HASH_LENGTH; assert keyHash[keyHash.length - 1] == LAST_HASH_CHAR; int lastCharIndex = KEY_TRUNCATE_BYTES - 1; while ((keyBytes[lastCharIndex] & 0xC0) == 0x80) { lastCharIndex -= 1; } if (utf8CharLen(keyBytes[lastCharIndex]) > (KEY_TRUNCATE_BYTES - lastCharIndex)) { while (lastCharIndex < KEY_TRUNCATE_BYTES) { keyBytes[lastCharIndex++] = '+'; } } System.arraycopy(keyHash, 0, keyBytes, KEY_TRUNCATE_BYTES, keyHash.length); return new String(keyBytes, 0, MAX_KEY_BYTES, Charsets.UTF_8); }  static String truncateKey(String key); }
KeyUtils { public static String truncateKey(String key) { byte[] keyBytes = key.getBytes(Charsets.UTF_8); if (keyBytes.length < MAX_KEY_BYTES || (keyBytes.length == MAX_KEY_BYTES && keyBytes[keyBytes.length - 1] != LAST_HASH_CHAR)) { return key; } byte[] keyHash = generateKeyHash(keyBytes); assert keyHash.length == HASH_LENGTH; assert keyHash[keyHash.length - 1] == LAST_HASH_CHAR; int lastCharIndex = KEY_TRUNCATE_BYTES - 1; while ((keyBytes[lastCharIndex] & 0xC0) == 0x80) { lastCharIndex -= 1; } if (utf8CharLen(keyBytes[lastCharIndex]) > (KEY_TRUNCATE_BYTES - lastCharIndex)) { while (lastCharIndex < KEY_TRUNCATE_BYTES) { keyBytes[lastCharIndex++] = '+'; } } System.arraycopy(keyHash, 0, keyBytes, KEY_TRUNCATE_BYTES, keyHash.length); return new String(keyBytes, 0, MAX_KEY_BYTES, Charsets.UTF_8); }  static String truncateKey(String key);  }