Index: luni/src/main/java/java/net/URLDecoder.java =================================================================== --- luni/src/main/java/java/net/URLDecoder.java (revision 532715) +++ luni/src/main/java/java/net/URLDecoder.java (working copy) @@ -19,9 +19,13 @@ import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; import org.apache.harmony.luni.util.Msg; -import org.apache.harmony.luni.util.Util; /** * This class is used to decode a string which is encoded in the @@ -29,6 +33,7 @@ */ public class URLDecoder { + static Charset defaultCharset; /** * Decodes the string argument which is assumed to be encoded in the * x-www-form-urlencoded MIME content type. @@ -47,7 +52,21 @@ */ @Deprecated public static String decode(String s) { - return Util.decode(s, true); + + if (defaultCharset == null){ + try { + defaultCharset = Charset.forName(System.getProperty("file.encoding")); + } catch (IllegalCharsetNameException e) { + // Ignored + } catch (UnsupportedCharsetException e) { + // Ignored + } + + if (defaultCharset == null) { + defaultCharset = Charset.forName("ISO-8859-1"); //$NON-NLS-1$ + } + } + return decode(s, defaultCharset); } /** @@ -81,14 +100,42 @@ .getString("K00a5", "enc")); //$NON-NLS-1$ //$NON-NLS-2$ } - StringBuffer result = new StringBuffer(s.length()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); + if(s.indexOf('%') == -1){ + if(s.indexOf('+') == -1) return s; + char str[] = s.toCharArray(); + for(int i = 0; i < str.length; i++){ + if(str[i] == '+') str[i] = ' '; + } + return new String(str); + } + + Charset charset = null; + try { + charset = Charset.forName(enc); + } catch (IllegalCharsetNameException e) { + throw (UnsupportedEncodingException) (new UnsupportedEncodingException( + enc).initCause(e)); + } catch (UnsupportedCharsetException e) { + throw (UnsupportedEncodingException) (new UnsupportedEncodingException( + enc).initCause(e)); + } + + return decode(s, charset); + } + + private static String decode(String s, Charset charset){ + + char str_buf[] = new char[s.length()] ; + byte buf[] = new byte[s.length() / 3]; + int buf_len = 0; + for (int i = 0; i < s.length();) { char c = s.charAt(i); if (c == '+') { - result.append(' '); + str_buf[buf_len] = ' '; } else if (c == '%') { - out.reset(); + + int len = 0; do { if (i + 2 >= s.length()) { throw new IllegalArgumentException(Msg.getString( @@ -101,16 +148,21 @@ "K01ff", //$NON-NLS-1$ s.substring(i, i + 3), String.valueOf(i))); } - out.write((byte) ((d1 << 4) + d2)); + buf[len++] = (byte) ((d1 << 4) + d2); i += 3; } while (i < s.length() && s.charAt(i) == '%'); - result.append(out.toString(enc)); + CharBuffer cb = charset.decode(ByteBuffer.wrap(buf, 0, len)); + len = cb.length(); + System.arraycopy(cb.array(), 0, str_buf, buf_len, len); + buf_len += len; continue; + } else { - result.append(c); + str_buf[buf_len] = c; } i++; + buf_len++; } - return result.toString(); + return new String(str_buf, 0, buf_len); } }