ReadyTalk / avian

[INACTIVE] Avian is a lightweight virtual machine and class library designed to provide a useful subset of Java's features, suitable for building self-contained applications.
https://readytalk.github.io/avian/
Other
1.22k stars 172 forks source link

FileURLConnection not working on Windows #452

Closed keinhaar closed 9 years ago

keinhaar commented 9 years ago

I was trying to open an File URL like "file:///c:/temp/xy.txt" on Windows, and got an IOException. After looking into avians FileURLConnection i thought the error was here, but testing somewhat further showed that the File class is also buggy. Exactly in its normalize method. It does not remove the "/" from the beginning, which is done by Oracle JDK when creating a File like this: new File("/c:/temp/xy.txt")

To fix both bugs i changed avian.file.Handler getInputStream() to look like this.

    public InputStream getInputStream() throws IOException {
      return new FileInputStream(new File(url.getFile()));
    }

and java.io.File normalize(String path) to look like this:

  private static String normalize(String path) {
     if(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);
  }

I think this should not break any other Operating system. Please include this fix in further releases... Thanks for this great software...