ignore leading / for files on Windows (fixes #452)

This commit is contained in:
Joshua Warner 2015-09-02 08:31:30 -06:00
parent 9f70aa753e
commit e2f2ed68f0
3 changed files with 21 additions and 1 deletions

View File

@ -34,7 +34,7 @@ public class Handler extends URLStreamHandler {
}
public InputStream getInputStream() throws IOException {
return new FileInputStream(url.getFile());
return new FileInputStream(new File(url.getFile()));
}
public void connect() {

View File

@ -14,6 +14,9 @@ public class File implements Serializable {
private static final String FileSeparator
= System.getProperty("file.separator");
private static final boolean IsWindows
= System.getProperty("os.name").equals("Windows");
public static final String separator = FileSeparator;
public static final char separatorChar = FileSeparator.charAt(0);
@ -89,6 +92,14 @@ public class File implements Serializable {
}
private static String normalize(String path) {
if(IsWindows
&& path.length() > 2
&& path.charAt(0) == '/'
&& path.charAt(2) == ':')
{
// remove a leading slash on Windows
path = path.substring(1);
}
return stripSeparators
("\\".equals(FileSeparator) ? path.replace('/', '\\') : path);
}

View File

@ -3,6 +3,9 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Files {
private static final boolean IsWindows
= System.getProperty("os.name").equals("Windows");
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
@ -81,6 +84,12 @@ public class Files {
}
}
if(IsWindows) {
expect(new File("/c:\\test").getPath().equals("c:\\test"));
} else {
expect(new File("/c:\\test").getPath().equals("/c:\\test"));
}
expect(new File("foo/bar").getParent().equals("foo"));
expect(new File("foo/bar/").getParent().equals("foo"));
expect(new File("foo/bar//").getParent().equals("foo"));