@jagged marlin I'm very proud of this solution ๐ :
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
static String encodeHash(final byte[] bytes, final Predicate<Character> excludeFromConversion) {
return Stream.of(bytes)
.<Character>mapMulti((bytes1, consumer) -> {
for (byte b : bytes1)
consumer.accept((char) Byte.toUnsignedInt(b));
})
.map(c -> {
if (excludeFromConversion.test(c))
return String.valueOf(c);
return String.format("%%%02X", (int) c);
})
.collect(Collectors.joining());
}
public static void main(String[] args) {
final byte[] hash = {
0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf1,
0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x12,
(byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a
};
System.out.println("encodeHash(hash, b -> Character.isLetterOrDigit(b) || b == '.' || b == '-' || b == '_' || b == '~') = "
+ encodeHash(hash, b -> Character.isLetterOrDigit(b) || b == '.' || b == '-' || b == '_' || b == '~'));
System.out.println("encodeHash(hash, b -> String.valueOf(b).matches(\"[0-9a-zA-Z.\\\\-_~]\")) = "
+ encodeHash(hash, b -> String.valueOf(b).matches("[0-9a-zA-Z.\\-_~]")));
}
}