Author: toad
Date: 2007-11-13 20:12:59 +0000 (Tue, 13 Nov 2007)
New Revision: 15776

Modified:
   trunk/freenet/src/freenet/support/URLDecoder.java
Log:
Revert 14684. Multi-byte UTF-8 sequences are not working, because we assume one 
%<code> = one character. Will re-investigate original bug.

Modified: trunk/freenet/src/freenet/support/URLDecoder.java
===================================================================
--- trunk/freenet/src/freenet/support/URLDecoder.java   2007-11-13 13:19:16 UTC 
(rev 15775)
+++ trunk/freenet/src/freenet/support/URLDecoder.java   2007-11-13 20:12:59 UTC 
(rev 15776)
@@ -3,8 +3,9 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package freenet.support;

+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;

 /**
  * Decode encoded URLs (or parts of URLs). @see URLEncoder.
@@ -40,13 +41,13 @@
                if (s.length() == 0)
                        return "";
                int len = s.length();
-               StringWriter decoded = new StringWriter();
+               ByteArrayOutputStream decodedBytes = new 
ByteArrayOutputStream();
                boolean hasDecodedSomething = false;

                for (int i = 0; i < len; i++) {
                        char c = s.charAt(i);
                        if (Character.isLetterOrDigit(c))
-                               decoded.write(c);
+                               decodedBytes.write(c);
                        else if (c == '%') {
                                if (i >= len - 2) {
                                        throw new URLEncodedFormatException(s);
@@ -61,25 +62,34 @@
                                        long read = Fields.hexToLong(hexval);
                                        if (read == 0)
                                                throw new 
URLEncodedFormatException("Can't encode" + " 00");
-                                       decoded.write((int) read);
+                                       decodedBytes.write((int) read);
                                        hasDecodedSomething = true;
                                } catch (NumberFormatException nfe) {
                                        // Not encoded?
                                        if(tolerant && !hasDecodedSomething) {
-                                               decoded.write('%');
-                                               decoded.write(hexval);
-                                               continue;
+                                               try {
+                                                       byte[] buf = 
('%'+hexval).getBytes("UTF-8");
+                                                       decodedBytes.write(buf, 
0, buf.length);
+                                                       continue;
+                                               } catch 
(UnsupportedEncodingException e) {
+                                                       throw new Error(e);
+                                               }
                                        }

                                        throw new 
URLEncodedFormatException("Not a two character hex % escape: "+hexval+" in "+s);
                                }
                        } else {
-                               decoded.write(c);
+                               try {
+                                       byte[] encoded = 
(""+c).getBytes("UTF-8");
+                                       decodedBytes.write(encoded, 0, 
encoded.length);
+                               } catch (UnsupportedEncodingException e) {
+                                       throw new Error(e);
+                               }
                        }
                }
                try {
-                       decoded.close();
-                       return decoded.toString();
+                       decodedBytes.close();
+                       return new String(decodedBytes.toByteArray(), "utf-8");
                } catch (IOException ioe1) {
                        /* if this throws something's wrong */
                }


Reply via email to