@Test(dataProvider = "failures") public void decode_failures(String input, String expectedMessage) { try { encoder.decode(input); Assert.fail("This code should not be reachable."); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), expectedMessage); } }
public String decode(String input) { assert input != null; if (input.equals(ENCODED_NULL)) return null; if (input.equals(ENCODED_BLANK)) return ""; boolean dirty = false; int length = input.length(); StringBuilder output = new StringBuilder(length * 2); for (int i = 0; i < length; i++) { char ch = input.charAt(i); if (ch == '$') { dirty = true; if (i + 1 < length && input.charAt(i + 1) == '$') { output.append('$'); i++; dirty = true; continue; } if (i + 4 < length) { String hex = input.substring(i + 1, i + 5); try { int unicode = Integer.parseInt(hex, 16); output.append((char) unicode); i += 4; dirty = true; continue; } catch (NumberFormatException ex) { } } throw new IllegalArgumentException( String.format( "Input string '%s' is not valid; the '$' character at position %d should be followed by another '$' or a four digit hex number (a unicode value).", input, i + 1)); } if (!safe.get((int) ch)) { throw new IllegalArgumentException( String.format("Input string '%s' is not valid; the character '%s' at position %d is not valid.", input, ch, i + 1)); } output.append(ch); } return dirty ? output.toString() : input; }
URLEncoderImpl implements URLEncoder { public String decode(String input) { assert input != null; if (input.equals(ENCODED_NULL)) return null; if (input.equals(ENCODED_BLANK)) return ""; boolean dirty = false; int length = input.length(); StringBuilder output = new StringBuilder(length * 2); for (int i = 0; i < length; i++) { char ch = input.charAt(i); if (ch == '$') { dirty = true; if (i + 1 < length && input.charAt(i + 1) == '$') { output.append('$'); i++; dirty = true; continue; } if (i + 4 < length) { String hex = input.substring(i + 1, i + 5); try { int unicode = Integer.parseInt(hex, 16); output.append((char) unicode); i += 4; dirty = true; continue; } catch (NumberFormatException ex) { } } throw new IllegalArgumentException( String.format( "Input string '%s' is not valid; the '$' character at position %d should be followed by another '$' or a four digit hex number (a unicode value).", input, i + 1)); } if (!safe.get((int) ch)) { throw new IllegalArgumentException( String.format("Input string '%s' is not valid; the character '%s' at position %d is not valid.", input, ch, i + 1)); } output.append(ch); } return dirty ? output.toString() : input; } }
URLEncoderImpl implements URLEncoder { public String decode(String input) { assert input != null; if (input.equals(ENCODED_NULL)) return null; if (input.equals(ENCODED_BLANK)) return ""; boolean dirty = false; int length = input.length(); StringBuilder output = new StringBuilder(length * 2); for (int i = 0; i < length; i++) { char ch = input.charAt(i); if (ch == '$') { dirty = true; if (i + 1 < length && input.charAt(i + 1) == '$') { output.append('$'); i++; dirty = true; continue; } if (i + 4 < length) { String hex = input.substring(i + 1, i + 5); try { int unicode = Integer.parseInt(hex, 16); output.append((char) unicode); i += 4; dirty = true; continue; } catch (NumberFormatException ex) { } } throw new IllegalArgumentException( String.format( "Input string '%s' is not valid; the '$' character at position %d should be followed by another '$' or a four digit hex number (a unicode value).", input, i + 1)); } if (!safe.get((int) ch)) { throw new IllegalArgumentException( String.format("Input string '%s' is not valid; the character '%s' at position %d is not valid.", input, ch, i + 1)); } output.append(ch); } return dirty ? output.toString() : input; }  }
URLEncoderImpl implements URLEncoder { public String decode(String input) { assert input != null; if (input.equals(ENCODED_NULL)) return null; if (input.equals(ENCODED_BLANK)) return ""; boolean dirty = false; int length = input.length(); StringBuilder output = new StringBuilder(length * 2); for (int i = 0; i < length; i++) { char ch = input.charAt(i); if (ch == '$') { dirty = true; if (i + 1 < length && input.charAt(i + 1) == '$') { output.append('$'); i++; dirty = true; continue; } if (i + 4 < length) { String hex = input.substring(i + 1, i + 5); try { int unicode = Integer.parseInt(hex, 16); output.append((char) unicode); i += 4; dirty = true; continue; } catch (NumberFormatException ex) { } } throw new IllegalArgumentException( String.format( "Input string '%s' is not valid; the '$' character at position %d should be followed by another '$' or a four digit hex number (a unicode value).", input, i + 1)); } if (!safe.get((int) ch)) { throw new IllegalArgumentException( String.format("Input string '%s' is not valid; the character '%s' at position %d is not valid.", input, ch, i + 1)); } output.append(ch); } return dirty ? output.toString() : input; }  String encode(String input); String decode(String input); }
URLEncoderImpl implements URLEncoder { public String decode(String input) { assert input != null; if (input.equals(ENCODED_NULL)) return null; if (input.equals(ENCODED_BLANK)) return ""; boolean dirty = false; int length = input.length(); StringBuilder output = new StringBuilder(length * 2); for (int i = 0; i < length; i++) { char ch = input.charAt(i); if (ch == '$') { dirty = true; if (i + 1 < length && input.charAt(i + 1) == '$') { output.append('$'); i++; dirty = true; continue; } if (i + 4 < length) { String hex = input.substring(i + 1, i + 5); try { int unicode = Integer.parseInt(hex, 16); output.append((char) unicode); i += 4; dirty = true; continue; } catch (NumberFormatException ex) { } } throw new IllegalArgumentException( String.format( "Input string '%s' is not valid; the '$' character at position %d should be followed by another '$' or a four digit hex number (a unicode value).", input, i + 1)); } if (!safe.get((int) ch)) { throw new IllegalArgumentException( String.format("Input string '%s' is not valid; the character '%s' at position %d is not valid.", input, ch, i + 1)); } output.append(ch); } return dirty ? output.toString() : input; }  String encode(String input); String decode(String input);  }