RowKeyUtil { public static byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) { int offset = rowKeyPrefix.length; while (offset > 0) { if (rowKeyPrefix[offset - 1] != (byte) 0xFF) { break; } offset--; } if (offset == 0) { return EMPTY_KEY; } byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset); newStopRow[newStopRow.length - 1]++; return newStopRow; }  static byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix);  }
@Test public void test_simple() { byte[] key = "test".getBytes(); byte[] nextKey = RowKeyUtil.calculateTheClosestNextRowKeyForPrefix(key); Assert.assertEquals((byte) (key[key.length - 1] + 1), nextKey[key.length - 1]); }
@Test public void test_unsignedRange() { byte[] key = new byte[] {0x7f}; byte[] nextKey = RowKeyUtil.calculateTheClosestNextRowKeyForPrefix(key); Assert.assertEquals((byte) 0x80, nextKey[0]); }
@Test public void test_trailing0xFF() { byte trailer = (byte) 0xFF; byte[] key = new byte[] {0x01, trailer, trailer, trailer}; byte[] nextKey = RowKeyUtil.calculateTheClosestNextRowKeyForPrefix(key); Assert.assertEquals((byte) 0x2, nextKey[0]); }