1 |
769 |
jeremybenn |
/* PlatformHelper.java -- Isolate OS-specific IO helper methods and variables
|
2 |
|
|
Copyright (C) 1998, 2002, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.java.io;
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* We had many changes in File.java, URLStreamHandler.java etc. to handle
|
42 |
|
|
* path representations on different platforms (Windows/Unix-family).
|
43 |
|
|
* Finally we'd like to collect all these ad hoc codes into this utility class.
|
44 |
|
|
* --Gansha
|
45 |
|
|
*/
|
46 |
|
|
public class PlatformHelper
|
47 |
|
|
{
|
48 |
|
|
public static final boolean isWindows = System.getProperty("os.name").indexOf("Windows") >= 0;
|
49 |
|
|
public static final String separator = System.getProperty("file.separator");
|
50 |
|
|
public static final char separatorChar = separator.charAt(0);
|
51 |
|
|
public static final String pathSeparator = System.getProperty("path.separator");
|
52 |
|
|
public static final char pathSeparatorChar = pathSeparator.charAt(0);
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* On most platforms 260 is equal or greater than a max path value,
|
56 |
|
|
* so we can set the initial buffer size of StringBuffer to half of this value
|
57 |
|
|
* to improve performance.
|
58 |
|
|
*/
|
59 |
|
|
public static final int INITIAL_MAX_PATH = 260/2;
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* This routine checks the input param "path" whether it begins with root path
|
63 |
|
|
* prefix.
|
64 |
|
|
* if not, return 0;
|
65 |
|
|
* if yes, return the len of root path prefix;
|
66 |
|
|
* --for Unix-family platform, root path begins with "/" and len is 1
|
67 |
|
|
* --for Windows platform, root path begins with "drive:\\" and len is 3
|
68 |
|
|
*/
|
69 |
|
|
public static final int beginWithRootPathPrefix(String path)
|
70 |
|
|
{
|
71 |
|
|
if (path.startsWith("/") || path.startsWith("\\"))
|
72 |
|
|
return 1;
|
73 |
|
|
|
74 |
|
|
if (!isWindows)
|
75 |
|
|
return 0;
|
76 |
|
|
|
77 |
|
|
if (path.length() > 2
|
78 |
|
|
&& Character.isLetter(path.charAt(0))
|
79 |
|
|
&& path.charAt(1) == ':'
|
80 |
|
|
&& (path.charAt(2) == '/' || path.charAt(2) == '\\'))
|
81 |
|
|
return 3;
|
82 |
|
|
|
83 |
|
|
return 0;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* This routine checks the input param "path" whether it's root directory.
|
88 |
|
|
* --for Unix-family platform, root directory is "/"
|
89 |
|
|
* --for Windows platform, root directory is "\\" or "drive:\\".
|
90 |
|
|
*/
|
91 |
|
|
public static final boolean isRootDirectory(String path)
|
92 |
|
|
{
|
93 |
|
|
int len = path.length();
|
94 |
|
|
return len > 0 && beginWithRootPathPrefix(path) == len;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* This routine checks whether input param "path" ends with separator
|
99 |
|
|
*/
|
100 |
|
|
public static final boolean endWithSeparator(String path)
|
101 |
|
|
{
|
102 |
|
|
if (path.endsWith("\\") || path.endsWith("/"))
|
103 |
|
|
return true;
|
104 |
|
|
|
105 |
|
|
return false;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* This routine removes from input param "path" the tail separator if it exists,
|
110 |
|
|
* and return the remain part.
|
111 |
|
|
*/
|
112 |
|
|
public static final String removeTailSeparator(String path)
|
113 |
|
|
{
|
114 |
|
|
if (endWithSeparator(path) && !isRootDirectory(path))
|
115 |
|
|
return path.substring(0, path.length() - 1);
|
116 |
|
|
|
117 |
|
|
return path;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* This routine returns last index of separator in input param "path",
|
122 |
|
|
* and return it.
|
123 |
|
|
*/
|
124 |
|
|
public static final int lastIndexOfSeparator(String path)
|
125 |
|
|
{
|
126 |
|
|
return Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
}
|