Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  File platform specific strangeness  (Read 2466 times)
0 Members and 1 Guest are viewing this topic.
Offline i30817

Junior Member





« Posted 2009-07-28 23:15:54 »

A curious problem...
Making a file using the two arguments constructor for a arbitrary string is failing with some platform specific string.

Like this for instance:
String key = "Love, Nat - The Life and Adventures of Nat Love Better Known in the Cattle Country as \"Deadwood Dick\""
File tmp = new File(System.getProperty("java.io.tmpdir"));
File cached = new File(tmp, key + ".jpg");
ImageIO.write(arg, "jpg", cached);


I know that " is a platform specific thinguie that will get erased from the file. What i was not expecting was that ImageIO threw a Exception. I mean ofcourse that file doesn't exist. Wasn't because of that i used the f**king constructor?



java.io.FileNotFoundException: C:\DOCUME~1\MICROB~1\LOCALS~1\Temp\Love, Nat - The Life and Adventures of Nat Love Better Known in the Cattle Country as "Deadwood Dick".jpg (The filename, directory name, or volume label syntax is incorrect)
        at java.io.RandomAccessFile.open(Native Method)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)
        at javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:53)
        at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:37)
        at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:393)
        at javax.imageio.ImageIO.write(ImageIO.java:1514)
....



Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #1 - Posted 2009-07-28 23:35:30 »

The File needs to also be a valid writable location, which is probably your problem - print out cached.getAbsolutePath() and see if that containing folder actually exists on your filesystem, and that Java has permissions to write there. Also, you can just include your own temporary folder for your game, and then just call file.deleteOnExit() and when the user quits the files will be erased. The problem with this is obviously in the case that the game crashes (because then it won't be deleted) but that hasn't been a big issue for me.

I usually just make a "temp" folder in the game directory on startup, then put whatever I want in there, and make it all deleteOnExit().

See my work:
OTC Software
Offline i30817

Junior Member





« Reply #2 - Posted 2009-07-28 23:40:57 »

It is a valid writing location, as attested by the hundreds of other temporary files there. It's in fact the default java.io.tmpdir directory.

The only problem is that the strings i'm using are potentially filename illegal (book title specified - it's not a game btw).


So there are " or : and ; sometimes. I Thought that file did the right thing and escape these highly platform specific things, but guess not. Tell me there is a Escape method somewhere, else i'm running to the nio2 mailing list to forcefully "suggest" a static escape method.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline i30817

Junior Member





« Reply #3 - Posted 2009-07-29 01:10:01 »

FFS at least something to recognize the illegal chars and replace them.
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2009-07-29 02:05:36 »

You just can't use quotes in most OSes. Or semicolons or colons. They can't be escaped either on any OS I know of. Clue is in the FNF exception message: "The filename, directory name, or volume label syntax is incorrect".

Cas Smiley

Offline i30817

Junior Member





« Reply #5 - Posted 2009-07-29 02:09:39 »

That, no offense meant, i already knew, I just want the illegal character list, on each now and future OS, so that i can replace those chars in my input strings.

Time to troll the nio2 mailing list i guess.
Offline i30817

Junior Member





« Reply #6 - Posted 2009-07-29 02:16:40 »

Going to white list in the mean time.


 Roll Eyes
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #7 - Posted 2009-07-29 03:54:15 »

You just can't use quotes in most OSes. Or semicolons or colons. They can't be escaped either on any OS I know of. Clue is in the FNF exception message: "The filename, directory name, or volume label syntax is incorrect".

Cas Smiley

I miss my Amiga where you could use escape sequences to put any character in a file name, so stupid machines didn't have to interfere with how people wanted to name files.  There's no good reason that any printable character shouldn't be allowed in a file name.   I wonder if anyone will wise up and implement something like that again? (It's only been 25 years since Amiga OS had it...)

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2009-07-29 11:26:47 »

I miss my Amiga where you could use escape sequences to put any character in a file name, so stupid machines didn't have to interfere with how people wanted to name files.  There's no good reason that any printable character shouldn't be allowed in a file name.   I wonder if anyone will wise up and implement something like that again? (It's only been 25 years since Amiga OS had it...)

well, having a / or \ woruld really mess things up.
Offline pjt33
« Reply #9 - Posted 2009-07-29 12:11:27 »

There's no good reason that any printable character shouldn't be allowed in a file name.
Is reducing the opportunity for people to make stupid mistakes not a good reason? The classic demonstration of this is creating a file called -rf in a UNIX filesystem and then challenging someone else to delete it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #10 - Posted 2009-07-30 17:03:12 »

There's no good reason that any printable character shouldn't be allowed in a file name.

On Linux and FreeBSD (all *nix?) you can create a file with a name that might make many shell operations tough:

touch " This is an evil filename: ! * # ; ?"

It's not unreasonable to try to prevent users from creating such files.   Smiley

As for the OP, you can Google the Windows illegal filename characters easily enough.  And assuming (though it may not be true) that any character is *technically* valid on *nix, perhaps compile a list of characters that mean something to a shell and exclude those as well.

Just preprocess the filenames and replace the unacceptable characters with something like '_'.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #11 - Posted 2009-07-31 03:53:34 »

Is reducing the opportunity for people to make stupid mistakes not a good reason? The classic demonstration of this is creating a file called -rf in a UNIX filesystem and then challenging someone else to delete it.

If you are using the shell that already implies you would rather do things the hard way know what you are doing.  Besides command lines are so 1970's... computers that expect me to go through a bunch of long winded typing all the time when I'm NOT programming are lame  Smiley.

I'm assuming of course that an OS that supports non-idiotic restrictions on file names would also have a non-idiotic file browser.  Your example is also silly sine a three character file name "-rf" is highly unlikely to be needed in a real-world scenario where people like to do simple things like use real-world names/labels.  E.g. any modern OS should allow a user to create a file name with a colon or slash because those are very common basic forms of punctuation that could be used in natural language for file names.  Such as:  "Ice Age: Dawn of the Dinosaurs" or "ESPN 25: Who's #1?" just to pick a couple titles from IMDB.COM which would be the logical name of the .mpg or .mov or .avi file that would contain the movie or TV show, or the folder name that the scripts would go in, etc.  That is, if users were allowed to name things like they would using an old fashioned paper folder filing cabinet system... Computers are supposed to be better and make things easier right... well restrictions like that make things awkward for the billions of users just so the programmers have things a tiny bit easier.


Quote
On Linux and FreeBSD (all *nix?) you can create a file with a name that might make many shell operations tough:

touch " This is an evil filename: ! * # ; ?"

It's not unreasonable to try to prevent users from creating such files.

Yes it is... for the same reasons as above.  The needs of the many (users) outweigh the needs of the few (programmers)...  It's this mindset that we have to do everything the way the machine wants it rather than the other way around (the machine is there to make things easier for us) that we need to get over.

Ok, I'm done with this rant  Grin

Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2009-07-31 12:08:19 »

You are, of course, spot on with that rant Smiley

Cas Smiley

Offline pjt33
« Reply #13 - Posted 2009-07-31 13:49:04 »

The needs of the many (users) outweigh the needs of the few (programmers)...  It's this mindset that we have to do everything the way the machine wants it rather than the other way around (the machine is there to make things easier for us) that we need to get over.
The problem is that different users have different desires (more accurate than "needs") and you can't satisfy them all. I want a filesystem which plays nicely with the shell, because I find GUI file managers frustratingly inefficient.

On a related note, you think my example is silly because it's unlikely to be used. Leaving aside issues of people using whatever is supported (see Murphy's Law as originally stated), I think your idea is silly because you're using the filename for something which should be file metadata. Call it iceage3.mov and let your GUI file manager display the full name of the film when you hover over it. When you have people giving everything 100-character filenames it becomes impossible to display them aesthetically and a major usability issue to distinguish them.

Actually the biggest problem I've had with a filesystem was to do with something else you haven't mentioned, which is character encoding. I had some files with names in Spanish, so there were some accents and ñs in the names. Transferred them between two computers, ran a Java program which processed them. They showed up in file.listFiles() but when you tried to open them they didn't exist. I ended up renaming them.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #14 - Posted 2009-07-31 16:37:32 »

Why not just force alphanumeric filenames? That will always work...

1  
2  
3  
4  
5  
6  
7  
8  
StringBuffer fileName = new StringBuffer(fileString);
for (int i = 0; i < fileName.length(); i++)
{
     if (!Character.isLetterOrDigit(fileName.charAt(i))
     {
          fileName.replace(i,i,"X");
     }
}

See my work:
OTC Software
Offline i30817

Junior Member





« Reply #15 - Posted 2009-08-01 23:21:46 »

Why not just force alphanumeric filenames? That will always work...

1  
2  
3  
4  
5  
6  
7  
8  
StringBuffer fileName = new StringBuffer(fileString);
for (int i = 0; i < fileName.length(); i++)
{
     if (!Character.isLetterOrDigit(fileName.charAt(i))
     {
          fileName.replace(i,i,"X");
     }
}



Eventually i did the whitelisting function. Actually, if you search for the exception in google you get a lot of hits of clueless programmers asking why their program is failing "once in a while".

Oh i also did the the Gutenberg book indexer and downloader for my ebook reading  program. Just setting a dropdown box for the language and its probably new version time.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
    private static Pattern whiteList = Pattern.compile("[^\\p{L}&&[^\\p{Digit}&-.]]", Pattern.CANON_EQ | Pattern.UNICODE_CASE);

    /**
     * Returns a smaller than 256 chars file with all characters
     * except the alphanumerics and whitespace turned to whitespace
     * (escapes illegal chars in all platforms).
     * This method assumes the parent is safe, ie, it has
     * no illegal character in the file name, is less than 256 chars, and
     * can be writen as a directory.
     * @param parent
     * @param child
     * @return
     */

    public static File getSafeFileSystemFile(File parent, String child) throws IOException {

        if (parent.isDirectory() || parent.mkdirs()) {
           
            String parentPath = parent.getCanonicalPath();
            String childPath = whiteList.matcher(child).replaceAll(" ");

            //-1 is seperator
           int maxLen = 255 - parentPath.length() - 1;

            if (childPath.length() > maxLen) {
                int extensionSeparator = childPath.lastIndexOf('.');
                String extension = "";
                if (extensionSeparator != -1) {
                    extension = childPath.substring(extensionSeparator, childPath.length());
                    maxLen -= extension.length();
                }

                int i = maxLen - 1;
                while (childPath.charAt(i) != ' ' && i > 0) {
                    i--;
                }

                while (childPath.charAt(i) == ' ' && i > 0) {
                    i--;
                }

                if (i == 0) {
                    childPath = childPath.substring(0, maxLen - 1);
                } else {
                    childPath = childPath.substring(0, i + 1);
                }
               
                return new File(parentPath + File.separator + childPath + extension);
            }
            return new File(parent, childPath);
        } else {
            throw new IllegalArgumentException("The parent file path given is not writable or the file not a directory");
        }
    }

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #16 - Posted 2009-08-06 04:42:58 »

The problem is that different users have different desires (more accurate than "needs") and you can't satisfy them all. I want a filesystem which plays nicely with the shell, because I find GUI file managers frustratingly inefficient.

When you hit TAB to do filename completion like any decent shell would support, the shell will get it right for you, don't worry.  Let's face it, you have to deal with spaces in file names wither you like it or not.  If you escape that with a backslash like many shells do you can escape anything else with a backslash.  This isn't even a remotely difficult thing for the shell (as I say, Amigas did this in 1985 - sure you could abuse it.. but you know when you are and mostly it wasn't an issue.).

Quote
On a related note, you think my example is silly because it's unlikely to be used. Leaving aside issues of people using whatever is supported (see Murphy's Law as originally stated), I think your idea is silly because you're using the filename for something which should be file metadata. Call it iceage3.mov and let your GUI file manager display the full name of the film when you hover over it.

OK. See here you have it exactly backwards!  What kind of crap is "iceage3.mov" when that is NOT the name of the movie... you are thinking like a programmer... that 8.3 crap file is obfuscated computer nonsense - there isn't even a simple space before the 3.  If anything in that should be metadata it is the ".mov" part which is a hack to get the damn machine to think it knows what the file is.  (Apple got that part mostly right but sadly UIs didn't catch up to the concept of dealing with file types independantly of file names.  Now even Apple has given up on the concept.)

Quote
When you have people giving everything 100-character filenames it becomes impossible to display them aesthetically and a major usability issue to distinguish them.

100 character names are of course not the sort of "names" that people will use. Particularly not for "everything".  People will use names that come natural to them - computers should allow it - that's all I'm saying.

The aesthetics of displaying the information is also just another issue that GUI designers need to solve.  Eg. if it works for the spine of a book then it should work just as well in a fancy GUI - with exactly the same characters.  It's the primitive nature of so-called modern GUIs that is at fault here.

Quote
Actually the biggest problem I've had with a filesystem was to do with something else you haven't mentioned, which is character encoding. I had some files with names in Spanish, so there were some accents and ñs in the names. Transferred them between two computers, ran a Java program which processed them. They showed up in file.listFiles() but when you tried to open them they didn't exist. I ended up renaming them.

Unicode is of course the only character encoding that should be used anywhere, period.  The problem remains however because the unicode folks decided that there should be multiple encodings for the same character (same char at multiple code points as well as the composed vs. decomposed accent problem), thus making comparison algorithms complicated.
Presumably it is possible to do a proper unicode string comparison algorithm (the unicode people should provide working reference implementations) and so it should be done - let the machine do the work it is supposed to do.


Sorry I thought I was done ;-)

Offline pjt33
« Reply #17 - Posted 2009-08-06 10:27:24 »

When you hit TAB to do filename completion like any decent shell would support, the shell will get it right for you, don't worry.  Let's face it, you have to deal with spaces in file names wither you like it or not.  If you escape that with a backslash like many shells do you can escape anything else with a backslash.  This isn't even a remotely difficult thing for the shell (as I say, Amigas did this in 1985 - sure you could abuse it.. but you know when you are and mostly it wasn't an issue.).
I know it does, but when you're dealing with things like "for x in `find . -type f`" a space will break it, so you have to avoid some simple idioms.

Quote
OK. See here you have it exactly backwards!  What kind of crap is "iceage3.mov" when that is NOT the name of the movie... you are thinking like a programmer... that 8.3 crap file is obfuscated computer nonsense - there isn't even a simple space before the 3.  If anything in that should be metadata it is the ".mov" part which is a hack to get the damn machine to think it knows what the file is.  (Apple got that part mostly right but sadly UIs didn't catch up to the concept of dealing with file types independantly of file names.  Now even Apple has given up on the concept.)
I'm thinking like someone who wants short filenames which don't need wrapping. Call it ice_age_3_movie if you want. And for the record I use Linux, so I'm used to tools using magic rather than extensions to identify file types - but most end users are used to .tla endings.

Quote
Unicode is of course the only character encoding that should be used anywhere, period.
Unicode is a character set, not a character encoding. Even in a Unicode-only world you still have to choose between UTF-8, UTF-16LE, or UTF-16BE.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #18 - Posted 2009-08-07 03:58:17 »

I know it does, but when you're dealing with things like "for x in `find . -type f`" a space will break it, so you have to avoid some simple idioms.

That's part of the problem of course.  If a simple space breaks things there's an obvious problem, no?  I mean shouldn't this stuff be easier to deal with?  It's already total jargon.  Why should we need to come up with fancy cryptic command lines just to deal with a simple list of files that might have spaces in some of the names?  Current command lines are too primitive.  something more along the lines of  the Mac OS X "Automator" are needed, but they have to be fast and expressive enough to replace the shell for hackers/programmers.  I.e. with enough advancements something similar to Automator could be the shell.  Forget about typing command lines... with a complete menu of automator actions selectable by keyboard shortcuts and hierarchical menu systems (pie menus?) you should be able to get everything done that you would be able to do with a cryptic shell script. -But faster and clearer.  If you needed more power, well then you would have to resort to hard core programming to make a new action.

Quote
I'm thinking like someone who wants short filenames which don't need wrapping. Call it ice_age_3_movie if you want. And for the record I use Linux, so I'm used to tools using magic rather than extensions to identify file types - but most end users are used to .tla endings.

They are forced to because nobody as developed a proper way to deal with file type information outside of the file name.  That's just another aspect of these primitive filesystem restrictions we are dealing with. No modern OS deals with the filesystem as a proper database which should be query-able with standard database commands (SQL).  Microsoft has *still* failed to deliver WinFS for example. We need stuff like this http://tech.inhelsinki.nl/dbfs/, but it has to go mainstream and we need ways of trivially moving the "files" around without losing all the metadata.

Quote
Unicode is a character set, not a character encoding. Even in a Unicode-only world you still have to choose between UTF-8, UTF-16LE, or UTF-16BE.

 Roll Eyes UTF-8 then... point being that there should be no issues with accented characters if modern filesystems and OS' did a decent job - that's all part of my rant.  The issue you had with accents happened because programmers dropped the ball, they take shortcuts and impose artificial limits because they are comfortable working with computer jargon.  Sadly, rather than fixing this problem we will see society catch up and become more computer literate... we will think more like machines so the problem of computers not communicating effectively with people will be "solved" be changing the behavior of the people rather than the machines...  we could have won if we fought a little harder  Wink

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (31 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!