Re: truly abstract (platform independent) pathnames

Discussion in 'Java' started by Harald Hein, Aug 14, 2003.

  1. Harald Hein

    Harald Hein Guest

    "Mohun Biswas" wrote:

    > Does anyone know of a class, built on File or otherwise, which
    > accounts for these issues?


    Hmmm, maybe if you use URL instead of File, with "file:" URLs?
     
    Harald Hein, Aug 14, 2003
    #1
    1. Advertising

  2. Harald Hein

    Mohun Biswas Guest

    Harald Hein wrote:
    > "Mohun Biswas" wrote:
    >
    >
    >>Does anyone know of a class, built on File or otherwise, which
    >>accounts for these issues?

    >
    >
    > Hmmm, maybe if you use URL instead of File, with "file:" URLs?


    As it turns out, file URLs may be workable as a solution to the
    isAbsolute() problem I used for illustration but can't address the
    entire problem. For instance, Windows places more restrictions on
    filenames than does Unix; "foo::bar" is perfectly legal on Unix but
    causes File to throw an exception on Windows.

    Maybe I can explain the situation better. My tool does file uploads via
    http (multipart-form-post via Jakarta FileUpload to be precise). This is
    a well-trodden path and I have it working well. In most uses, however,
    the server side is only interested in the file's contents. What makes my
    application a little different is that I need to store the pathname of
    the file uploaded from and later do some analysis based on that pathname.

    I've been subclassing File to handle this and it's worked well till I
    ran into these cross-platform issues. All I really need from File is a
    few convenience methods like getName()/getParent() (basename/dirname
    equivalents) and isAbsolute(). If only File had a switch such that you
    could tell it "treat the pathname I'm about to give you as a Windows
    path, regardless of what OS the JVM is currently executing on" I'd be
    all set. But File is locked to the current OS. Does anyone know a class
    with the above methods which allows the OS rules to be set programmatically?

    MB
     
    Mohun Biswas, Aug 15, 2003
    #2
    1. Advertising

  3. "Mohun Biswas" <> wrote in message
    news:sw8%a.120532$...
    > Harald Hein wrote:
    > > "Mohun Biswas" wrote:

    ....
    > >>Does anyone know of a class, built on File or otherwise, which
    > >>accounts for these issues?

    > >
    > >
    > > Hmmm, maybe if you use URL instead of File, with "file:" URLs?

    >
    > As it turns out, file URLs may be workable as a solution to the
    > isAbsolute() problem I used for illustration but can't address the
    > entire problem. For instance, Windows places more restrictions on
    > filenames than does Unix; "foo::bar" is perfectly legal on Unix but
    > causes File to throw an exception on Windows.


    I hit a hitch presuming URL's could handle all
    Windows filenames. Take the file name.
    'Family Group #1.jpg'

    An URL will take the '#' to mean that it
    represents a jar archive, and 'points to'
    the file '1.jpg' within that archive. :-(

    --
    Andrew Thompson
    http://www.lensescapes.com/
    http://www.physci.org/
    http://www.1point1c.org/
     
    Andrew Thompson, Aug 16, 2003
    #3
  4. Harald Hein

    Roedy Green Guest

    On Sat, 16 Aug 2003 03:13:51 GMT, "Andrew Thompson"
    <> wrote or quoted :

    >An URL will take the '#' to mean that it
    >represents a jar archive, and 'points to'
    >the file '1.jpg' within that archive.


    ! has the same effect. Best to stick to a VERY limited set of chars if
    you are making them up. It will save you grief if you port later.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 16, 2003
    #4
  5. "Roedy Green" <> wrote in message
    news:...
    > On Sat, 16 Aug 2003 03:13:51 GMT, "Andrew Thompson"
    > <> wrote or quoted :
    >
    > >An URL will take the '#' to mean that it
    > >represents a jar archive, and 'points to'
    > >the file '1.jpg' within that archive.

    >
    > ! has the same effect. Best to stick to a VERY limited set of chars if
    > you are making them up. It will save you grief if you port later.


    I agree Roedy, but am not sure I get your point.

    The situation where the '#' did not work was with
    a filename specified by the _user_ of the program.
    [ An image slideshow utility ]

    The program had to either deal with it, or fail..
    (In this case, my program fails. I will have to
    come up with a fix)

    --
    Andrew Thompson
    http://www.lensescapes.com/
    http://www.physci.org/
    http://www.1point1c.org/
     
    Andrew Thompson, Aug 16, 2003
    #5
  6. Mohun Biswas wrote on 08/15/2003 07:12 PM:
    > Harald Hein wrote:
    >
    >> "Mohun Biswas" wrote:
    >>
    >>
    >>> Does anyone know of a class, built on File or otherwise, which
    >>> accounts for these issues?

    >>
    >>
    >>
    >> Hmmm, maybe if you use URL instead of File, with "file:" URLs?

    >
    >
    > As it turns out, file URLs may be workable as a solution to the
    > isAbsolute() problem I used for illustration but can't address the
    > entire problem. For instance, Windows places more restrictions on
    > filenames than does Unix; "foo::bar" is perfectly legal on Unix but
    > causes File to throw an exception on Windows.
    >
    > Maybe I can explain the situation better. My tool does file uploads via
    > http (multipart-form-post via Jakarta FileUpload to be precise). This is
    > a well-trodden path and I have it working well. In most uses, however,
    > the server side is only interested in the file's contents. What makes my
    > application a little different is that I need to store the pathname of
    > the file uploaded from and later do some analysis based on that pathname.
    >
    > I've been subclassing File to handle this and it's worked well till I
    > ran into these cross-platform issues. All I really need from File is a
    > few convenience methods like getName()/getParent() (basename/dirname
    > equivalents) and isAbsolute(). If only File had a switch such that you
    > could tell it "treat the pathname I'm about to give you as a Windows
    > path, regardless of what OS the JVM is currently executing on" I'd be
    > all set. But File is locked to the current OS. Does anyone know a class
    > with the above methods which allows the OS rules to be set
    > programmatically?
    >
    > MB
    >


    I think you should take a look at Jakarta Apache Commons's VFS library
    at http://jakarta.apache.org/commons/sandbox/vfs/index.html

    In short the description is:
    "Commons VFS provides a single API for accessing various different file
    systems. It presents a uniform view of the files from various different
    sources, such as the files on local disk, on an HTTP server, or inside a
    Zip archive
    ..- A single consistent API for accessing files of different types.
    ..- Support for numerous file system types.
    ..- More...
    "

    Eduardo
     
    Eduardo Francos, Aug 16, 2003
    #6
  7. Harald Hein

    Roedy Green Guest

    On Sat, 16 Aug 2003 06:23:04 GMT, "Andrew Thompson"
    <> wrote or quoted :

    >I agree Roedy, but am not sure I get your point.


    If you are composing internal filenames, stick to simple names. If you
    are thinking of portability, filter suggested names for new files to
    warn against weird punctuation use which may fail on other platforms.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 16, 2003
    #7
  8. "Roedy Green" <> wrote in message
    news:eek:...
    > On Sat, 16 Aug 2003 06:23:04 GMT, "Andrew Thompson"
    > <> wrote or quoted :
    >
    > >I agree Roedy, but am not sure I get your point.

    >
    > If you are composing internal filenames, stick to simple names...


    Thanks for clarifying.

    > ..If you
    > are thinking of portability, filter suggested names for new files to
    > warn against weird punctuation use which may fail on other platforms.


    Good idea. I was thinking of trying a fall-back
    method to attempt to load the image as a file,
    but yours is a simpler more generic solution.

    --
    Andrew Thompson
    http://www.lensescapes.com/
    http://www.physci.org/
    http://www.1point1c.org/
     
    Andrew Thompson, Aug 16, 2003
    #8
  9. Harald Hein

    Chris Smith Guest

    Andrew Thompson wrote:
    > I hit a hitch presuming URL's could handle all
    > Windows filenames. Take the file name.
    > 'Family Group #1.jpg'
    >
    > An URL will take the '#' to mean that it
    > represents a jar archive, and 'points to'
    > the file '1.jpg' within that archive. :-(


    That would only be the case if you didn't properly encode the URL.
    Unfortunately, File.toURL has a bug that prevents it from doing that
    encoding (and Sun is aware of the bug, but apparently decided to add a
    new method that works correctly, rather than fixing the bug in the old
    method.) Use File.toURI to convert that to a URL, and all is well:

    import java.io.File;

    public class Test
    {
    public static void main(String[] args)
    throws Exception
    {
    File f = new File("C:\\test\\this#file.jpg");
    System.out.println(f.toURI());
    }
    }

    Output:

    file:/C:/test/this%23file.jpg

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 17, 2003
    #9
  10. "Chris Smith" <> wrote in message
    news:...
    > Andrew Thompson wrote:
    > > I hit a hitch presuming URL's could handle all
    > > Windows filenames.

    ....
    > That would only be the case if you didn't properly encode the URL.

    <snip Chris's solution>

    Thanks Chris! I'll give that a go.. :)

    --
    Andrew Thompson
    http://www.lensescapes.com/
    http://www.physci.org/
    http://www.1point1c.org/
     
    Andrew Thompson, Aug 17, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ibeetb
    Replies:
    1
    Views:
    457
    Steve C. Orr [MVP, MCSD]
    Jun 1, 2004
  2. RA Scheltema
    Replies:
    10
    Views:
    560
    Joona I Palaste
    Jan 24, 2004
  3. RA Scheltema

    platform independent serialization of a long

    RA Scheltema, Jan 23, 2004, in forum: C Programming
    Replies:
    10
    Views:
    602
    Joona I Palaste
    Jan 24, 2004
  4. bobrik
    Replies:
    18
    Views:
    1,835
    Jorge Vargas
    Aug 31, 2006
  5. Andy
    Replies:
    130
    Views:
    2,242
    Andy O'Meara
    Nov 10, 2008
Loading...

Share This Page