[Arakhnę-Dev] [191] * Bug fix: FileSystem should encode/ decode URL to maintain a valid value.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 191
Author:   galland
Date:     2010-10-04 14:34:16 +0200 (Mon, 04 Oct 2010)
Log Message:
-----------
* Bug fix: FileSystem should encode/decode URL to maintain a valid value.

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	2010-10-01 17:29:53 UTC (rev 190)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-10-04 12:34:16 UTC (rev 191)
@@ -26,14 +26,17 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLDecoder;
 import java.nio.channels.Channels;
 import java.nio.channels.FileChannel;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.charset.Charset;
 import java.util.Iterator;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -314,6 +317,9 @@
 			if (prefix!=null) {
 				return toJarURL(prefix, path);
 			}
+			if (path!=null) {
+				path = URLDecoder.decode(path, Charset.defaultCharset().displayName());
+			}
 			URI uri = new URI(
 					filename.getProtocol(), 
 					filename.getUserInfo(), 
@@ -730,8 +736,8 @@
 					urlBase.getUserInfo(), 
 					urlBase.getHost(), 
 					urlBase.getPort(), 
-					buf.toString(),
-					urlBase.getQuery(),
+					URLDecoder.decode(buf.toString(), Charset.defaultCharset().displayName()),
+					URLDecoder.decode(urlBase.getQuery(), Charset.defaultCharset().displayName()),
 					urlBase.getRef());
 			return uri.toURL();
 		}
@@ -779,8 +785,8 @@
 					urlBase.getUserInfo(), 
 					urlBase.getHost(), 
 					urlBase.getPort(), 
-					buf.toString(),
-					urlBase.getQuery(),
+					URLDecoder.decode(buf.toString(), Charset.defaultCharset().displayName()),
+					URLDecoder.decode(urlBase.getQuery(), Charset.defaultCharset().displayName()),
 					urlBase.getRef());
 			return uri.toURL();
 		}
@@ -881,8 +887,8 @@
 					filename.getUserInfo(), 
 					filename.getHost(), 
 					filename.getPort(), 
-					buf.toString(),
-					filename.getQuery(),
+					URLDecoder.decode(buf.toString(), Charset.defaultCharset().displayName()),
+					URLDecoder.decode(filename.getQuery(), Charset.defaultCharset().displayName()),
 					filename.getRef());
 			return uri.toURL();
 		}
@@ -961,8 +967,8 @@
 					filename.getUserInfo(), 
 					filename.getHost(), 
 					filename.getPort(), 
-					buf.toString(),
-					filename.getQuery(),
+					URLDecoder.decode(buf.toString(), Charset.defaultCharset().displayName()),
+					URLDecoder.decode(filename.getQuery(), Charset.defaultCharset().displayName()),
 					filename.getRef());
 			return uri.toURL();
 		}
@@ -1306,9 +1312,15 @@
 			// constructors:
 			try {
 				uri = new URI(url.getProtocol(), url.getUserInfo(), url
-						.getHost(), url.getPort(), url.getPath(), url
-						.getQuery(), url.getRef());
+						.getHost(), url.getPort(), 
+						URLDecoder.decode(url.getPath(), Charset.defaultCharset().displayName()),
+						URLDecoder.decode(url.getQuery(), Charset.defaultCharset().displayName()),
+						url.getRef());
 			}
+			catch(UnsupportedEncodingException e1) {
+				// The URL is broken beyond automatic repair
+				throw new IllegalArgumentException("broken URL: " + url); //$NON-NLS-1$
+			}
 			catch (URISyntaxException e1) {
 				// The URL is broken beyond automatic repair
 				throw new IllegalArgumentException("broken URL: " + url); //$NON-NLS-1$

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-10-01 17:29:53 UTC (rev 190)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-10-04 12:34:16 UTC (rev 191)
@@ -301,6 +301,9 @@
 		assertEquals(new URL("file", "", "/"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 				FileSystem.dirname(new URL("file:///a/"))); //$NON-NLS-1$
 		assertNull(FileSystem.dirname(new URL("file://"))); //$NON-NLS-1$
+		
+		assertEquals(new URL("file", "", "/a/b%20c/"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+				FileSystem.dirname(new File("/a/b c/d.txt").toURI().toURL())); //$NON-NLS-1$
 	}
 
 	/**


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/