Hi there,

we are having problems here with java.io.File on Windows CE. Which handles files different again than other Windows and of course different than Unix. I could have hacked some more special casing in there. But I'd like to propose to pull some more methods into VMFile. See ChangeLog and attached patch. Basically this moves the impl of getAbsolutePath(), isAbsolute() and toURL() to VMFile.

Maybe we should get rid of all the special casing anyway and provide a default straightforward impl for Posixy systems, for the sake of efficiency and cleanness.

2006-08-15  Roman Kennke  <[EMAIL PROTECTED]>

        * java/io/File.java
        (getAbsolutePath): Fetch absolute path from
        VMFile.getAbsolutePath(). Moved actual impl to there.
        (isAbsolute): Let VMFile determine the absoluteness.
        (toURL): Let VMFile convert the filename.
        * vm/reference/java/io/VMFile.java
        (getAbsolutePath): New method.
        (isAbsolute): New method.
        (toURL): New method.


/Roman
Index: java/io/File.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/File.java,v
retrieving revision 1.65
diff -u -1 -2 -r1.65 File.java
--- java/io/File.java	29 Jun 2006 09:59:57 -0000	1.65
+++ java/io/File.java	15 Aug 2006 08:52:05 -0000
@@ -418,63 +418,26 @@
    * This method returns the path of this file as an absolute path name.
    * If the path name is already absolute, then it is returned.  Otherwise
    * the value returned is the current directory plus the separatory
    * string plus the path of the file.  The current directory is determined
    * from the <code>user.dir</code> system property.
    *
    * @return The absolute path of this file
    */
   public String getAbsolutePath()
   {
     if (isAbsolute())
       return path;
-    else if (separatorChar == '\\' 
-             && path.length() > 0 && path.charAt (0) == '\\')
-      {
-        // On Windows, even if the path starts with a '\\' it is not
-        // really absolute until we prefix the drive specifier from
-        // the current working directory to it.
-        return System.getProperty ("user.dir").substring (0, 2) + path;
-      }
-    else if (separatorChar == '\\' 
-             && path.length() > 1 && path.charAt (1) == ':'
-             && ((path.charAt (0) >= 'a' && path.charAt (0) <= 'z')
-                 || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')))
-      {
-        // On Windows, a process has a current working directory for
-        // each drive and a path like "G:foo\bar" would mean the 
-        // absolute path "G:\wombat\foo\bar" if "\wombat" is the 
-        // working directory on the G drive.
-        String drvDir = null;
-        try
-          {
-            drvDir = new File (path.substring (0, 2)).getCanonicalPath();
-          }
-        catch (IOException e)
-          {
-            drvDir = path.substring (0, 2) + "\\";
-          }
-        
-        // Note: this would return "C:\\." for the path "C:.", if "\"
-        // is the working folder on the C drive, but this is 
-        // consistent with what Sun's JRE 1.4.1.01 actually returns!
-        if (path.length() > 2)
-          return drvDir + '\\' + path.substring (2, path.length());
-        else
-          return drvDir;
-      }
-    else if (path.equals(""))
-      return System.getProperty ("user.dir");
     else
-      return System.getProperty ("user.dir") + separatorChar + path;
+      return VMFile.getAbsolutePath(path);
   }
 
   /**
    * This method returns a <code>File</code> object representing the
    * absolute path of this object.
    *
    * @return A <code>File</code> with the absolute path of the object.
    *
    * @since 1.2
    */
   public File getAbsoluteFile()
   {
@@ -648,33 +611,25 @@
 
   /**
    * This method returns true if this object represents an absolute file
    * path and false if it does not.  The definition of an absolute path varies
    * by system.  As an example, on GNU systems, a path is absolute if it starts
    * with a "/".
    *
    * @return <code>true</code> if this object represents an absolute 
    * file name, <code>false</code> otherwise.
    */
   public boolean isAbsolute()
   {
-    if (separatorChar == '\\')
-	return path.startsWith(dupSeparator) || 
-	    (path.length() > 2 && 
-	     ((path.charAt(0) >= 'a' && path.charAt(0) <= 'z') ||
-	      (path.charAt(0) >= 'A' && path.charAt(0) <= 'Z')) &&
-	     path.charAt(1) == ':' &&
-	     path.charAt(2) == '\\');
-    else
-	return path.startsWith(separator);
+    return VMFile.isAbsolute(path);
   }
 
   /**
    * This method tests whether or not the file represented by this object
    * is a directory.  In order for this method to return <code>true</code>,
    * the file represented by this object must exist and be a directory.
    * 
    * @return <code>true</code> if this file is a directory, <code>false</code>
    * otherwise
    *
    * @exception SecurityException If reading of the file is not permitted
    */
@@ -989,32 +944,25 @@
   /**
    * This method returns a <code>URL</code> with the <code>file:</code>
    * protocol that represents this file.  The exact form of this URL is
    * system dependent.
    *
    * @return A <code>URL</code> for this object.
    *
    * @exception MalformedURLException If the URL cannot be created 
    * successfully.
    */
   public URL toURL() throws MalformedURLException
   {
-    // On Win32, Sun's JDK returns URLs of the form "file:/c:/foo/bar.txt",
-    // while on UNIX, it returns URLs of the form "file:/foo/bar.txt". 
-    if (separatorChar == '\\')
-      return new URL ("file:/" + getAbsolutePath().replace ('\\', '/')
-		      + (isDirectory() ? "/" : ""));
-    else
-      return new URL ("file:" + getAbsolutePath()
-		      + (isDirectory() ? "/" : ""));
+    return VMFile.toURL(path);
   }
 
 
   /**
    * This method creates a directory for the path represented by this object.
    *
    * @return <code>true</code> if the directory was created, 
    * <code>false</code> otherwise
    *
    * @exception SecurityException If write access is not allowed to this file
    */
   public boolean mkdir()
Index: vm/reference/java/io/VMFile.java
===================================================================
RCS file: /cvsroot/classpath/classpath/vm/reference/java/io/VMFile.java,v
retrieving revision 1.8
diff -u -1 -2 -r1.8 VMFile.java
--- vm/reference/java/io/VMFile.java	7 Jun 2006 15:09:40 -0000	1.8
+++ vm/reference/java/io/VMFile.java	15 Aug 2006 08:52:05 -0000
@@ -29,24 +29,27 @@
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package java.io;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+
 import gnu.classpath.Configuration;
 import gnu.java.io.PlatformHelper;
 
 
 /**
  * @author Michael Koch ([EMAIL PROTECTED])
  */
 final class VMFile
 {
   // FIXME: We support only case sensitive filesystems currently.
   static final boolean IS_CASE_SENSITIVE = true;
   static final boolean IS_DOS_8_3 = false;
@@ -200,24 +203,126 @@
   {
 	int pos = PlatformHelper.lastIndexOfSeparator(path);
 	if (pos == -1)
 	  return path;
 	
 	if (PlatformHelper.endWithSeparator(path))
 	  return "";
 	
 	return path.substring(pos + File.separator.length());
   }
 
   /**
+   * Returns the path as an absolute path name. The value returned is the
+   * current directory plus the separatory string plus the path of the file.
+   * The current directory is determined from the <code>user.dir</code> system
+   * property.
+   *
+   * @param path the path to convert to absolute path
+   *
+   * @return the absolute path that corresponds to <code>path</code>
+   */
+  static String getAbsolutePath(String path)
+  {
+    if (File.separatorChar == '\\' 
+      && path.length() > 0 && path.charAt (0) == '\\')
+      {
+        // On Windows, even if the path starts with a '\\' it is not
+        // really absolute until we prefix the drive specifier from
+        // the current working directory to it.
+        return System.getProperty ("user.dir").substring (0, 2) + path;
+      }
+    else if (File.separatorChar == '\\'
+             && path.length() > 1 && path.charAt (1) == ':'
+             && ((path.charAt (0) >= 'a' && path.charAt (0) <= 'z')
+             || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')))
+      {
+        // On Windows, a process has a current working directory for
+        // each drive and a path like "G:foo\bar" would mean the 
+        // absolute path "G:\wombat\foo\bar" if "\wombat" is the 
+        // working directory on the G drive.
+        String drvDir = null;
+        try
+          {
+            drvDir = new File (path.substring (0, 2)).getCanonicalPath();
+          }
+        catch (IOException e)
+          {
+            drvDir = path.substring (0, 2) + "\\";
+          }
+
+        // Note: this would return "C:\\." for the path "C:.", if "\"
+        // is the working folder on the C drive, but this is 
+        // consistent with what Sun's JRE 1.4.1.01 actually returns!
+        if (path.length() > 2)
+          return drvDir + '\\' + path.substring (2, path.length());
+        else
+          return drvDir;
+      }
+    else if (path.equals(""))
+      return System.getProperty ("user.dir");
+    else
+      return System.getProperty ("user.dir") + File.separatorChar + path;
+  }
+
+  /**
+   * This method returns true if the path represents an absolute file
+   * path and false if it does not.  The definition of an absolute path varies
+   * by system.  As an example, on GNU systems, a path is absolute if it starts
+   * with a "/".
+   *
+   * @param path the path to check
+   *
+   * @return <code>true</code> if path represents an absolute file name,
+   *         <code>false</code> otherwise.
+   */
+  static boolean isAbsolute(String path)
+  {
+    if (File.separatorChar == '\\')
+        return path.startsWith(File.separator + File.separator)
+               || (path.length() > 2
+                   && ((path.charAt(0) >= 'a' && path.charAt(0) <= 'z')
+                       || (path.charAt(0) >= 'A' && path.charAt(0) <= 'Z'))
+                       && path.charAt(1) == ':'
+                       && path.charAt(2) == '\\');
+    else
+      return path.startsWith(File.separator);
+  }
+
+  /**
+   * Returns a <code>URL</code> with the <code>file:</code>
+   * protocol that represents this file.  The exact form of this URL is
+   * system dependent.
+   *
+   * @param path the path to convert to URL
+   *
+   * @return a <code>URL</code> for this object.
+   *
+   * @throws MalformedURLException if the URL cannot be created
+   *         successfully.
+   */
+  static URL toURL(String path)
+    throws MalformedURLException
+  {
+    // On Win32, Sun's JDK returns URLs of the form "file:/c:/foo/bar.txt",
+    // while on UNIX, it returns URLs of the form "file:/foo/bar.txt". 
+    if (File.separatorChar == '\\')
+      return new URL ("file:/" + getAbsolutePath(path).replace ('\\', '/')
+                      + (isDirectory(path) ? "/" : ""));
+    else
+      return new URL ("file:" + getAbsolutePath(path)
+                      + (isDirectory(path) ? "/" : ""));
+  }
+
+   /**
    * This method returns a canonical representation of the pathname of
    * this file.  The actual form of the canonical representation is
    * system-dependent.  On the GNU system, conversion to canonical
    * form involves the removal of redundant separators, references to
    * "." and "..", and symbolic links.
    * <p>
    * Note that this method, unlike the other methods which return path
    * names, can throw an IOException.  This is because native method 
    * might be required in order to resolve the canonical path
    *
    * @exception IOException If an error occurs
    */

Reply via email to