You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Nicholas Allen (JIRA)" <ji...@apache.org> on 2012/11/13 09:39:16 UTC

[jira] [Created] (VFS-443) Need an easy way to convert from a FileObject to a File

Nicholas Allen created VFS-443:
----------------------------------

             Summary: Need an easy way to convert from a FileObject to a File
                 Key: VFS-443
                 URL: https://issues.apache.org/jira/browse/VFS-443
             Project: Commons VFS
          Issue Type: Bug
    Affects Versions: 2.0
            Reporter: Nicholas Allen
             Fix For: 2.1


I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.

Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:

1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 

2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).

So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.

The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:

/**
    * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
    * returns null.
    */
   public File getLocalFile(final FileObject fileObject)
   {
      if (fileObject instanceof LocalFile)
      {
         final LocalFile localFile = (LocalFile)fileObject;

         return localFile.getLocalFile();
      }

      return null;
   }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Nicholas Allen updated VFS-443:
-------------------------------

    Description: 
I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.

Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:

1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 

2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).

So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.

The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:

{code:title=FileUtilities.java}
/**
    * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
    * returns null.
    */
   public File getLocalFile(final FileObject fileObject)
   {
      if (fileObject instanceof LocalFile)
      {
         final LocalFile localFile = (LocalFile)fileObject;

         return localFile.getLocalFile();
      }

      return null;
   }
{code}

  was:
I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.

Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:

1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 

2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).

So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.

The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:

/**
    * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
    * returns null.
    */
   public File getLocalFile(final FileObject fileObject)
   {
      if (fileObject instanceof LocalFile)
      {
         final LocalFile localFile = (LocalFile)fileObject;

         return localFile.getLocalFile();
      }

      return null;
   }

    
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> {code:title=FileUtilities.java}
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496550#comment-13496550 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

I added Junit tests to check that conversion works to some utility functions and they fail using the method that was suggested.

Here are my unit tests:

{code:title=FileUtilityTests.java|borderStyle=solid}

@Test
   public void testFileObjectConversion()
   {
      testFileObjectConversion(new File("hello.txt"));
      testFileObjectConversion(new File("hello there.txt"));
      testFileObjectConversion(new File("hello!?%#.txt"));
   }

   private void testFileObjectConversion(final File file)
   {
      final FileObject fileObject = FileUtilities.getFileObject(file);
      final File localFile = FileUtilities.getLocalFile(fileObject);

      Assert.assertTrue(localFile != null);
      Assert.assertEquals(file.getAbsoluteFile(), localFile);
   }

{code}

And here are the functions that are tested:

{code:title=FileUtilities.java|borderStyle=solid}

/** Converts a {@link File} to a {@link FileObject}. */
   public static FileObject getFileObject(final File file)
   {
      try
      {
         final File absoluteFile = file.getAbsoluteFile();
         final FileObject fileObject = VFS.getManager().toFileObject(absoluteFile);

         assert fileObject != null;
         assert absoluteFile.equals(getLocalFile(fileObject));

         return fileObject;
      }
      catch (final FileSystemException e)
      {
         throw new IllegalStateException(e);
      }
   }

   /**
    * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
    * returns null.
    */
   public static File getLocalFile(final FileObject fileObject)
   {
      if (fileObject instanceof LocalFile)
      {
         try
         {
            return new File(fileObject.getURL().getPath());
         }
         catch (final FileSystemException e)
         {
            throw new IllegalStateException(e);
         }
      }

      return null;
   }

{code}
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Resolved] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gary Gregory resolved VFS-443.
------------------------------

    Resolution: Fixed
      Assignee: Gary Gregory
    
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496505#comment-13496505 ] 

Gary Gregory commented on VFS-443:
----------------------------------

"That does not work if the url contains special characters (eg !,%, space etc) s"

It depends on the file provider, this works (in trunk):

{code:java}
    @Test
    public void testFileObjectToFile() throws FileSystemException {
        FileObject fo = VFS.getManager().resolveFile("file://C:\\Users\\ggregory\\Documents\\eclipse\\LDE\\Java6Tests\\src\\test\\resources\\test space.txt");
        File f = new File(fo.getURL().getPath());
        System.out.println(f + " " + f.exists());
    }

    @Test
    public void testResFileObjectToAbsoluteFile() throws FileSystemException {
        FileObject fo = VFS.getManager().resolveFile("res:test space.txt");
        File f = new File(fo.getURL().getPath());
        System.out.println(f + " " + f.exists());
    }
{code}

Can you provide an example of what you want to do?

Thank you,
Gary
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496989#comment-13496989 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

Thanks for making this public ;-) Is this now deployed as snapshot in maven repository yet? If so I can quickly update our maven dependency and check it works for you. The javadocs look good to me. I can't comment too much on the URL handling in VFS though...
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496556#comment-13496556 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

The test for a file with space works but with ?!# chars it doesn't.
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497351#comment-13497351 ] 

Joerg Schaible commented on VFS-443:
------------------------------------

1/ I am -1 to make getLocalFile() public. The internal File instance was always meant to be a lazy object that is not necessarily created.

2/ the comment lies for Unix ;-)

{noformat}
$ touch \?.txt
$ ls *.txt
?.txt
{noformat}

and of course can a file in Unix also have characters '\\', ':', '\'', or  ... (well, as long as it does not save it into FAT).

3/ For me it seems that VFS does not really separate between URL and URI. E.g. an URL cannot contain a space, while it is valid for an URI. Therefore, if something like FileObject.getURL() returns a URL, it should always be possible to create a URI from it - and this is not limited to file URLs only. Basically it is now at least possible to use the JDK for proper conversion, 1.4 was the last to fail. VFS is a lot older, but it looks like there is plenty of cruft left, that does not really work as expected either.

                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> {code:title=FileUtilities.java}
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496995#comment-13496995 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

Looking at the LocalFile.java class it looks like the implementation has bugs in it. Just making getLocalFile() public will not work because this just directly returns the field and in my debugger I can see that this is null at this point. It seems the file has to be attached first. Looking at the LocalFile.doAttach() method it seems that this just uses the decoded path in the name but on Windows this should be with "\" not "/" and also the C:\ part is missing. I can't debug it yet to check but that is what I would expect to happen.

{code:title=LocalFile.java}

/**
     * Returns the local file that this file object represents.
     */
    protected File getLocalFile()
    {
        return file;
    }

    /**
     * Attaches this file object to its file resource.
     */
    @Override
    protected void doAttach() throws Exception
    {
        if (file == null)
        {
            // Remove the "file:///"
            // LocalFileName localFileName = (LocalFileName) getName();
            String fileName = rootFile + getName().getPathDecoded();
            // fileName = UriParser.decode(fileName);
            file = new File(fileName);
        }
    }

{code}
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Comment Edited] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496487#comment-13496487 ] 

Nicholas Allen edited comment on VFS-443 at 11/13/12 7:58 PM:
--------------------------------------------------------------

I don't see why the only public methods have to be overrides. Why even expose the LocalFile class in that case? It can't do anything more than a FileObject can so as a client of the library I don't need to know about that class at all if I can access all it's functionality through its base class. It's a common design pattern that a sub-class can extend and add extra functionality so I don't really see why this should be a problem at all from a design point of view.
                
      was (Author: nickallen):
    I don't see why the only public methods have to be overrides. Why even expose the LocalFile class? It can't do anything more than a FileObject can so as a client of the library I don't need to know about that class at all if I can access all it's functionality through its base class. It's a common design pattern that a sub-class can extend and add extra functionality so I don't really see why this should be a problem at all from a design point of view.
                  
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496487#comment-13496487 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

I don't see why the only public methods have to be overrides. Why even expose the LocalFile class? It can't do anything more than a FileObject can so as a client of the library I don't need to know about that class at all if I can access all it's functionality through its base class. It's a common design pattern that a sub-class can extend and add extra functionality so I don't really see why this should be a problem at all from a design point of view.
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496185#comment-13496185 ] 

Gary Gregory commented on VFS-443:
----------------------------------

What about using:

{code:java}
File f = new File(fo.getURL().getPath());
{code}

Making getLocalFile() a public method is possible but against the current design. If you look at all the FileObject subclasses, the only public methods are @Override methods.
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497328#comment-13497328 ] 

Gary Gregory commented on VFS-443:
----------------------------------

@Joerge's https://issues.apache.org/jira/browse/VFS-443?focusedCommentId=13496963&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13496963

Well, this is quite a mess. 

Is {{LocalFileName.getURI()}} the only place at fault here?

{code:java}
    /**
     * Returns the absolute URI of the file.
     * @return The absolute URI of the file.
     */
    @Override
    public String getURI()
    {
        String uri = super.getURI();

        if (uri != null && uri.length() > 0)
        {
            try
            {
                // VFS-325: Handle URI special characters in filename
                // Decode the base uri and re-encode with URI special characters
                uri = UriParser.decode(uri);

                uri = UriParser.encode(uri, RESERVED_URI_CHARS);
            }
            catch (FileSystemException e)
            {
                // Default to base uri value
            }
        }

        return uri;
    }
{code}

Then we have:

{code:java}
    // URI Characters that are possible in local filenames, but must be escaped
    // for proper URI handling.
    //
    // How reserved URI chars were selected:
    //
    //  URIs can contain :, /, ?, #, @
    //      See http://download.oracle.com/javase/6/docs/api/java/net/URI.html
    //          http://tools.ietf.org/html/rfc3986#section-2.2
    //
    //  Since : and / occur before the path, only chars after path are escaped (i.e., # and ?)
    //  ? is a reserved filesystem character for Windows and Unix, so can't be part of a filename.
    //  Therefore only # is a reserved char in a URI as part of the path that can be in the filename.
    private static final char RESERVED_URI_CHARS[] = {'#'};
{code}

Adding {{' '}} (a space char) to the array causes the two tests above to pass. 

But this is a hack, and seems to go against the method as coded.

Any thoughts on how to best fix this?

                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> {code:title=FileUtilities.java}
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497403#comment-13497403 ] 

Gary Gregory commented on VFS-443:
----------------------------------

-1 it is:
{noformat}
commit -m "Revert to making getLocalFile() protected." C:/svn/org/apache/commons/trunks-proper/vfs/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
    Sending        C:/svn/org/apache/commons/trunks-proper/vfs/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
    Transmitting file data ...
    Committed revision 1409369.
{noformat}
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> {code:title=FileUtilities.java}
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Nicholas Allen (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496484#comment-13496484 ] 

Nicholas Allen commented on VFS-443:
------------------------------------

That does not work if the url contains special characters (eg !,%, space etc) so is not a suitable replacement.
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496815#comment-13496815 ] 

Gary Gregory commented on VFS-443:
----------------------------------

OK, you've convinced me, {{getLocalFile()}} is now public with a usage recommendation in the Javadoc.

Please provide feedback on the Javadoc if you would. Or anything else in VFS2.

Thank you!

{noformat}
commit -m "[VFS-443] [Local] Need an easy way to convert from a FileObject to a File." C:/svn/org/apache/commons/trunks-proper/vfs/src/changes/changes.xml C:/svn/org/apache/commons/trunks-proper/vfs/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
    Sending        C:/svn/org/apache/commons/trunks-proper/vfs/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
    Sending        C:/svn/org/apache/commons/trunks-proper/vfs/src/changes/changes.xml
    Transmitting file data ...
    Committed revision 1409075.
{noformat}
                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Reopened] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Joerg Schaible reopened VFS-443:
--------------------------------

    
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (VFS-443) Need an easy way to convert from a FileObject to a File

Posted by "Joerg Schaible (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13496963#comment-13496963 ] 

Joerg Schaible commented on VFS-443:
------------------------------------

Hi Greg,

actually I think this is not the proper action. Your code should be the obvious solution, but it fails, because it simply demonstrates a fundamental problem with VFS' URL handling:

{code}
public void testFileNameWithSpaces() throws VirtualFileSystemException, URISyntaxException, IOException
{
  final File file = new File("target", "a name.txt");
  final String fileURL = file.toURI().toURL().toExternalForm();
  assertEquals(file.getAbsoluteFile(), new File(file.toURI().getPath()));
  assertEquals(file.getAbsoluteFile(), new File(new URL(fileURL).toURI().getPath()));

  final FileSystemManager manager = VFS.getManager();
  final FileObject fo = manager.resolveFile(fileURL);
  assertEquals(file.getAbsoluteFile(), new File(new URL(fo.getURL().toExternalForm()).toURI().getPath()));
}

public void testFileNameWithCharacters() throws VirtualFileSystemException, URISyntaxException, IOException
{
  final File file = new File("target", "+# %&.txt");
  final String fileURL = file.toURI().toURL().toExternalForm();
  assertEquals(file.getAbsoluteFile(), new File(file.toURI().getPath()));
  assertEquals(file.getAbsoluteFile(), new File(new URL(fileURL).toURI().getPath()));

  try {
    new FileOutputStream(file).close();
    assertTrue(file.exists());

    final FileSystemManager manager = VFS.getManager();
    final FileObject fo = manager.resolveFile(fileURL);
    assertTrue(fo.exists());
    assertEquals(file.getAbsoluteFile(), new File(new URL(fo.getURL().toExternalForm()).toURI().getPath()));
  } finally {
    file.delete();
  }
}
{code}

Formally URLs are defined as subset of an URI in RFC 2396. However, java.net.URL has a long and sad story and the class does not ensure that the URL instance complies to this RFC. Even worse, the direct conversion between File and URL was never implemented properly and it will never be corrected, because too much code relies on the bad behavior (i.e. tries to fix the result afterwards). Therefore the JDK documents to do a proper conversion like in my unit test above using temporary URIs (see Javadoc for URL and URI).

However, VFS fails here badly. The first test above fails in the last line with a URISyntaxException, because the URL returned from FileObject is no longer compliant - although a compliant URL was provided to VFS. The last assert in the second test fails, because the URL is truncated; VFS does no proper URL encoding/decoding.

                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to convert from a FileObject to a java.io.File and those reasons make sense - however, I think that some things are being overlooked and there are still valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system and then as specs change it could become a requirement to work over http or inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be able to get back to a java.io.File in case I need to interface with same other library. I would like a method that converted to a File or null if not possible. This would allow me to take an alternate action (eg copy file to local temp file if it's not already a local file). There's no need to copy the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in LocalFile public. Once the user knows it's a LocalFile object it makes sense to call this method to obtain the java.io.File. So I could write a method like this:
> /**
>     * If the supplied {@link FileObject} represents a local file then this returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira