Hi,

To make opening files and jars from URLs with funny characters possible
Andrew wrote the following for libgcj. This was needed to load some
files from jonas.

2005-11-17  Andrew Haley  <[EMAIL PROTECTED]>

   * gnu/java/net/protocol/file/Connection.java (unquote): New
   method.
   (connect): Unquote filename.
   * gnu/java/net/protocol/jar/Connection.java (get): Likewise.

   * java/net/URL.java (URL): If the file part of a spec is absolute,
   ignore the file part of its context.

The jar Connection patch is a bit different from what went into libgcj
since we haven't merged these classes yet. file Connection is fully
merged now.

Committed,

Mark
Index: gnu/java/net/protocol/file/Connection.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/net/protocol/file/Connection.java,v
retrieving revision 1.17
diff -u -r1.17 Connection.java
--- gnu/java/net/protocol/file/Connection.java	2 Jul 2005 20:32:13 -0000	1.17
+++ gnu/java/net/protocol/file/Connection.java	17 Nov 2005 10:48:54 -0000
@@ -59,6 +59,7 @@
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
+import java.net.MalformedURLException;
 
 /**
  * This subclass of java.net.URLConnection models a URLConnection via
@@ -125,6 +126,54 @@
   }
   
   /**
+   * Unquote "%" + hex quotes characters
+   *
+   * @param str The string to unquote or null.
+   *
+   * @return The unquoted string or null if str was null.
+   *
+   * @exception MalformedURLException If the given string contains invalid
+   * escape sequences.
+   *
+   * Sadly the same as URI.unquote, but there's nothing we can do to
+   * make it accessible.
+   *
+   */
+  public static String unquote(String str) throws MalformedURLException
+  {
+    if (str == null)
+      return null;
+    byte[] buf = new byte[str.length()];
+    int pos = 0;
+    for (int i = 0; i < str.length(); i++)
+      {
+	char c = str.charAt(i);
+	if (c > 127)
+	  throw new MalformedURLException(str + " : Invalid character");
+	if (c == '%')
+	  {
+	    if (i + 2 >= str.length())
+	      throw new MalformedURLException(str + " : Invalid quoted character");
+	    int hi = Character.digit(str.charAt(++i), 16);
+	    int lo = Character.digit(str.charAt(++i), 16);
+	    if (lo < 0 || hi < 0)
+	      throw new MalformedURLException(str + " : Invalid quoted character");
+	    buf[pos++] = (byte) (hi * 16 + lo);
+	  }
+	else
+	  buf[pos++] = (byte) c;
+      }
+    try
+      {
+	return new String(buf, 0, pos, "utf-8");
+      }
+    catch (java.io.UnsupportedEncodingException x2)
+      {
+	throw (Error) new InternalError().initCause(x2);
+      }
+  }
+
+  /**
    * "Connects" to the file by opening it.
    */
   public void connect() throws IOException
@@ -134,7 +183,7 @@
       return;
     
     // If not connected, then file needs to be openned.
-    file = new File (getURL().getFile());
+    file = new File (unquote(getURL().getFile()));
 
     if (! file.isDirectory())
       {
Index: gnu/java/net/protocol/jar/Connection.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/net/protocol/jar/Connection.java,v
retrieving revision 1.10
diff -u -r1.10 Connection.java
--- gnu/java/net/protocol/jar/Connection.java	20 Sep 2005 21:01:44 -0000	1.10
+++ gnu/java/net/protocol/jar/Connection.java	17 Nov 2005 10:48:54 -0000
@@ -82,7 +82,9 @@
 
       if ("file".equals (url.getProtocol()))
 	{
-	  File f = new File (url.getFile());
+	  String fn = url.getFile();
+	  fn = gnu.java.net.protocol.file.Connection.unquote(fn);
+	  File f = new File (fn);
 	  jf = new JarFile (f, true, ZipFile.OPEN_READ);
 	}
       else
Index: java/net/URL.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/URL.java,v
retrieving revision 1.48
diff -u -r1.48 URL.java
--- java/net/URL.java	2 Jul 2005 20:32:39 -0000	1.48
+++ java/net/URL.java	17 Nov 2005 10:48:54 -0000
@@ -408,10 +408,7 @@
 	    // The 1.2 doc specifically says these are copied to the new URL.
 	    host = context.host;
 	    port = context.port;
-	    file = context.file;
             userInfo = context.userInfo;
-	    if (file == null || file.length() == 0)
-	      file = "/";
 	    authority = context.authority;
 	  }
       }
@@ -423,10 +420,13 @@
 	protocol = context.protocol;
 	host = context.host;
 	port = context.port;
-	file = context.file;
         userInfo = context.userInfo;
-	if (file == null || file.length() == 0)
-	  file = "/";
+	if (spec.indexOf(":/", 1) < 0)
+	  {
+	    file = context.file;
+	    if (file == null || file.length() == 0)
+	      file = "/";
+	  }
 	authority = context.authority;
       }
     else // Protocol NOT specified in spec. and no context available.

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to