(String s, BitSet allowedCodePoints)
| 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); |
no test coverage detected