MCPcopy Create free account
hub / github.com/grpc/grpc-java / percentEncode

Method percentEncode

api/src/main/java/io/grpc/Uri.java:1113–1144  ·  view source on GitHub ↗
(String s, BitSet allowedCodePoints)

Source from the content-addressed store, hash-verified

1111 }
1112
1113 @Nullable
1114 private static String percentEncode(String s, BitSet allowedCodePoints) {
1115 if (s == null) {
1116 return null;
1117 }
1118 CharsetEncoder encoder =
1119 StandardCharsets.UTF_8
1120 .newEncoder()
1121 .onMalformedInput(CodingErrorAction.REPORT)
1122 .onUnmappableCharacter(CodingErrorAction.REPORT);
1123 ByteBuffer utf8Bytes;
1124 try {
1125 utf8Bytes = encoder.encode(CharBuffer.wrap(s));
1126 } catch (MalformedInputException e) {
1127 throw new IllegalArgumentException("Malformed input", e); // Must be a broken surrogate pair.
1128 } catch (CharacterCodingException e) {
1129 throw new VerifyException(e); // Should not happen when encoding to UTF-8.
1130 }
1131
1132 StringBuilder sb = new StringBuilder();
1133 while (utf8Bytes.hasRemaining()) {
1134 int b = 0xff & utf8Bytes.get();
1135 if (allowedCodePoints.get(b)) {
1136 sb.append((char) b);
1137 } else {
1138 sb.append('%');
1139 sb.append(hexDigitsByVal[(b & 0xF0) >> 4]);
1140 sb.append(hexDigitsByVal[b & 0x0F]);
1141 }
1142 }
1143 return sb.toString();
1144 }
1145
1146 private static void checkPercentEncodedArg(String s, String what, BitSet allowedChars) {
1147 percentDecode(s, what, allowedChars, null);

Callers 5

setPathMethod · 0.80
setFragmentMethod · 0.80
setUserInfoMethod · 0.80
setHostMethod · 0.80
toUriStringMethod · 0.80

Calls 5

appendMethod · 0.80
wrapMethod · 0.65
getMethod · 0.65
encodeMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected