@Test public void testUnicodeCodePoint2PercentHexString() { assertEquals("%07", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(7, "UTF-8")); assertEquals("%07", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(7, "ISO-8859-1")); assertEquals("%20", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(32, "UTF-8")); assertEquals("%20", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(32, "ISO-8859-1")); assertEquals("%41", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(65, "UTF-8")); assertEquals("%41", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(65, "ISO-8859-1")); assertEquals("%7f", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(127, "UTF-8")); assertEquals("%7f", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(127, "ISO-8859-1")); assertEquals("%c2%80", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(128, "UTF-8")); assertEquals("%80", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(128, "ISO-8859-1")); assertEquals("%c3%86", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(198, "UTF-8")); assertEquals("%c6", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(198, "ISO-8859-1")); assertEquals("%c3%bf", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(255, "UTF-8")); assertEquals("%ff", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(255, "ISO-8859-1")); assertEquals("%e0%a0%80", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(2048, "UTF-8")); assertEquals("%e0%a4%81", StringValidateEncodeUtils.unicodeCodePoint2PercentHexString(2305, "UTF-8")); }
public static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName) { if (!Character.isDefined(codePoint)) throw new IllegalArgumentException( String.format("Given code point [U+%1$04X - %1$d] not assigned to an abstract character.", codePoint)); if (Character.getType(codePoint) == Character.SURROGATE) throw new IllegalArgumentException(String .format("Given code point [U+%1$04X - %1$d] is an unencodable (by itself) surrogate character.", codePoint)); Charset charset = Charset.availableCharsets().get(charsetName); if (charset == null) throw new IllegalArgumentException(String.format("Unsupported charset [%s].", charsetName)); char[] chars = Character.toChars(codePoint); ByteBuffer byteBuffer = null; try { byteBuffer = charset.newEncoder().encode(CharBuffer.wrap(chars)); } catch (CharacterCodingException e) { String message = String.format("Given code point [U+%1$04X - %1$d] cannot be encode in given charset [%2$s].", codePoint, charsetName); throw new IllegalArgumentException(message, e); } byteBuffer.rewind(); StringBuilder encodedString = new StringBuilder(); for (int i = 0; i < byteBuffer.limit(); i++) { String asHex = Integer.toHexString(byteBuffer.get() & 0xFF); encodedString.append("%").append(asHex.length() == 1 ? "0" : "").append(asHex); } return encodedString.toString(); }
StringValidateEncodeUtils { public static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName) { if (!Character.isDefined(codePoint)) throw new IllegalArgumentException( String.format("Given code point [U+%1$04X - %1$d] not assigned to an abstract character.", codePoint)); if (Character.getType(codePoint) == Character.SURROGATE) throw new IllegalArgumentException(String .format("Given code point [U+%1$04X - %1$d] is an unencodable (by itself) surrogate character.", codePoint)); Charset charset = Charset.availableCharsets().get(charsetName); if (charset == null) throw new IllegalArgumentException(String.format("Unsupported charset [%s].", charsetName)); char[] chars = Character.toChars(codePoint); ByteBuffer byteBuffer = null; try { byteBuffer = charset.newEncoder().encode(CharBuffer.wrap(chars)); } catch (CharacterCodingException e) { String message = String.format("Given code point [U+%1$04X - %1$d] cannot be encode in given charset [%2$s].", codePoint, charsetName); throw new IllegalArgumentException(message, e); } byteBuffer.rewind(); StringBuilder encodedString = new StringBuilder(); for (int i = 0; i < byteBuffer.limit(); i++) { String asHex = Integer.toHexString(byteBuffer.get() & 0xFF); encodedString.append("%").append(asHex.length() == 1 ? "0" : "").append(asHex); } return encodedString.toString(); } }
StringValidateEncodeUtils { public static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName) { if (!Character.isDefined(codePoint)) throw new IllegalArgumentException( String.format("Given code point [U+%1$04X - %1$d] not assigned to an abstract character.", codePoint)); if (Character.getType(codePoint) == Character.SURROGATE) throw new IllegalArgumentException(String .format("Given code point [U+%1$04X - %1$d] is an unencodable (by itself) surrogate character.", codePoint)); Charset charset = Charset.availableCharsets().get(charsetName); if (charset == null) throw new IllegalArgumentException(String.format("Unsupported charset [%s].", charsetName)); char[] chars = Character.toChars(codePoint); ByteBuffer byteBuffer = null; try { byteBuffer = charset.newEncoder().encode(CharBuffer.wrap(chars)); } catch (CharacterCodingException e) { String message = String.format("Given code point [U+%1$04X - %1$d] cannot be encode in given charset [%2$s].", codePoint, charsetName); throw new IllegalArgumentException(message, e); } byteBuffer.rewind(); StringBuilder encodedString = new StringBuilder(); for (int i = 0; i < byteBuffer.limit(); i++) { String asHex = Integer.toHexString(byteBuffer.get() & 0xFF); encodedString.append("%").append(asHex.length() == 1 ? "0" : "").append(asHex); } return encodedString.toString(); } private  StringValidateEncodeUtils(); }
StringValidateEncodeUtils { public static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName) { if (!Character.isDefined(codePoint)) throw new IllegalArgumentException( String.format("Given code point [U+%1$04X - %1$d] not assigned to an abstract character.", codePoint)); if (Character.getType(codePoint) == Character.SURROGATE) throw new IllegalArgumentException(String .format("Given code point [U+%1$04X - %1$d] is an unencodable (by itself) surrogate character.", codePoint)); Charset charset = Charset.availableCharsets().get(charsetName); if (charset == null) throw new IllegalArgumentException(String.format("Unsupported charset [%s].", charsetName)); char[] chars = Character.toChars(codePoint); ByteBuffer byteBuffer = null; try { byteBuffer = charset.newEncoder().encode(CharBuffer.wrap(chars)); } catch (CharacterCodingException e) { String message = String.format("Given code point [U+%1$04X - %1$d] cannot be encode in given charset [%2$s].", codePoint, charsetName); throw new IllegalArgumentException(message, e); } byteBuffer.rewind(); StringBuilder encodedString = new StringBuilder(); for (int i = 0; i < byteBuffer.limit(); i++) { String asHex = Integer.toHexString(byteBuffer.get() & 0xFF); encodedString.append("%").append(asHex.length() == 1 ? "0" : "").append(asHex); } return encodedString.toString(); } private  StringValidateEncodeUtils(); static boolean validSingleLineString(String singleLineString); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validPath(String path); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validFilePath(String path); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validUriString(String uri); static boolean validIdString(String id); static boolean containsAngleBracketCharacters(String string); static boolean containsAmpersandCharacters(String string); static boolean containsBackslashCharacters(String string); static boolean validDecimalNumber(String number); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validBooleanString(String boolString); static boolean validAlphanumericString(String alphNumString); static boolean validAlphanumericStringConstrainedSet(String alphNumString, String[] constrainedSet,
      boolean ignoreCase); @SuppressWarnings({"UnnecessaryContinue"}) static boolean descendOnlyFilePath(String path); static boolean validPercentHexOctetsString(String percentHexOctetsString); static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName); }
StringValidateEncodeUtils { public static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName) { if (!Character.isDefined(codePoint)) throw new IllegalArgumentException( String.format("Given code point [U+%1$04X - %1$d] not assigned to an abstract character.", codePoint)); if (Character.getType(codePoint) == Character.SURROGATE) throw new IllegalArgumentException(String .format("Given code point [U+%1$04X - %1$d] is an unencodable (by itself) surrogate character.", codePoint)); Charset charset = Charset.availableCharsets().get(charsetName); if (charset == null) throw new IllegalArgumentException(String.format("Unsupported charset [%s].", charsetName)); char[] chars = Character.toChars(codePoint); ByteBuffer byteBuffer = null; try { byteBuffer = charset.newEncoder().encode(CharBuffer.wrap(chars)); } catch (CharacterCodingException e) { String message = String.format("Given code point [U+%1$04X - %1$d] cannot be encode in given charset [%2$s].", codePoint, charsetName); throw new IllegalArgumentException(message, e); } byteBuffer.rewind(); StringBuilder encodedString = new StringBuilder(); for (int i = 0; i < byteBuffer.limit(); i++) { String asHex = Integer.toHexString(byteBuffer.get() & 0xFF); encodedString.append("%").append(asHex.length() == 1 ? "0" : "").append(asHex); } return encodedString.toString(); } private  StringValidateEncodeUtils(); static boolean validSingleLineString(String singleLineString); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validPath(String path); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validFilePath(String path); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validUriString(String uri); static boolean validIdString(String id); static boolean containsAngleBracketCharacters(String string); static boolean containsAmpersandCharacters(String string); static boolean containsBackslashCharacters(String string); static boolean validDecimalNumber(String number); @SuppressWarnings({"SimplifiableIfStatement"}) static boolean validBooleanString(String boolString); static boolean validAlphanumericString(String alphNumString); static boolean validAlphanumericStringConstrainedSet(String alphNumString, String[] constrainedSet,
      boolean ignoreCase); @SuppressWarnings({"UnnecessaryContinue"}) static boolean descendOnlyFilePath(String path); static boolean validPercentHexOctetsString(String percentHexOctetsString); static String unicodeCodePoint2PercentHexString(int codePoint, String charsetName); final static String CHARACTER_ENCODING_UTF_8; }