[Arakhnę-Dev] [100] Add URL support in FileSystem. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 100
Author: galland
Date: 2009-12-23 10:32:29 +0100 (Wed, 23 Dec 2009)
Log Message:
-----------
Add URL support in FileSystem.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2009-12-23 09:32:10 UTC (rev 99)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2009-12-23 09:32:29 UTC (rev 100)
@@ -30,7 +30,10 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLConnection;
+import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
/** An utility class that permits to deal with filenames.
@@ -44,10 +47,141 @@
*/
public static final char EXTENSION_SEPARATOR_CHAR = '.';
+ /** String which is representing the current directory in a relative path.
+ */
+ public static final String CURRENT_DIRECTORY = "."; //$NON-NLS-1$
+
+ /** String which is representing the parent directory in a relative path.
+ */
+ public static final String PARENT_DIRECTORY = ".."; //$NON-NLS-1$
+
+ /** Character used to separate paths on an URL.
+ */
+ public static final char URL_PATH_SEPARATOR_CHAR = '/';
+
+ /** Character used to separate paths on an URL.
+ */
+ public static final String URL_PATH_SEPARATOR = "/"; //$NON-NLS-1$
+
/** String used to specify a file extension.
*/
public static final String EXTENSION_SEPARATOR = "."; //$NON-NLS-1$
+
+ /** Prefix used to join in a Jar URL the jar filename and the inside-jar filename.
+ */
+ public static final String JAR_URL_FILE_ROOT = "!/"; //$NON-NLS-1$
+ /** Replies if the given URL has a jar scheme.
+ *
+ * @param url
+ * @return <code>true</code> if the given URL uses a jar scheme.
+ */
+ public static boolean isJarURL(URL url) {
+ return url!=null && "jar".equalsIgnoreCase(url.getProtocol()); //$NON-NLS-1$
+ }
+
+ /** Replies the jar part of the jar-scheme URL.
+ *
+ * @param url
+ * @return the URL of the jar file in the given URL, or <code>null</code>
+ * if the given URL does not use jar scheme.
+ */
+ public static URL getJarURL(URL url) {
+ if (!isJarURL(url)) return null;
+ String path = url.getPath();
+ int idx = path.lastIndexOf(JAR_URL_FILE_ROOT);
+ if (idx>=0) path = path.substring(0, idx);
+ try {
+ return new URL(path);
+ }
+ catch(MalformedURLException _) {
+ return null;
+ }
+ }
+
+ /** Replies the file part of the jar-scheme URL.
+ *
+ * @param url
+ * @return the file in the given URL, or <code>null</code>
+ * if the given URL does not use jar scheme.
+ */
+ public static File getJarFile(URL url) {
+ if (isJarURL(url)) {
+ String path = url.getPath();
+ int idx = path.lastIndexOf(JAR_URL_FILE_ROOT);
+ if (idx>=0) return new File(path.substring(idx+1));
+ }
+ return null;
+ }
+
+ /** Replies the jar-schemed URL composed of the two given components.
+ *
+ * @param jarFile is the URL to the jar file.
+ * @param insideFile is the name of the file inside the jar.
+ * @return the jar-schemed URL.
+ * @throws MalformedURLException when the URL is malformed.
+ */
+ public static URL toJarURL(File jarFile, File insideFile) throws MalformedURLException {
+ if (jarFile==null || insideFile==null) return null;
+ return toJarURL(jarFile, insideFile.getPath());
+ }
+
+ /** Replies the jar-schemed URL composed of the two given components.
+ *
+ * @param jarFile is the URL to the jar file.
+ * @param insideFile is the name of the file inside the jar.
+ * @return the jar-schemed URL.
+ * @throws MalformedURLException when the URL is malformed.
+ */
+ public static URL toJarURL(File jarFile, String insideFile) throws MalformedURLException {
+ if (jarFile==null || insideFile==null) return null;
+ StringBuffer buf = new StringBuffer("jar:"); //$NON-NLS-1$
+ buf.append(jarFile.toURI().toURL().toExternalForm());
+ buf.append(JAR_URL_FILE_ROOT);
+ String path = insideFile.replace(File.separatorChar, URL_PATH_SEPARATOR_CHAR);
+ if (path.startsWith(URL_PATH_SEPARATOR)) {
+ buf.append(path.substring(URL_PATH_SEPARATOR.length()));
+ }
+ else {
+ buf.append(path);
+ }
+ return new URL(buf.toString());
+ }
+
+ /** Replies the jar-schemed URL composed of the two given components.
+ *
+ * @param jarFile is the URL to the jar file.
+ * @param insideFile is the name of the file inside the jar.
+ * @return the jar-schemed URL.
+ * @throws MalformedURLException when the URL is malformed.
+ */
+ public static URL toJarURL(URL jarFile, File insideFile) throws MalformedURLException {
+ if (jarFile==null || insideFile==null) return null;
+ return toJarURL(jarFile, insideFile.getPath());
+ }
+
+ /** Replies the jar-schemed URL composed of the two given components.
+ *
+ * @param jarFile is the URL to the jar file.
+ * @param insideFile is the name of the file inside the jar.
+ * @return the jar-schemed URL.
+ * @throws MalformedURLException when the URL is malformed.
+ */
+ public static URL toJarURL(URL jarFile, String insideFile) throws MalformedURLException {
+ if (jarFile==null || insideFile==null) return null;
+ StringBuffer buf = new StringBuffer("jar:"); //$NON-NLS-1$
+ buf.append(jarFile.toExternalForm());
+ buf.append(JAR_URL_FILE_ROOT);
+ String path = insideFile.replace(File.separatorChar, URL_PATH_SEPARATOR_CHAR);
+ if (path.startsWith(URL_PATH_SEPARATOR)) {
+ buf.append(path.substring(URL_PATH_SEPARATOR.length()));
+ }
+ else {
+ buf.append(path);
+ }
+ return new URL(buf.toString());
+ }
+
/** Replies if the current operating system uses case-sensitive filename.
*
* @return <code>true</code> if the filenames on the current file system are case sensitive,
@@ -84,12 +218,81 @@
*
* @param filename is the name to parse.
* @return the dirname of the specified file.
+ * @see #shortBasename(String)
+ * @see #largeBasename(String)
+ * @see #basename(String)
+ * @see #extension(String)
+ * @deprecated use {@link #extension(File)} or {@link #extension(URL)}
*/
+ @Deprecated
public static String dirname(String filename) {
if (filename==null) return null;
- return new File(filename).getParent();
+ int idx = filename.lastIndexOf(File.separatorChar);
+ if (idx<0) return CURRENT_DIRECTORY;
+ if (idx==0) return File.separator;
+ return filename.substring(0,idx);
}
+ /** Replies the dirname of the specified file.
+ *
+ * @param filename is the name to parse.
+ * @return the dirname of the specified file.
+ * @see #shortBasename(File)
+ * @see #largeBasename(File)
+ * @see #basename(File)
+ * @see #extension(File)
+ */
+ public static File dirname(File filename) {
+ if (filename==null) return null;
+ return filename.getParentFile();
+ }
+
+ /** Replies the dirname of the specified file.
+ *
+ * @param filename is the name to parse.
+ * @return the dirname of the specified file.
+ * @see #shortBasename(URL)
+ * @see #largeBasename(URL)
+ * @see #basename(URL)
+ * @see #extension(URL)
+ */
+ public static URL dirname(URL filename) {
+ if (filename==null) return null;
+
+ URL prefix = null;
+ String path;
+ if (isJarURL(filename)) {
+ prefix = getJarURL(filename);
+ path = getJarFile(filename).getPath();
+ }
+ else
+ path = filename.getPath();
+
+ int idx = path.lastIndexOf(URL_PATH_SEPARATOR_CHAR);
+ if (idx==path.length()-1)
+ idx = path.lastIndexOf(URL_PATH_SEPARATOR_CHAR, path.length()-2);
+
+ path = (idx<0) ? URL_PATH_SEPARATOR : path.substring(0, idx+1);
+
+ try {
+ if (prefix!=null) {
+ return toJarURL(prefix, path);
+ }
+ URI uri = new URI(
+ filename.getProtocol(),
+ filename.getUserInfo(),
+ filename.getHost(),
+ filename.getPort(),
+ path,
+ null,
+ null);
+ return uri.toURL();
+ }
+ catch (Throwable _) {
+ return null;
+ }
+ }
+
/** Replies the basename of the specified file with the extension.
*
* @param filename is the name to parse.
@@ -97,7 +300,8 @@
*/
public static String largeBasename(String filename) {
if (filename==null) return null;
- return new File(filename).getName();
+ int idx = filename.lastIndexOf(File.separatorChar);
+ return (idx<0) ? filename : filename.substring(idx+1);
}
/** Replies the basename of the specified file with the extension.
@@ -110,14 +314,54 @@
return filename.getName();
}
+ /** Replies the basename of the specified file with the extension.
+ *
+ * @param filename is the name to parse.
+ * @return the basename of the specified file with the extension.
+ */
+ public static String largeBasename(URL filename) {
+ if (filename==null) return null;
+ String fullPath = filename.getPath();
+ int idx = fullPath.lastIndexOf(URL_PATH_SEPARATOR_CHAR);
+ int end = fullPath.length();
+ if (idx==end-1) {
+ end --;
+ idx = fullPath.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end-1);
+ }
+ if (idx<0) idx = -1;
+ return fullPath.substring(idx+1, end);
+ }
+
/** Reply the basename of the specified file without the last extension.
*
* @param filename is the name to parse.
* @return the basename of the specified file without the last extension.
+ * @see #shortBasename(String)
+ * @see #largeBasename(String)
+ * @see #dirname(String)
+ * @see #extension(String)
*/
public static String basename(String filename) {
if (filename==null) return null;
- String largeBasename = new File(filename).getName();
+ int idx = filename.lastIndexOf(File.separatorChar);
+ String basename = (idx<0) ? filename : filename.substring(idx+1);
+ idx = basename.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) return basename;
+ return basename.substring(0,idx);
+ }
+
+ /** Reply the basename of the specified file without the last extension.
+ *
+ * @param filename is the name to parse.
+ * @return the basename of the specified file without the last extension.
+ * @see #shortBasename(File)
+ * @see #largeBasename(File)
+ * @see #dirname(File)
+ * @see #extension(File)
+ */
+ public static String basename(File filename) {
+ if (filename==null) return null;
+ String largeBasename = filename.getName();
int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
if (idx<=0) return largeBasename;
return largeBasename.substring(0,idx);
@@ -127,10 +371,25 @@
*
* @param filename is the name to parse.
* @return the basename of the specified file without the last extension.
+ * @see #shortBasename(URL)
+ * @see #largeBasename(URL)
+ * @see #dirname(URL)
+ * @see #extension(URL)
*/
- public static String basename(File filename) {
+ public static String basename(URL filename) {
if (filename==null) return null;
- return basename(filename.getAbsolutePath());
+ String largeBasename = filename.getPath();
+ int idx = largeBasename.lastIndexOf(URL_PATH_SEPARATOR_CHAR);
+ int end = largeBasename.length();
+ if (idx==end-1) {
+ end --;
+ idx = largeBasename.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end-1);
+ }
+ if (idx<0) idx = -1;
+ largeBasename = largeBasename.substring(idx+1, end);
+ idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) return largeBasename;
+ return largeBasename.substring(0,idx);
}
/** Reply the basename of the specified file without all the extensions.
@@ -140,9 +399,23 @@
*/
public static String shortBasename(String filename) {
if (filename==null) return null;
- String largeBasename = new File(filename).getName();
+ int idx = filename.lastIndexOf(File.separatorChar);
+ String basename = (idx<0) ? filename : filename.substring(idx+1);
+ idx = basename.indexOf(getFileExtensionCharacter());
+ if (idx<0) return basename;
+ return basename.substring(0,idx);
+ }
+
+ /** Reply the basename of the specified file without all the extensions.
+ *
+ * @param filename is the name to parse.
+ * @return the basename of the specified file without all the extensions.
+ */
+ public static String shortBasename(File filename) {
+ if (filename==null) return null;
+ String largeBasename = filename.getName();
int idx = largeBasename.indexOf(getFileExtensionCharacter());
- if (idx<=0) return largeBasename;
+ if (idx<0) return largeBasename;
return largeBasename.substring(0,idx);
}
@@ -151,42 +424,103 @@
* @param filename is the name to parse.
* @return the basename of the specified file without all the extensions.
*/
- public static String shortBasename(File filename) {
+ public static String shortBasename(URL filename) {
if (filename==null) return null;
- return shortBasename(filename.getAbsolutePath());
+ String largeBasename = filename.getPath();
+ int idx = largeBasename.lastIndexOf(URL_PATH_SEPARATOR_CHAR);
+ int end = largeBasename.length();
+ if (idx==end-1) {
+ end --;
+ idx = largeBasename.lastIndexOf(URL_PATH_SEPARATOR_CHAR, end-1);
+ }
+ if (idx<0) idx = -1;
+ largeBasename = largeBasename.substring(idx+1, end);
+ idx = largeBasename.indexOf(getFileExtensionCharacter());
+ if (idx<0) return largeBasename;
+ return largeBasename.substring(0,idx);
}
/** Reply the extension of the specified file.
*
* @param filename is the name to parse.
* @return the extension of the specified file
+ * @deprecated use {@link #extension(File)} or {@link #extension(URL)}
+ * @see #shortBasename(String)
+ * @see #largeBasename(String)
+ * @see #basename(String)
+ * @see #dirname(String)
+ * @see #extensions(String)
*/
+ @Deprecated
public static String extension(String filename) {
+ try {
+ return extension(new URL(filename));
+ }
+ catch(MalformedURLException _) {
+ return extension(new File(filename));
+ }
+ }
+
+ /** Reply the extension of the specified file.
+ *
+ * @param filename is the name to parse.
+ * @return the extension of the specified file
+ * @see #shortBasename(File)
+ * @see #largeBasename(File)
+ * @see #basename(File)
+ * @see #dirname(File)
+ * @see #extensions(File)
+ */
+ public static String extension(File filename) {
if (filename==null) return null;
- String largeBasename = new File(filename).getName();
+ String largeBasename = largeBasename(filename);
int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
if (idx<=0) return ""; //$NON-NLS-1$
return largeBasename.substring(idx);
}
-
+
/** Reply the extension of the specified file.
*
* @param filename is the name to parse.
* @return the extension of the specified file
+ * @see #shortBasename(URL)
+ * @see #largeBasename(URL)
+ * @see #basename(URL)
+ * @see #dirname(URL)
+ * @see #extensions(URL)
*/
- public static String extension(File filename) {
+ public static String extension(URL filename) {
if (filename==null) return null;
- return extension(filename.getAbsolutePath());
+ String largeBasename = largeBasename(filename);
+ int idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
+ if (idx<=0) return ""; //$NON-NLS-1$
+ return largeBasename.substring(idx);
}
/** Reply all the extensions of the specified file.
*
* @param filename is the name to parse.
* @return the extensions of the specified file
+ * @deprecated use {@link #extensions(File)} or {@link #extensions(URL)}
*/
+ @Deprecated
public static String[] extensions(String filename) {
+ try {
+ return extensions(new URL(filename));
+ }
+ catch(MalformedURLException _) {
+ return extensions(new File(filename));
+ }
+ }
+
+ /** Reply all the extensions of the specified file.
+ *
+ * @param filename is the name to parse.
+ * @return the extensions of the specified file
+ */
+ public static String[] extensions(File filename) {
if (filename==null) return new String[0];
- String largeBasename = new File(filename).getName();
+ String largeBasename = largeBasename(filename);
String[] parts = largeBasename.split("["+getFileExtensionCharacter()+"]"); //$NON-NLS-1$ //$NON-NLS-2$
if (parts.length<=1) return new String[0];
String[] r = new String[parts.length-1];
@@ -199,19 +533,30 @@
* @param filename is the name to parse.
* @return the extensions of the specified file
*/
- public static String[] extensions(File filename) {
+ public static String[] extensions(URL filename) {
if (filename==null) return new String[0];
- return extensions(filename.getAbsolutePath());
+ String largeBasename = largeBasename(filename);
+ String[] parts = largeBasename.split("["+getFileExtensionCharacter()+"]"); //$NON-NLS-1$ //$NON-NLS-2$
+ if (parts.length<=1) return new String[0];
+ String[] r = new String[parts.length-1];
+ System.arraycopy(parts, 1, r, 0, r.length);
+ return r;
}
/** Replies the parts of a path.
*
* @param filename is the name to parse.
* @return the parts of a path.
+ * @deprecated use {@link #split(File)} or {@link #split(URL)}
*/
+ @Deprecated
public static String[] split(String filename) {
- if (filename==null) return new String[0];
- return filename.split("["+File.separatorChar+"]"); //$NON-NLS-1$ //$NON-NLS-2$
+ try {
+ return split(new URL(filename));
+ }
+ catch(MalformedURLException _) {
+ return split(new File(filename));
+ }
}
/** Replies the parts of a path.
@@ -221,14 +566,31 @@
*/
public static String[] split(File filename) {
if (filename==null) return new String[0];
- return split(filename.getPath());
+ return filename.getPath().split("["+File.separatorChar+"]"); //$NON-NLS-1$ //$NON-NLS-2$
}
+ /** Replies the parts of a path.
+ *
+ * @param filename is the name to parse.
+ * @return the parts of a path.
+ */
+ public static String[] split(URL filename) {
+ if (filename==null) return new String[0];
+ String path;
+ if (isJarURL(filename))
+ path = getJarFile(filename).getPath();
+ else
+ path = filename.getPath();
+ return path.split("["+URL_PATH_SEPARATOR_CHAR+"]"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
/** Join the parts of a path with the current OS directory separator.
*
* @param elements are the path's elements to join.
* @return the result of the join of the path's elements.
+ * @deprecated use {@link #split(File)} or {@link #split(URL)}
*/
+ @Deprecated
public static String join(String... elements) {
StringBuffer buf = new StringBuffer();
boolean first = true;
@@ -248,6 +610,136 @@
return buf.toString();
}
+ /** Join the parts of a path and append them to the given File.
+ *
+ * @param fileBase is the file to put as prefix.
+ * @param elements are the path's elements to join.
+ * @return the result of the join of the path's elements.
+ */
+ public static File join(File fileBase, String... elements) {
+ if (fileBase==null) return null;
+ StringBuffer buf = new StringBuffer(fileBase.getPath());
+ boolean empty;
+ for(String elt : elements) {
+ empty = (elt==null || elt.length()==0);
+ if (!empty) {
+ assert(elt!=null);
+ if (!elt.startsWith(File.separator)
+ && buf.length()>=0
+ && buf.charAt(buf.length()-1)!=File.separatorChar) {
+ buf.append(File.separatorChar);
+ }
+ buf.append(elt);
+ }
+ }
+ return new File(buf.toString());
+ }
+
+ /** Join the parts of a path and append them to the given File.
+ *
+ * @param fileBase is the file to put as prefix.
+ * @param elements are the path's elements to join.
+ * @return the result of the join of the path's elements.
+ */
+ public static File join(File fileBase, File... elements) {
+ if (fileBase==null) return null;
+ StringBuffer buf = new StringBuffer(fileBase.getPath());
+ for(File elt : elements) {
+ if (!elt.isAbsolute()) {
+ if (buf.length()>=0 && buf.charAt(buf.length()-1)!=File.separatorChar) {
+ buf.append(File.separatorChar);
+ }
+ }
+ buf.append(elt.getPath());
+ }
+ return new File(buf.toString());
+ }
+
+ /** Join the parts of a path and append them to the given URL.
+ *
+ * @param urlBase is the url to put as prefix.
+ * @param elements are the path's elements to join.
+ * @return the result of the join of the path's elements.
+ */
+ public static URL join(URL urlBase, String... elements) {
+ if (urlBase==null) return null;
+ StringBuffer buf = new StringBuffer(urlBase.getPath());
+ boolean empty;
+ for(String elt : elements) {
+ empty = (elt==null || elt.length()==0);
+ if (!empty) {
+ assert(elt!=null);
+ if (!elt.startsWith(File.separator)
+ && (buf.length()==0
+ || buf.charAt(buf.length()-1)!=URL_PATH_SEPARATOR_CHAR)) {
+ buf.append(URL_PATH_SEPARATOR_CHAR);
+ }
+ buf.append(elt);
+ }
+ }
+ try {
+ if (isJarURL(urlBase)) {
+ return new URL(
+ urlBase.getProtocol(),
+ urlBase.getHost(),
+ urlBase.getPort(),
+ buf.toString());
+ }
+ URI uri = new URI(
+ urlBase.getProtocol(),
+ urlBase.getUserInfo(),
+ urlBase.getHost(),
+ urlBase.getPort(),
+ buf.toString(),
+ urlBase.getQuery(),
+ urlBase.getRef());
+ return uri.toURL();
+ }
+ catch (Throwable _) {
+ return null;
+ }
+ }
+
+ /** Join the parts of a path and append them to the given URL.
+ *
+ * @param urlBase is the url to put as prefix.
+ * @param elements are the path's elements to join.
+ * @return the result of the join of the path's elements.
+ */
+ public static URL join(URL urlBase, File... elements) {
+ if (urlBase==null) return null;
+ StringBuffer buf = new StringBuffer(urlBase.getPath());
+ for(File elt : elements) {
+ if (!elt.isAbsolute()) {
+ if (buf.length()==0 || buf.charAt(buf.length()-1)!=URL_PATH_SEPARATOR_CHAR) {
+ buf.append(URL_PATH_SEPARATOR_CHAR);
+ }
+ }
+ buf.append(elt.getPath());
+ }
+ try {
+ if (isJarURL(urlBase)) {
+ return new URL(
+ urlBase.getProtocol(),
+ urlBase.getHost(),
+ urlBase.getPort(),
+ buf.toString());
+ }
+ URI uri = new URI(
+ urlBase.getProtocol(),
+ urlBase.getUserInfo(),
+ urlBase.getHost(),
+ urlBase.getPort(),
+ buf.toString(),
+ urlBase.getQuery(),
+ urlBase.getRef());
+ return uri.toURL();
+ }
+ catch (Throwable _) {
+ return null;
+ }
+ }
+
/** Replies if the specified file has the specified extension.
* <p>
* The test is dependent of the case-sensitive attribute of operating system.
@@ -256,11 +748,32 @@
* @param extension is the extension to test.
* @return <code>true</code> if the given filename has the given extension,
* otherwise <code>false</code>
+ * @deprecated use {@link #hasExtension(File,String)} or {@link #hasExtension(URL,String)}
*/
+ @Deprecated
public static boolean hasExtension(String filename, String extension) {
+ try {
+ return hasExtension(new URL(filename), extension);
+ }
+ catch(MalformedURLException _) {
+ return hasExtension(new File(filename), extension);
+ }
+ }
+
+ /** Replies if the specified file has the specified extension.
+ * <p>
+ * The test is dependent of the case-sensitive attribute of operating system.
+ *
+ * @param filename is the filename to parse
+ * @param extension is the extension to test.
+ * @return <code>true</code> if the given filename has the given extension,
+ * otherwise <code>false</code>
+ */
+ public static boolean hasExtension(File filename, String extension) {
if (filename==null) return false;
String extent = extension;
- if (!extent.startsWith(EXTENSION_SEPARATOR)) extent = EXTENSION_SEPARATOR+extent;
+ if (!"".equals(extent) && !extent.startsWith(EXTENSION_SEPARATOR)) //$NON-NLS-1$
+ extent = EXTENSION_SEPARATOR+extent;
String ext = extension(filename);
if (ext==null) return false;
if (isCaseSensitiveFilenameSystem())
@@ -277,21 +790,46 @@
* @return <code>true</code> if the given filename has the given extension,
* otherwise <code>false</code>
*/
- public static boolean hasExtension(File filename, String extension) {
+ public static boolean hasExtension(URL filename, String extension) {
if (filename==null) return false;
- return hasExtension(filename.getName(), extension);
+ String extent = extension;
+ if (!"".equals(extent) && !extent.startsWith(EXTENSION_SEPARATOR)) //$NON-NLS-1$
+ extent = EXTENSION_SEPARATOR+extent;
+ String ext = extension(filename);
+ if (ext==null) return false;
+ if (isCaseSensitiveFilenameSystem())
+ return ext.equals(extent);
+ return ext.equalsIgnoreCase(extent);
}
/** Remove the extension from the specified filename.
*
* @param filename is the filename to parse.
* @return the filename without the extension.
+ * @deprecated use {@link #removeExtension(File)} or {@link #removeExtension(URL)}
*/
+ @Deprecated
public static String removeExtension(String filename) {
+ try {
+ return removeExtension(new URL(filename)).toExternalForm();
+ }
+ catch(MalformedURLException _) {
+ return removeExtension(new File(filename)).getPath();
+ }
+ }
+
+ /** Remove the extension from the specified filename.
+ *
+ * @param filename is the filename to parse.
+ * @return the filename without the extension.
+ */
+ public static File removeExtension(File filename) {
if (filename==null) return null;
- int idx = filename.lastIndexOf(getFileExtensionCharacter());
- if (idx<=0) return filename;
- return filename.substring(0,idx);
+ File dir = filename.getParentFile();
+ String name = filename.getName();
+ int idx = name.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) return filename;
+ return new File(dir, name.substring(0,idx));
}
/** Remove the extension from the specified filename.
@@ -299,9 +837,36 @@
* @param filename is the filename to parse.
* @return the filename without the extension.
*/
- public static String removeExtension(File filename) {
+ public static URL removeExtension(URL filename) {
if (filename==null) return null;
- return removeExtension(filename.getAbsolutePath());
+ String path = filename.getPath();
+ int idx = path.lastIndexOf(URL_PATH_SEPARATOR);
+ StringBuffer buf = new StringBuffer((idx<0) ? "" : path.substring(0, idx+1)); //$NON-NLS-1$
+ String largeBasename = path.substring(idx+1);
+ idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) return filename;
+ buf.append(largeBasename.substring(0, idx));
+ try {
+ if (isJarURL(filename)) {
+ return new URL(
+ filename.getProtocol(),
+ filename.getHost(),
+ filename.getPort(),
+ buf.toString());
+ }
+ URI uri = new URI(
+ filename.getProtocol(),
+ filename.getUserInfo(),
+ filename.getHost(),
+ filename.getPort(),
+ buf.toString(),
+ filename.getQuery(),
+ filename.getRef());
+ return uri.toURL();
+ }
+ catch(Throwable _) {
+ return null;
+ }
}
/** Replace the extension of the specified filename by the given extension.
@@ -310,23 +875,20 @@
* @param filename is the filename to parse.
* @param extension is the extension to remove if it is existing.
* @return the filename without the extension.
+ * @deprecated use {@link #replaceExtension(File,String)} or {@link #replaceExtension(URL,String)}
*/
+ @Deprecated
public static String replaceExtension(String filename, String extension) {
- if (filename==null) return null;
- String extent = extension;
- if (!extent.startsWith(EXTENSION_SEPARATOR)) extent = EXTENSION_SEPARATOR+extent;
- int idx = filename.lastIndexOf(EXTENSION_SEPARATOR_CHAR);
- StringBuffer buf = new StringBuffer();
- if (idx<=0) buf.append(filename);
- else {
- buf.append(filename.substring(0, idx));
- buf.append(extent);
+ try {
+ return replaceExtension(new URL(filename), extension).toExternalForm();
}
- return buf.toString();
+ catch(MalformedURLException _) {
+ return replaceExtension(new File(filename), extension).getPath();
+ }
}
/** Replace the extension of the specified filename by the given extension.
- * If the filename has no extension, the specifiedone will be added.
+ * If the filename has no extension, the specified one will be added.
*
* @param filename is the filename to parse.
* @param extension is the extension to remove if it is existing.
@@ -334,9 +896,62 @@
*/
public static File replaceExtension(File filename, String extension) {
if (filename==null) return null;
- return new File(replaceExtension(filename.getAbsolutePath(), extension));
+ File dir = filename.getParentFile();
+ String name = filename.getName();
+ int idx = name.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) return new File(dir, name+extension);
+ return new File(dir, name.substring(0,idx)+extension);
}
+ /** Replace the extension of the specified filename by the given extension.
+ * If the filename has no extension, the specified one will be added.
+ *
+ * @param filename is the filename to parse.
+ * @param extension is the extension to remove if it is existing.
+ * @return the filename without the extension.
+ */
+ public static URL replaceExtension(URL filename, String extension) {
+ if (filename==null) return null;
+ String path = filename.getPath();
+ int idx = path.lastIndexOf(URL_PATH_SEPARATOR);
+ int end = path.length();
+ if (idx==end-1) {
+ end --;
+ idx = path.lastIndexOf(URL_PATH_SEPARATOR, end-1);
+ }
+ StringBuffer buf = new StringBuffer((idx<0) ? "" : path.substring(0, idx+1)); //$NON-NLS-1$
+ String largeBasename = path.substring(idx+1, end);
+ idx = largeBasename.lastIndexOf(getFileExtensionCharacter());
+ if (idx<0) {
+ buf.append(largeBasename);
+ }
+ else {
+ buf.append(largeBasename.substring(0, idx));
+ }
+ buf.append(extension);
+ try {
+ if (isJarURL(filename)) {
+ return new URL(
+ filename.getProtocol(),
+ filename.getHost(),
+ filename.getPort(),
+ buf.toString());
+ }
+ URI uri = new URI(
+ filename.getProtocol(),
+ filename.getUserInfo(),
+ filename.getHost(),
+ filename.getPort(),
+ buf.toString(),
+ filename.getQuery(),
+ filename.getRef());
+ return uri.toURL();
+ }
+ catch(Throwable _) {
+ return null;
+ }
+ }
+
/** Copy the first file into the second file.
* <p>
* The content of the second file will be lost.
@@ -346,6 +961,7 @@
* @param in is the file to copy.
* @param out is the target file
* @throws IOException in case of error.
+ * @see #fileCopy(URL, File)
*/
public static void fileCopy(File in, File out) throws IOException {
FileChannel inChannel = new FileInputStream(in).getChannel();
@@ -371,6 +987,44 @@
}
}
+ /** Copy the first file into the second file.
+ * <p>
+ * The content of the second file will be lost.
+ * This copy function allows to do a copy between two different
+ * partitions.
+ *
+ * @param in is the file to copy.
+ * @param out is the target file
+ * @throws IOException in case of error.
+ * @see #fileCopy(File, File)
+ */
+ public static void fileCopy(URL in, File out) throws IOException {
+ URLConnection connection = in.openConnection();
+ ReadableByteChannel inChannel = Channels.newChannel(connection.getInputStream());
+ FileChannel outChannel = new FileOutputStream(out).getChannel();
+ int size = connection.getContentLength();
+ try {
+ // apparently has trouble copying large files on Windows
+ if (size<0 || OperatingSystem.WIN.isCurrentOS()) {
+ // magic number for Windows, 64Mb - 32Kb
+ int maxCount = (64 * 1024 * 1024) - (32 * 1024);
+ long position = 0;
+ long copied = 1;
+ while ( (size>=0 && position<size) || (size<0 && copied>0)) {
+ copied = outChannel.transferFrom(inChannel, position, maxCount);
+ position += copied;
+ }
+ }
+ else {
+ outChannel.transferFrom(inChannel, 0, size);
+ }
+ }
+ finally {
+ if (inChannel!=null) inChannel.close();
+ if (outChannel!=null) outChannel.close();
+ }
+ }
+
/** Replies the user home directory.
*
* @return the home directory of the current user.
@@ -416,11 +1070,12 @@
else if (os==OperatingSystem.WIN) {
String userName = System.getProperty("user.name"); //$NON-NLS-1$
if (userName!=null && !"".equals(userName)) { //$NON-NLS-1$
- return new File(join(
- "C:","Documents and Settings", //$NON-NLS-1$//$NON-NLS-2$
+ return join(
+ new File("C:"), //$NON-NLS-1$
+ "Documents and Settings", //$NON-NLS-1$
userName,
"Local Settings","Application Data", //$NON-NLS-1$//$NON-NLS-2$
- software));
+ software);
}
}
return new File(userHome,software);
@@ -466,7 +1121,7 @@
OperatingSystem os = OperatingSystem.getCurrentOS();
if (os.isUnixCompliant()) {
File[] roots = File.listRoots();
- return new File(new File(join(roots[0].getAbsolutePath(),"etc")), software); //$NON-NLS-1$
+ return join(roots[0],"etc", software); //$NON-NLS-1$
}
else if (os==OperatingSystem.WIN) {
File pfDirectory;
@@ -514,7 +1169,7 @@
OperatingSystem os = OperatingSystem.getCurrentOS();
if (os.isUnixCompliant()) {
File[] roots = File.listRoots();
- return new File(new File(join(roots[0].getAbsolutePath(),"usr","lib")), software); //$NON-NLS-1$ //$NON-NLS-2$
+ return join(roots[0],"usr","lib", software); //$NON-NLS-1$ //$NON-NLS-2$
}
else if (os==OperatingSystem.WIN) {
File pfDirectory;
@@ -652,12 +1307,12 @@
if (urlDescription.toLowerCase().startsWith("resource:")) { //$NON-NLS-1$
resourceName = urlDescription.substring(9);
- if (resourceName.startsWith("/")) resourceName = resourceName.substring(1); //$NON-NLS-1$
+ if (resourceName.startsWith(URL_PATH_SEPARATOR)) resourceName = resourceName.substring(1);
return ClassLoader.getSystemResource(resourceName);
}
resourceName = urlDescription;
- if (resourceName.startsWith("/")) resourceName = resourceName.substring(1); //$NON-NLS-1$
+ if (resourceName.startsWith(URL_PATH_SEPARATOR)) resourceName = resourceName.substring(1);
url = ClassLoader.getSystemResource(resourceName);
if (url!=null) return url;
}
@@ -697,7 +1352,7 @@
return filename;
}
- /** Replies if the given URL is using a protocol which oculd be map to files.
+ /** Replies if the given URL is using a protocol which could be map to files.
*
* @param url
* @return <code>true</code> if the given url is a "file", "http",
@@ -730,12 +1385,12 @@
String scheme = filename.getProtocol();
if ("jar".equalsIgnoreCase(scheme)) { //$NON-NLS-1$
try {
- String[] parts = filename.getPath().split("!/"); //$NON-NLS-1$
+ String[] parts = filename.getPath().split(JAR_URL_FILE_ROOT);
URL u = makeAbsolute(new URL(parts[0]), current);
StringBuffer adr = new StringBuffer("jar:"); //$NON-NLS-1$
adr.append(u.toExternalForm());
for(int i=1; i<parts.length; i++) {
- adr.append("!/"); //$NON-NLS-1$
+ adr.append(JAR_URL_FILE_ROOT);
adr.append(parts[i]);
}
return new URL(adr.toString());
@@ -748,9 +1403,9 @@
int port = filename.getPort();
try {
String absPath = filename.getPath();
- if (!absPath.startsWith("/")) { //$NON-NLS-1$
+ if (!absPath.startsWith(URL_PATH_SEPARATOR)) {
URL rootUrl = current.toURI().toURL();
- absPath = rootUrl.getPath()+"/"+absPath; //$NON-NLS-1$
+ absPath = rootUrl.getPath()+URL_PATH_SEPARATOR+absPath;
return new URL(filename.getProtocol(), filename.getHost(), port, absPath);
}
}
@@ -788,26 +1443,26 @@
String prefix, parentStr;
if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
- int index = path.indexOf("!/"); //$NON-NLS-1$
+ int index = path.indexOf(JAR_URL_FILE_ROOT);
assert(index>0);
prefix = path.substring(0,index+1);
path = path.substring(index+1);
- parentStr = "/"; //$NON-NLS-1$
+ parentStr = URL_PATH_SEPARATOR;
}
else if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
prefix = null;
- parentStr = "../"; //$NON-NLS-1$
+ parentStr = ".."+URL_PATH_SEPARATOR; //$NON-NLS-1$
}
else {
prefix = null;
- parentStr = "/"; //$NON-NLS-1$
+ parentStr = URL_PATH_SEPARATOR;
}
if (path==null || "".equals(path)) path = parentStr; //$NON-NLS-1$
- int index = path.lastIndexOf('/');
+ int index = path.lastIndexOf(URL_PATH_SEPARATOR_CHAR);
if (index==-1) path = parentStr;
else if (index==path.length()-1) {
- index = path.lastIndexOf('/', index-1);
+ index = path.lastIndexOf(URL_PATH_SEPARATOR_CHAR, index-1);
if (index==-1) path = parentStr;
else path = path.substring(0, index+1);
}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-12-23 09:32:10 UTC (rev 99)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2009-12-23 09:32:29 UTC (rev 100)
@@ -20,9 +20,11 @@
package org.arakhne.vmutil;
import java.io.File;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/**
@@ -33,16 +35,132 @@
private static final File f1 = new File("/home/test.x.z.z"); //$NON-NLS-1$
private static final File f2 = new File("/home"); //$NON-NLS-1$
+ private static final File pf1 = f1.getParentFile();
+ private static final File pf2 = f2.getParentFile();
+ private static final URL u1, u2, u3, u4, u5, u6, u7, u8, u9, u10, u11, u13, u14, u15;
+ private static final URL pu1, pu2, pu3, pu7, pu13;
+ private static final String TEST_URL1 = "http://toto:titi@xxxxxxxxxxxxxxx/path/to/file.x.z.z?toto#frag"; //$NON-NLS-1$
+ private static final String JOIN_TEST_URL1 = "http://toto:titi@xxxxxxxxxxxxxxx/path/to/file.x.z.z/home/test.x.z.z?toto#frag"; //$NON-NLS-1$
+ private static final String PARENT_TEST_URL1 = "http://toto:titi@xxxxxxxxxxxxxxx/path/to/"; //$NON-NLS-1$
+ private static final String WOEXT_TEST_URL1 = "http://toto:titi@xxxxxxxxxxxxxxx/path/to/file.x.z?toto#frag"; //$NON-NLS-1$
+ private static final String REPEXT_TEST_URL1 = "http://toto:titi@xxxxxxxxxxxxxxx/path/to/file.x.z.toto?toto#frag"; //$NON-NLS-1$
+ private static final String TEST_URL2 = "jar:file:/home/test/j.jar!/org/arakhne/vmutil/file.x.z.z"; //$NON-NLS-1$
+ private static final String PARENT_TEST_URL2 = "jar:file:/home/test/j.jar!/org/arakhne/vmutil/"; //$NON-NLS-1$
+ private static final String JOIN_TEST_URL2 = "jar:file:/home/test/j.jar!/org/arakhne/vmutil/file.x.z.z/home/test.x.z.z"; //$NON-NLS-1$
+ private static final String WOEXT_TEST_URL2 = "jar:file:/home/test/j.jar!/org/arakhne/vmutil/file.x.z"; //$NON-NLS-1$
+ private static final String REPEXT_TEST_URL2 = "jar:file:/home/test/j.jar!/org/arakhne/vmutil/file.x.z.toto"; //$NON-NLS-1$
+ private static final String JARPART_TEST_URL2 = "file:/home/test/j.jar"; //$NON-NLS-1$
+ private static final File f3 = new File("/home/test/j.jar"); //$NON-NLS-1$
+ private static final File f4 = new File("/org/arakhne/vmutil/file.x.z.z"); //$NON-NLS-1$
+ private static final String TEST_URL3 = "jar:jar:http://www.arakhne.org/j.jar!/inner/myjar.jar!/org/arakhne/vmutil/file.x.z.z"; //$NON-NLS-1$
+ private static final String PARENT_TEST_URL3 = "jar:jar:http://www.arakhne.org/j.jar!/inner/myjar.jar!/org/arakhne/vmutil/"; //$NON-NLS-1$
+ private static final String JARPART_TEST_URL3 = "jar:http://www.arakhne.org/j.jar!/inner/myjar.jar"; //$NON-NLS-1$
+ private static final String JOIN_TEST_URL3 = "jar:jar:http://www.arakhne.org/j.jar!/inner/myjar.jar!/org/arakhne/vmutil/file.x.z.z/home/test.x.z.z"; //$NON-NLS-1$
+ static {
+ try {
+ u1 = f1.toURI().toURL();
+ u2 = f2.toURI().toURL();
+ u3 = new URL(TEST_URL1);
+ u4 = new URL(JOIN_TEST_URL1);
+ u5 = new URL(WOEXT_TEST_URL1);
+ u6 = new URL(REPEXT_TEST_URL1);
+ u7 = new URL(TEST_URL2);
+ u8 = new URL(JOIN_TEST_URL2);
+ u9 = new URL(WOEXT_TEST_URL2);
+ u10 = new URL(REPEXT_TEST_URL2);
+ u11 = new URL(JARPART_TEST_URL2);
+ u13 = new URL(TEST_URL3);
+ u14 = new URL(JARPART_TEST_URL3);
+ u15 = new URL(JOIN_TEST_URL3);
+ pu1 = pf1.toURI().toURL();
+ pu2 = pf2.toURI().toURL();
+ pu3 = new URL(PARENT_TEST_URL1);
+ pu7 = new URL(PARENT_TEST_URL2);
+ pu13 = new URL(PARENT_TEST_URL3);
+ }
+ catch(Throwable e) {
+ throw new AssertionFailedError(e.getLocalizedMessage());
+ }
+ }
+
/**
*/
- public void testDirname() {
- assertEquals("/home", FileSystem.dirname(f1.getAbsolutePath())); //$NON-NLS-1$
- assertEquals("/", FileSystem.dirname(f2.getAbsolutePath())); //$NON-NLS-1$
+ public void tetIsJarURLURL() {
+ assertFalse(FileSystem.isJarURL(u1));
+ assertFalse(FileSystem.isJarURL(u2));
+ assertFalse(FileSystem.isJarURL(u3));
+ assertTrue(FileSystem.isJarURL(u7));
+ assertTrue(FileSystem.isJarURL(u13));
}
+
+ /**
+ */
+ public void testGetJarURLURL() {
+ assertNull(FileSystem.getJarURL(u1));
+ assertNull(FileSystem.getJarURL(u2));
+ assertNull(FileSystem.getJarURL(u3));
+ assertEquals(u11, FileSystem.getJarURL(u7));
+ assertEquals(u14, FileSystem.getJarURL(u13));
+ }
/**
*/
+ public void testGetJarFileURL() {
+ assertNull(FileSystem.getJarFile(u1));
+ assertNull(FileSystem.getJarFile(u2));
+ assertNull(FileSystem.getJarFile(u3));
+ assertEquals(f4, FileSystem.getJarFile(u7));
+ assertEquals(f4, FileSystem.getJarFile(u13));
+ }
+
+ /**
+ * @throws MalformedURLException
+ */
+ public void testToJarURLFileFile() throws MalformedURLException {
+ assertEquals(u7, FileSystem.toJarURL(f3, f4));
+ }
+
+ /**
+ * @throws MalformedURLException
+ */
+ public void testToJarURLFileString() throws MalformedURLException {
+ assertEquals(u7, FileSystem.toJarURL(f3, f4.getPath()));
+ }
+
+ /**
+ * @throws MalformedURLException
+ */
+ public void testToJarURLURLFile() throws MalformedURLException {
+ assertEquals(u7, FileSystem.toJarURL(u11, f4));
+ }
+
+ /**
+ * @throws MalformedURLException
+ */
+ public void testToJarURLURLString() throws MalformedURLException {
+ assertEquals(u7, FileSystem.toJarURL(u11, f4.getPath()));
+ }
+
+ /**
+ */
+ public void testDirnameFile() {
+ assertEquals(pf1, FileSystem.dirname(f1));
+ assertEquals(pf2, FileSystem.dirname(f2));
+ }
+
+ /**
+ */
+ public void testDirnameURL() {
+ assertEquals(pu1, FileSystem.dirname(u1));
+ assertEquals(pu2, FileSystem.dirname(u2));
+ assertEquals(pu3, FileSystem.dirname(u3));
+ assertEquals(pu7, FileSystem.dirname(u7));
+ assertEquals(pu13, FileSystem.dirname(u13));
+ }
+
+ /**
+ */
public void testLargeBasenameString() {
assertEquals("test.x.z.z", FileSystem.largeBasename(f1.getAbsolutePath())); //$NON-NLS-1$
assertEquals("home", FileSystem.largeBasename(f2.getAbsolutePath())); //$NON-NLS-1$
@@ -57,6 +175,15 @@
/**
*/
+ public void testLargeBasenameURL() {
+ assertEquals("test.x.z.z", FileSystem.largeBasename(u1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.largeBasename(u2)); //$NON-NLS-1$
+ assertEquals("file.x.z.z", FileSystem.largeBasename(u3)); //$NON-NLS-1$
+ assertEquals("file.x.z.z", FileSystem.largeBasename(u7)); //$NON-NLS-1$
+ }
+
+ /**
+ */
public void testBasenameString() {
assertEquals("test.x.z", FileSystem.basename(f1.getAbsolutePath())); //$NON-NLS-1$
assertEquals("home", FileSystem.basename(f2.getAbsolutePath())); //$NON-NLS-1$
@@ -71,6 +198,15 @@
/**
*/
+ public void testBasenameURL() {
+ assertEquals("test.x.z", FileSystem.basename(u1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.basename(u2)); //$NON-NLS-1$
+ assertEquals("file.x.z", FileSystem.basename(u3)); //$NON-NLS-1$
+ assertEquals("file.x.z", FileSystem.basename(u7)); //$NON-NLS-1$
+ }
+
+ /**
+ */
public void testShortBasenameString() {
assertEquals("test", FileSystem.shortBasename(f1.getAbsolutePath())); //$NON-NLS-1$
assertEquals("home", FileSystem.shortBasename(f2.getAbsolutePath())); //$NON-NLS-1$
@@ -85,9 +221,11 @@
/**
*/
- public void testExtensionString() {
- assertEquals(".z", FileSystem.extension(f1.getAbsolutePath())); //$NON-NLS-1$
- assertEquals("", FileSystem.extension(f2.getAbsolutePath())); //$NON-NLS-1$
+ public void testShortBasenameURL() {
+ assertEquals("test", FileSystem.shortBasename(u1)); //$NON-NLS-1$
+ assertEquals("home", FileSystem.shortBasename(u2)); //$NON-NLS-1$
+ assertEquals("file", FileSystem.shortBasename(u3)); //$NON-NLS-1$
+ assertEquals("file", FileSystem.shortBasename(u7)); //$NON-NLS-1$
}
/**
@@ -99,9 +237,12 @@
/**
*/
- public void testExtensionsString() {
- assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(f1.getAbsolutePath()))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- assertTrue(Arrays.equals(new String[0], FileSystem.extensions(f2.getAbsolutePath())));
+ public void testExtensionURL() {
+ assertEquals(".z", FileSystem.extension(u1)); //$NON-NLS-1$
+ assertEquals("", FileSystem.extension(u2)); //$NON-NLS-1$
+ assertEquals(".z", FileSystem.extension(u3)); //$NON-NLS-1$
+ assertEquals(".z", FileSystem.extension(u7)); //$NON-NLS-1$
+ assertEquals(".z", FileSystem.extension(u13)); //$NON-NLS-1$
}
/**
@@ -113,45 +254,81 @@
/**
*/
- public void testSplitString() {
+ public void testExtensionsURL() {
+ assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(u1))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertTrue(Arrays.equals(new String[0], FileSystem.extensions(u2)));
+ assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(u3))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertTrue(Arrays.equals(new String[]{"x","z","z"}, FileSystem.extensions(u7))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ }
+
+ /**
+ */
+ public void testSplitFile() {
assertTrue(Arrays.equals(
new String[] {
"", //$NON-NLS-1$
"home", //$NON-NLS-1$
"test.x.z.z", //$NON-NLS-1$
},
- FileSystem.split(f1.getAbsolutePath())));
+ FileSystem.split(f1)));
assertTrue(Arrays.equals(
new String[] {
"", //$NON-NLS-1$
"home", //$NON-NLS-1$
},
- FileSystem.split(f2.getAbsolutePath())));
+ FileSystem.split(f2)));
}
/**
*/
- public void testSplitFile() {
+ public void testSplitURL() {
+ String[] tab;
+
+ tab = FileSystem.split(u1);
assertTrue(Arrays.equals(
new String[] {
"", //$NON-NLS-1$
"home", //$NON-NLS-1$
"test.x.z.z", //$NON-NLS-1$
},
- FileSystem.split(f1)));
+ tab));
+
+ tab = FileSystem.split(u2);
assertTrue(Arrays.equals(
new String[] {
"", //$NON-NLS-1$
"home", //$NON-NLS-1$
},
- FileSystem.split(f2)));
+ tab));
+
+ tab = FileSystem.split(u7);
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "org", //$NON-NLS-1$
+ "arakhne", //$NON-NLS-1$
+ "vmutil", //$NON-NLS-1$
+ "file.x.z.z", //$NON-NLS-1$
+ },
+ tab));
+
+ tab = FileSystem.split(u13);
+ assertTrue(Arrays.equals(
+ new String[] {
+ "", //$NON-NLS-1$
+ "org", //$NON-NLS-1$
+ "arakhne", //$NON-NLS-1$
+ "vmutil", //$NON-NLS-1$
+ "file.x.z.z", //$NON-NLS-1$
+ },
+ tab));
}
/**
*/
- public void testJoin() {
- assertEquals(f1.getAbsolutePath(),
- FileSystem.join(
+ public void testJoinFileStringArray() {
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z"), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(f1,
"", //$NON-NLS-1$
"home", //$NON-NLS-1$
"test.x.z.z")); //$NON-NLS-1$
@@ -159,59 +336,143 @@
/**
*/
- public void testHasExtensionStringString() {
- /*assertTrue(FileSystem.hasExtension(f1.getAbsolutePath(), ".z")); //$NON-NLS-1$
- assertTrue(FileSystem.hasExtension(f1.getAbsolutePath(), "z")); //$NON-NLS-1$
- assertFalse(FileSystem.hasExtension(f1.getAbsolutePath(), ".x")); //$NON-NLS-1$
- assertFalse(FileSystem.hasExtension(f1.getAbsolutePath(), "")); //$NON-NLS-1$
- assertTrue(FileSystem.hasExtension(f2.getAbsolutePath(), "")); //$NON-NLS-1$
- assertFalse(FileSystem.hasExtension(f2.getAbsolutePath(), ".z")); //$NON-NLS-1$
- */
+ public void testJoinFileFileArray() {
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z"), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(f1,
+ new File("home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z"), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(f1,
+ new File(File.separator+"home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
}
/**
+ * @throws Exception
*/
+ public void testJoinURLStringArray() throws Exception {
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z").toURI().toURL(), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(u1,
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z")); //$NON-NLS-1$
+
+ assertEquals(u4,
+ FileSystem.join(u3,
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z")); //$NON-NLS-1$
+
+ assertEquals(u8,
+ FileSystem.join(u7,
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z")); //$NON-NLS-1$
+
+ assertEquals(u15,
+ FileSystem.join(u13,
+ "", //$NON-NLS-1$
+ "home", //$NON-NLS-1$
+ "test.x.z.z")); //$NON-NLS-1$
+ }
+
+ /**
+ * @throws Exception
+ */
+ public void testJoinURLFileArray() throws Exception {
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z").toURI().toURL(), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(u1,
+ new File("home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ assertEquals(new File(new File(f1, "home"), "test.x.z.z").toURI().toURL(), //$NON-NLS-1$ //$NON-NLS-2$
+ FileSystem.join(u1,
+ new File(File.separator+"home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ assertEquals(u4,
+ FileSystem.join(u3,
+ new File(File.separator+"home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ assertEquals(u8,
+ FileSystem.join(u7,
+ new File(File.separator+"home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ assertEquals(u15,
+ FileSystem.join(u13,
+ new File(File.separator+"home"), //$NON-NLS-1$
+ new File("test.x.z.z"))); //$NON-NLS-1$
+ }
+
+ /**
+ */
public void testHasExtensionFileString() {
- /*assertTrue(FileSystem.hasExtension(f1, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(f1, ".z")); //$NON-NLS-1$
assertTrue(FileSystem.hasExtension(f1, "z")); //$NON-NLS-1$
assertFalse(FileSystem.hasExtension(f1, ".x")); //$NON-NLS-1$
assertFalse(FileSystem.hasExtension(f1, "")); //$NON-NLS-1$
assertTrue(FileSystem.hasExtension(f2, "")); //$NON-NLS-1$
assertFalse(FileSystem.hasExtension(f2, ".z")); //$NON-NLS-1$
- */
}
/**
*/
- public void testRemoveExtensionString() {
- assertEquals("/home/test.x.z", FileSystem.removeExtension(f1.getAbsolutePath())); //$NON-NLS-1$
- assertEquals("/home", FileSystem.removeExtension(f2.getAbsolutePath())); //$NON-NLS-1$
+ public void testHasExtensionURLString() {
+ assertTrue(FileSystem.hasExtension(u1, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u1, "z")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u1, ".x")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u1, "")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u2, "")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u2, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u3, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u3, "z")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u3, ".x")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u3, "")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u7, ".z")); //$NON-NLS-1$
+ assertTrue(FileSystem.hasExtension(u7, "z")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u7, ".x")); //$NON-NLS-1$
+ assertFalse(FileSystem.hasExtension(u7, "")); //$NON-NLS-1$
}
/**
*/
public void testRemoveExtensionFile() {
- assertEquals("/home/test.x.z", FileSystem.removeExtension(f1)); //$NON-NLS-1$
- assertEquals("/home", FileSystem.removeExtension(f2)); //$NON-NLS-1$
+ assertEquals(new File("/home/test.x.z"), FileSystem.removeExtension(f1)); //$NON-NLS-1$
+ assertEquals(new File("/home"), FileSystem.removeExtension(f2)); //$NON-NLS-1$
}
/**
+ * @throws Exception
*/
- public void testReplaceExtensionStringString() {
- assertEquals("/home/test.x.z.toto", FileSystem.replaceExtension(f1.getAbsolutePath(), ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
- assertEquals("/home", FileSystem.replaceExtension(f2.getAbsolutePath(), ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ public void testRemoveExtensionURL() throws Exception {
+ assertEquals(new File("/home/test.x.z").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.removeExtension(u1));
+ assertEquals(new File("/home").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.removeExtension(u2));
+ assertEquals(u5, FileSystem.removeExtension(u3));
+ assertEquals(u9, FileSystem.removeExtension(u7));
}
/**
*/
public void testReplaceExtensionFileString() {
assertEquals(new File("/home/test.x.z.toto"), FileSystem.replaceExtension(f1, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
- assertEquals(new File("/home"), FileSystem.replaceExtension(f2, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(new File("/home.toto"), FileSystem.replaceExtension(f2, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* @throws Exception
*/
+ public void testReplaceExtensionURLString() throws Exception {
+ assertEquals(new File("/home/test.x.z.toto").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.replaceExtension(u1, ".toto")); //$NON-NLS-1$
+ assertEquals(new File("/home.toto").toURI().toURL(), //$NON-NLS-1$
+ FileSystem.replaceExtension(u2, ".toto")); //$NON-NLS-1$
+ assertEquals(u6, FileSystem.replaceExtension(u3, ".toto")); //$NON-NLS-1$
+ assertEquals(u10, FileSystem.replaceExtension(u7, ".toto")); //$NON-NLS-1$
+ }
+
+ /**
+ * @throws Exception
+ */
public void testConvertStringToUrl() throws Exception {
assertNull(FileSystem.convertStringToUrl(null, true));
assertNull(FileSystem.convertStringToUrl("", true)); //$NON-NLS-1$