This is an automated email from the ASF dual-hosted git repository. zhaojinchao pushed a commit to branch revert-25077-dev in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
commit ffd3c9fdd6d04fe0a9ace34717ea3a029da1dede Author: zhaojinchao <[email protected]> AuthorDate: Mon Apr 10 18:00:57 2023 +0800 Revert "Remove UnstableApiUsage on CharDigestLikeEncryptAlgorithm (#25077)" This reverts commit 37d13d25a32dfb55fa411ae2fec34aa949f4df50. --- .../like/CharDigestLikeEncryptAlgorithm.java | 55 +++++++++++++++------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/like/CharDigestLikeEncryptAlgorithm.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/like/CharDigestLikeEncryptAlgorithm.java index 87f40615af9..edabec91b15 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/like/CharDigestLikeEncryptAlgorithm.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/like/CharDigestLikeEncryptAlgorithm.java @@ -17,25 +17,25 @@ package org.apache.shardingsphere.encrypt.algorithm.like; +import com.google.common.base.Charsets; +import com.google.common.base.Strings; +import com.google.common.io.CharStreams; +import com.google.common.io.LineProcessor; import lombok.SneakyThrows; -import org.apache.shardingsphere.encrypt.api.context.EncryptContext; -import org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException; import org.apache.shardingsphere.encrypt.spi.LikeEncryptAlgorithm; +import org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException; +import org.apache.shardingsphere.encrypt.api.context.EncryptContext; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; import java.util.Map; import java.util.Properties; -import java.util.stream.Collectors; -import java.util.stream.IntStream; /** * Char digest like encrypt algorithm. */ +@SuppressWarnings("UnstableApiUsage") public final class CharDigestLikeEncryptAlgorithm implements LikeEncryptAlgorithm<Object, String> { private static final String DELTA = "delta"; @@ -107,14 +107,37 @@ public final class CharDigestLikeEncryptAlgorithm implements LikeEncryptAlgorith } private Map<Character, Integer> createCharIndexes(final Properties props) { - String dictContent = props.getProperty(DICT, loadDefaultDict()); - return IntStream.range(0, dictContent.length()).boxed().collect(Collectors.toMap(dictContent::charAt, index -> index, (a, b) -> b)); + String dictContent = props.containsKey(DICT) && !Strings.isNullOrEmpty(props.getProperty(DICT)) ? props.getProperty(DICT) : initDefaultDict(); + Map<Character, Integer> result = new HashMap<>(dictContent.length(), 1); + for (int index = 0; index < dictContent.length(); index++) { + result.put(dictContent.charAt(index), index); + } + return result; } - @SneakyThrows({IOException.class, URISyntaxException.class}) - private String loadDefaultDict() { - List<String> lines = Files.readAllLines(Paths.get(ClassLoader.getSystemResource("algorithm/like/common_chinese_character.dict").toURI()), StandardCharsets.UTF_8); - return lines.stream().filter(each -> !each.isEmpty() && !each.startsWith("#")).collect(Collectors.joining()); + @SneakyThrows + private String initDefaultDict() { + InputStream inputStream = CharDigestLikeEncryptAlgorithm.class.getClassLoader().getResourceAsStream("algorithm/like/common_chinese_character.dict"); + LineProcessor<String> lineProcessor = new LineProcessor<String>() { + + private final StringBuilder builder = new StringBuilder(); + + @Override + public boolean processLine(final String line) { + if (line.startsWith("#") || 0 == line.length()) { + return true; + } else { + builder.append(line); + return false; + } + } + + @Override + public String getResult() { + return builder.toString(); + } + }; + return CharStreams.readLines(new InputStreamReader(inputStream, Charsets.UTF_8), lineProcessor); } @Override
