String str = s.replace('+', ' ');   int i; int start = 0; byte[] bytes = null; int length = str.length(); StringBuffer result = new StringBuffer(length); while ((i = str.indexOf('%', start)) >= 0)
try
  public static String decode(String s, String encoding)    throws UnsupportedEncodingException  {    // First convert all '+' characters to spaces.    String str = s.replace('+', ' ');        // Then go through the whole string looking for byte encoded characters    int i;    int start = 0;    byte[] bytes = null;    int length = str.length();    StringBuffer result = new StringBuffer(length);    while ((i = str.indexOf('%', start)) >= 0)      {	// Add all non-encoded characters to the result buffer	result.append(str.substring(start, i));	start = i;	// Get all consecutive encoded bytes	while ((i + 2 < length) && (str.charAt(i) == '%'))	  i += 3;	// Decode all these bytes	if ((bytes == null) || (bytes.length < ((i - start) / 3)))	  bytes = new byte[((i - start) / 3)];	int index = 0;	try	  {	    while (start < i)	      {		String sub = str.substring(start + 1, start + 3);		bytes[index] = (byte) Integer.parseInt(sub, 16);		index++;		start += 3;	      }	  }	catch (NumberFormatException nfe)	  {	    // One of the hex encoded strings was bad	  }	// Add the bytes as characters according to the given encoding	result.append(new String(bytes, 0, index, encoding));	// Make sure we skip to just after a % sign	// There might not have been enough encoded characters after the %	// or the hex chars were not actually hex chars (NumberFormatException)	if (start < length && s.charAt(start) == '%')	  {	    result.append('%');	    start++;	  }      }    // Add any characters left    if (start < str.length())      result.append(str.substring(start));    return result.toString();  }