1 |
/* PlatformHelper.java -- Isolate OS-specific IO helper methods and variables |
/* PlatformHelper.java -- Isolate OS-specific IO helper methods and variables |
2 |
Copyright (C) 1998 Free Software Foundation, Inc. |
Copyright (C) 1998, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
41 |
import java.util.StringTokenizer; |
import java.util.StringTokenizer; |
42 |
|
|
43 |
/** |
/** |
44 |
* We have many submissions in File.java, URLStreamHandler.java etc. to handle path |
* We had many cahnges in File.java, URLStreamHandler.java etc. to handle |
45 |
* representations on different platforms (Windows/Unix-family). Finally we'd like |
* path representations on different platforms (Windows/Unix-family). |
46 |
* to collect all these ad hoc codes into this utility class. |
* Finally we'd like to collect all these ad hoc codes into this utility class. |
47 |
* --Gansha |
* --Gansha |
48 |
*/ |
*/ |
49 |
|
|
50 |
public class PlatformHelper{ |
public class PlatformHelper{ |
51 |
|
|
52 |
public static final boolean isWindows = System.getProperty("os.name").indexOf("Windows") >= 0; |
public static final boolean isWindows |
53 |
|
= System.getProperty("os.name").indexOf("Windows") >= 0; |
54 |
|
|
55 |
public static final String separator = System.getProperty("file.separator"); |
public static final String separator = System.getProperty("file.separator"); |
56 |
public static final char separatorChar = separator.charAt(0); |
public static final char separatorChar = separator.charAt(0); |
58 |
public static final char pathSeparatorChar = pathSeparator.charAt(0); |
public static final char pathSeparatorChar = pathSeparator.charAt(0); |
59 |
|
|
60 |
/** |
/** |
61 |
* This routine checks the input param "path" whether it begins with root path prefix. |
* On most platforms 260 is equal or greater than a max path value, |
62 |
|
* so we can set the initial buffer size of StringBuffer to half of this value |
63 |
|
* to improve performance. |
64 |
|
*/ |
65 |
|
public static final int INITIAL_MAX_PATH = 260/2; |
66 |
|
|
67 |
|
/** |
68 |
|
* This routine checks the input param "path" whether it begins with root path |
69 |
|
* prefix. |
70 |
* if not, return 0; |
* if not, return 0; |
71 |
* if yes, return the len of root path prefix; |
* if yes, return the len of root path prefix; |
72 |
* --for Unix-family platform, root path begins with "/" and len is 1 |
* --for Unix-family platform, root path begins with "/" and len is 1 |
115 |
*/ |
*/ |
116 |
String tmppath = path.replace('/', separatorChar); |
String tmppath = path.replace('/', separatorChar); |
117 |
StringBuffer canonpath; |
StringBuffer canonpath; |
118 |
|
// We found it'll be more efficient and easy to handle to |
119 |
|
// return a lowercased canonical path |
120 |
|
if(isWindows) |
121 |
|
tmppath = tmppath.toLowerCase(); |
122 |
int i; |
int i; |
123 |
if ((i = beginWithRootPathPrefix(tmppath)) == 0 ) |
if ((i = beginWithRootPathPrefix(tmppath)) == 0 ) |
124 |
return path; |
return path; |
125 |
canonpath = new StringBuffer(tmppath.substring(0, i)); |
|
126 |
|
/* The original |
127 |
|
"canonpath = new StringBuffer(tmppath.substring(0, i))" |
128 |
|
isn't very efficient because StringBuffer's |
129 |
|
ensureCapacity_unsynchronized will fail definitely each time |
130 |
|
and will enlarge buffer and copy contents. . |
131 |
|
*/ |
132 |
|
canonpath = new StringBuffer(INITIAL_MAX_PATH); |
133 |
|
canonpath.append(tmppath.substring(0, i)); |
134 |
tmppath = tmppath.substring(i); |
tmppath = tmppath.substring(i); |
135 |
// pathdepth==0 indicates there're only root path in the buffer |
// pathdepth==0 indicates there're only root path in the buffer |
136 |
int pathdepth = 0; |
int pathdepth = 0; |