BlockHash { protected static int MatchingBytesToLeft(ByteBuffer source_match_start, int source_match_offset, byte[] target_match_start, int target_match_start_offset, int max_bytes) { int bytes_found = 0; while (bytes_found < max_bytes) { --source_match_offset; --target_match_start_offset; if (source_match_start.get(source_match_offset) != target_match_start[target_match_start_offset]) { break; } ++bytes_found; } return bytes_found; } BlockHash(byte[] source_data, int starting_offset, boolean populate_hash_table);  BlockHash(ByteBuffer source_data, int starting_offset, boolean populate_hash_table); static BlockHash CreateDictionaryHash(byte[] dictionary_data); static BlockHash CreateTargetHash(byte[] target_data, int dictionary_size); static BlockHash CreateTargetHash(ByteBuffer target_data, int dictionary_size); void AddOneIndexHash(int index, int hash_value); void AddAllBlocksThroughIndex(int end_index); void FindBestMatch(int hash_value, ByteBuffer target, Match best_match); void FindBestMatch(int hash_value, byte[] target_candidate, int target_candidate_start, byte[] target, int target_start, Match best_match); static final int kBlockSize; }
@Test public void LeftLimitedByMaxBytes() throws UnsupportedEncodingException { final int expected_length = kBlockSize * "e hear is ".getBytes(StandardCharsets.US_ASCII).length; final int max_bytes = expected_length - 1; Assert.assertEquals(max_bytes, BlockHash.MatchingBytesToLeft( search_string, index_of_f_in_fearsome, search_string_altered, index_of_f_in_fearsome, max_bytes)); }
@Test public void LeftNotLimited() throws UnsupportedEncodingException { final int expected_length = kBlockSize * "e hear is ".getBytes(StandardCharsets.US_ASCII).length; final int max_bytes = expected_length + 1; Assert.assertEquals(expected_length, BlockHash.MatchingBytesToLeft( search_string, index_of_f_in_fearsome, search_string_altered, index_of_f_in_fearsome, max_bytes)); Assert.assertEquals(expected_length, BlockHash.MatchingBytesToLeft( search_string, index_of_f_in_fearsome, search_string_altered, index_of_f_in_fearsome, Integer.MAX_VALUE)); }