6419701: DefaultFileManager clean up: URI.create
authorjjg
Tue, 08 Sep 2009 11:12:13 -0700
changeset 3782 ae62279eeb46
parent 3781 cad98ced28c5
child 3783 f1aefacd7231
6419701: DefaultFileManager clean up: URI.create 6483788: DefaultFileManager.ZipFileObject.toUri() fails to escape space characters 6501502: JSR 199: FileObject.toUri should return file:///c:/ or file:/c:/ not file://c:/ 6877206: JavaFileObject.toUri returns bogus URI (win) 6877223: tests @ignored because of issues with File.toURI on Windows Reviewed-by: mcimadamore, alanb
langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java
langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
langtools/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java
langtools/src/share/classes/com/sun/tools/javac/file/SymbolArchive.java
langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java
langtools/src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java
langtools/test/tools/javac/Diagnostics/6769027/tester.properties
langtools/test/tools/javac/api/6440333/T6440333.java
langtools/test/tools/javac/api/Sibling.java
langtools/test/tools/javac/api/T6483788.java
langtools/test/tools/javac/api/T6501502.java
langtools/test/tools/javac/api/T6877206.java
--- a/langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java	Tue Sep 08 11:12:13 2009 -0700
@@ -29,6 +29,8 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.charset.CharsetDecoder;
 import javax.lang.model.element.Modifier;
 import javax.lang.model.element.NestingKind;
@@ -93,7 +95,26 @@
         return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
     }
 
+    protected static URI createJarUri(File jarFile, String entryName) {
+        URI jarURI = jarFile.toURI().normalize();
+        String separator = entryName.startsWith("/") ? "!" : "!/";
+        try {
+            // The jar URI convention appears to be not to re-encode the jarURI
+            return new URI("jar:" + jarURI + separator + entryName);
+        } catch (URISyntaxException e) {
+            throw new CannotCreateUriError(jarURI + separator + entryName, e);
+        }
+    }
+
+    /** Used when URLSyntaxException is thrown unexpectedly during
+     *  implementations of (Base)FileObject.toURI(). */
+    protected static class CannotCreateUriError extends Error {
+        private static final long serialVersionUID = 9101708840997613546L;
+        public CannotCreateUriError(String value, Throwable cause) {
+            super(value, cause);
+        }
+    }
+
     /** The file manager that created this JavaFileObject. */
     protected final JavacFileManager fileManager;
-
 }
--- a/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Sep 08 11:12:13 2009 -0700
@@ -26,6 +26,7 @@
 package com.sun.tools.javac.file;
 
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -36,6 +37,7 @@
 import java.lang.reflect.Constructor;
 import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.nio.ByteBuffer;
@@ -77,7 +79,6 @@
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.Options;
 
-import java.io.Closeable;
 import static javax.tools.StandardLocation.*;
 import static com.sun.tools.javac.main.OptionName.*;
 
@@ -437,6 +438,7 @@
             return Collections.emptySet();
         }
 
+        @Override
         public String toString() {
             return "MissingArchive[" + zipFileName + "]";
         }
@@ -654,10 +656,10 @@
     private final ByteBufferCache byteBufferCache;
 
     CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
-        Charset charset = (this.charset == null)
+        Charset cs = (this.charset == null)
             ? Charset.forName(encodingName)
             : this.charset;
-        CharsetDecoder decoder = charset.newDecoder();
+        CharsetDecoder decoder = cs.newDecoder();
 
         CodingErrorAction action;
         if (ignoreEncodingErrors)
@@ -892,7 +894,7 @@
         nullCheck(location);
         // validatePackageName(packageName);
         nullCheck(packageName);
-        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
+        if (!isRelativeUri(relativeName))
             throw new IllegalArgumentException("Invalid relative name: " + relativeName);
         RelativeFile name = packageName.length() == 0
             ? new RelativeFile(relativeName)
@@ -946,7 +948,7 @@
         nullCheck(location);
         // validatePackageName(packageName);
         nullCheck(packageName);
-        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
+        if (!isRelativeUri(relativeName))
             throw new IllegalArgumentException("relativeName is invalid");
         RelativeFile name = packageName.length() == 0
             ? new RelativeFile(relativeName)
@@ -1085,6 +1087,15 @@
         return first != '.' && first != '/';
     }
 
+    // Convenience method
+    protected static boolean isRelativeUri(String u) {
+        try {
+            return isRelativeUri(new URI(u));
+        } catch (URISyntaxException e) {
+            return false;
+        }
+    }
+
     /**
      * Converts a relative file name to a relative URI.  This is
      * different from File.toURI as this method does not canonicalize
@@ -1099,7 +1110,7 @@
     public static String getRelativeName(File file) {
         if (!file.isAbsolute()) {
             String result = file.getPath().replace(File.separatorChar, '/');
-            if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701
+            if (isRelativeUri(result))
                 return result;
         }
         throw new IllegalArgumentException("Invalid relative path: " + file);
--- a/langtools/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Tue Sep 08 11:12:13 2009 -0700
@@ -34,7 +34,6 @@
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.CharsetDecoder;
@@ -73,6 +72,7 @@
         return new FileInputStream(f);
     }
 
+    @Override
     protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
         return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
     }
@@ -147,6 +147,7 @@
     }
 
     @Deprecated
+    @Override
     public String getPath() {
         return f.getPath();
     }
@@ -201,11 +202,6 @@
     }
 
     public URI toUri() {
-        try {
-            String path = f.getAbsolutePath().replace(File.separatorChar, '/');
-            return new URI("file://" + path).normalize();
-        } catch (URISyntaxException ex) {
-            return f.toURI();
-        }
+        return f.toURI().normalize();
     }
 }
--- a/langtools/src/share/classes/com/sun/tools/javac/file/SymbolArchive.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/SymbolArchive.java	Tue Sep 08 11:12:13 2009 -0700
@@ -73,12 +73,14 @@
         map.put(dirname, list);
     }
 
+    @Override
     public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
         RelativeDirectory prefix_subdir = new RelativeDirectory(prefix, subdirectory.path);
         ZipEntry ze = new RelativeFile(prefix_subdir, file).getZipEntry(zdir);
         return new SymbolFileObject(this, file, ze);
     }
 
+    @Override
     public String toString() {
         return "SymbolArchive[" + zdir.getName() + "]";
     }
--- a/langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Tue Sep 08 11:12:13 2009 -0700
@@ -122,6 +122,7 @@
         zdir.close();
     }
 
+    @Override
     public String toString() {
         return "ZipArchive[" + zdir.getName() + "]";
     }
@@ -154,6 +155,7 @@
             throw new UnsupportedOperationException();
         }
 
+        @Override
         protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
             return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
         }
@@ -177,6 +179,7 @@
         }
 
         @Deprecated
+        @Override
         public String getPath() {
             return zarch.zdir.getName() + "(" + entry + ")";
         }
@@ -235,9 +238,8 @@
         }
 
         public URI toUri() {
-            String zipName = new File(getZipName()).toURI().normalize().getPath();
-            String entryName = getZipEntryName();
-            return URI.create("jar:" + zipName + "!" + entryName);
+            File zipFile = new File(getZipName());
+            return createJarUri(zipFile, entry.getName());
         }
 
         @Override
--- a/langtools/src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java	Tue Sep 08 11:12:13 2009 -0700
@@ -72,7 +72,7 @@
     public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
         RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
         ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
-        JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
+        JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
         return ret;
     }
 
@@ -84,6 +84,7 @@
         zfIndex.close();
     }
 
+    @Override
     public String toString() {
         return "ZipFileIndexArchive[" + zfIndex + "]";
     }
@@ -111,10 +112,10 @@
 
         /** The name of the zip file where this entry resides.
          */
-        String zipName;
+        File zipName;
 
 
-        ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) {
+        ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) {
             super(fileManager);
             this.name = entry.getFileName();
             this.zfIndex = zfIndex;
@@ -130,6 +131,7 @@
             return inputStream;
         }
 
+        @Override
         protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
             return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
         }
@@ -157,6 +159,7 @@
 
         /** @deprecated see bug 6410637 */
         @Deprecated
+        @Override
         public String getPath() {
             return zipName + "(" + entry.getName() + ")";
         }
@@ -183,7 +186,7 @@
         }
 
         public String getZipName() {
-            return zipName;
+            return zipName.getPath();
         }
 
         public String getZipEntryName() {
@@ -191,9 +194,10 @@
         }
 
         public URI toUri() {
-            String zipName = new File(getZipName()).toURI().normalize().getPath();
-            String entryName = getZipEntryName();
-            return URI.create("jar:" + zipName + "!" + entryName);
+            if (zfIndex.symbolFilePrefix != null)
+                return createJarUri(zipName, zfIndex.symbolFilePrefix.path + entry.getName());
+            else
+                return createJarUri(zipName, entry.getName());
         }
 
         private byte[] read() throws IOException {
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Sep 08 11:12:13 2009 -0700
@@ -27,6 +27,7 @@
 
 import java.io.*;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.CharBuffer;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -2614,7 +2615,11 @@
         }
 
         public URI toUri() {
-            return URI.create(name.toString());
+            try {
+                return new URI(null, name.toString(), null);
+            } catch (URISyntaxException e) {
+                throw new CannotCreateUriError(name.toString(), e);
+            }
         }
 
         @Override
--- a/langtools/src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java	Tue Sep 08 11:12:13 2009 -0700
@@ -297,7 +297,7 @@
                         po instanceof SourcePositionImpl) {
                     URI uri = ((SourcePositionImpl) po).filename.toUri();
                     if ("file".equals(uri.getScheme())) {
-                        File f = new File(uri.getPath());
+                        File f = new File(uri);
                         File dir = f.getParentFile();
                         if (dir != null) {
                             File pf = new File(dir, "package.html");
--- a/langtools/test/tools/javac/Diagnostics/6769027/tester.properties	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/test/tools/javac/Diagnostics/6769027/tester.properties	Tue Sep 08 11:12:13 2009 -0700
@@ -3,11 +3,11 @@
 
 compiler.err.double=\
     This is a test error message.\n\
-	This is another line of the above error message {0}
+        This is another line of the above error message {0}
 
 compiler.misc.single=\
     This is a test subdiagnostic {0}
 
 compiler.misc.double=\
     This is a test subdiagnostic.\n\
-	This is another line of the above subdiagnostic {0}
+        This is another line of the above subdiagnostic {0}
--- a/langtools/test/tools/javac/api/6440333/T6440333.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/test/tools/javac/api/6440333/T6440333.java	Tue Sep 08 11:12:13 2009 -0700
@@ -26,7 +26,6 @@
  * @bug     6440333
  * @summary SimpleJavaFileObject.toString() generates URI with some extra message
  * @author  Peter von der Ah\u00e9
- * @ignore 6877223 test ignored because of issues with File.toUri on Windows (6877206)
  * @library ../lib
  * @compile T6440333.java
  * @run main T6440333
@@ -34,6 +33,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URI;
 import javax.tools.JavaFileObject;
 
 public class T6440333 extends ToolTester {
@@ -41,14 +41,10 @@
         File path = test_src.getCanonicalFile();
         File src = new File(new File(path, "."), "T6440333.java");
         JavaFileObject fo = fm.getJavaFileObjects(src).iterator().next();
-        String expect = src.getCanonicalFile().getPath().replace(File.separatorChar, '/');
+        URI expect = src.getCanonicalFile().toURI();
         System.err.println("Expect " + expect);
-        // CURRENTLY, the following line fails on Windows because a file C:/w/jjg/...
-        // returns a URI file://C/w/jjg... which incorrectly encodes the drive letter
-        // in the URI authority.   This is against the spec that the authority is
-        // undefined and breaks the contract that new File(f.toURI()).equals(f.getAbsoluteFile())
-        System.err.println("Got: " +  fo.toUri().getPath());
-        if (!expect.equals(fo.toUri().getPath())) {
+        System.err.println("Found  " + fo.toUri());
+        if (!expect.equals(fo.toUri())) {
             throw new AssertionError();
         }
     }
--- a/langtools/test/tools/javac/api/Sibling.java	Thu Sep 03 18:34:17 2009 -0700
+++ b/langtools/test/tools/javac/api/Sibling.java	Tue Sep 08 11:12:13 2009 -0700
@@ -26,7 +26,6 @@
  * @bug     6399602
  * @summary Verify that files are created relative to sibling
  * @author  Peter von der Ah\u00e9
- * @ignore 6877223 test ignored because of issues with File.toUri on Windows (6877206)
  */
 
 import java.io.File;
@@ -48,10 +47,9 @@
                                                             "foo.bar.baz.Test",
                                                             CLASS,
                                                             sibling);
-        String name =
-            new File("Test.class").getAbsolutePath().replace(File.separatorChar, '/');
-        if (!classFile.toUri().getPath().equals(name))
-            throw new AssertionError("Expected " + name + ", got " +
-                                     classFile.toUri().getPath());
+        File file = new File("Test.class").getAbsoluteFile();
+        if (!classFile.toUri().equals(file.toURI()))
+            throw new AssertionError("Expected " + file.toURI() + ", got " +
+                                     classFile.toUri());
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/T6483788.java	Tue Sep 08 11:12:13 2009 -0700
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6483788
+ * @summary DefaultFileManager.ZipFileObject.toUri() fails to escape space characters
+ */
+
+import java.io.*;
+import java.net.*;
+import java.util.Collections;
+import java.util.jar.*;
+import java.util.zip.*;
+import javax.tools.*;
+
+public class T6483788 {
+    public static void main(String[] args) throws Exception {
+        new T6483788().run();
+    }
+
+    void run() throws Exception {
+        File jar = createJar();
+        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
+        fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(jar));
+        JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.CLASS_PATH, "dummy", JavaFileObject.Kind.CLASS);
+        System.err.println("file: " + fo);
+        URI uri = fo.toUri();
+        System.err.println("uri: " + uri);
+        if (uri.toString().contains(" "))
+            throw new Exception("unexpected space character found");
+    }
+
+    File createJar() throws IOException {
+        byte[] dummy_data = new byte[10];
+        File f = new File("a b.jar");
+        OutputStream out = new FileOutputStream(f);
+        try {
+            JarOutputStream jar = new JarOutputStream(out);
+            jar.putNextEntry(new ZipEntry("dummy.class"));
+            jar.write(dummy_data);
+            jar.close();
+        } finally {
+            out.close();
+        }
+        return f;
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/T6501502.java	Tue Sep 08 11:12:13 2009 -0700
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6501502 6877206 6483788
+ * @summary JSR 199: FileObject.toUri should return file:///c:/ or file:/c:/ not file://c:/
+ */
+
+import java.io.*;
+import java.net.URI;
+import javax.tools.*;
+
+public class T6501502 {
+    public static void main(String... args) throws Exception {
+        new T6501502().run();
+    }
+
+    // The spec for java.io.File includes the following:
+    //      For a given abstract pathname f it is guaranteed that
+    //          new File( f.toURI()).equals( f.getAbsoluteFile())
+    // For JavaFileObject we test as follows:
+    //      new File( CONVERT_TO_FILEOBJECT(f).toURI()).equals( f.getAbsoluteFile())
+    // to verify that we get reasonable URIs returned from toURI.
+    // To make this a general test, and not just a Windows test,
+    // we test a number of platform-independent paths.
+    void run() throws Exception {
+        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
+        fm = c.getStandardFileManager(null, null, null);
+        System.err.println(System.getProperties());
+        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
+        File testSrcDir = new File(System.getProperty("test.src"));
+        File testClassesDir = new File(System.getProperty("test.classes"));
+        test(new File("abc.tmp"));
+        test(new File(tmpDir, "bad.file"));
+        test(new File(testSrcDir, "T6501501.java"));
+        test(new File(testClassesDir, "T6501501.class"));
+        test(new File("a b"));
+    }
+
+    void test(File f) throws Exception {
+        System.err.println("test " + f);
+        FileObject fo = fm.getJavaFileObjects(f).iterator().next();
+        URI uri = fo.toUri();
+        System.err.println("FileObject uri: " + uri);
+        if (!new File(uri).equals(f.getAbsoluteFile()))
+            throw new Exception("unexpected URI returned");
+    }
+
+    StandardJavaFileManager fm;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/T6877206.java	Tue Sep 08 11:12:13 2009 -0700
@@ -0,0 +1,263 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6877206
+ * @summary JavaFileObject.toUri returns bogus URI (win)
+ */
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.jar.*;
+import java.util.zip.*;
+import javax.tools.*;
+
+import com.sun.tools.javac.file.JavacFileManager;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.Options;
+
+// Test URIs returned from JavacFileManager and its support classes.
+// For a variety of file objects, verify the validity of FileObject.toUri()
+// by verifying the URI exists and points to the same contents as the file
+// object itself
+
+public class T6877206 {
+    public static void main(String... args) throws Exception {
+        new T6877206().run();
+    }
+
+    Set<String> foundClasses = new TreeSet<String>();
+    Set<String> foundJars = new TreeSet<String>();
+
+    void run() throws Exception {
+        File rt_jar = findRtJar();
+
+        // names for entries to be created in directories and jar files
+        String[] entries = { "p/A.class", "p/resources/A-1.jpg" };
+
+        // test various combinations of directories and jar files, intended to
+        // cover all sources of URIs within JavacFileManager's support classes
+
+        test(createFileManager(), createDir("dir", entries), "p", entries.length);
+        test(createFileManager(), createDir("a b/dir", entries), "p", entries.length);
+
+        for (boolean useJavaUtilZip: new boolean[] { false, true }) {
+            test(createFileManager(useJavaUtilZip), createJar("jar", entries), "p", entries.length);
+            test(createFileManager(useJavaUtilZip), createJar("jar jar", entries), "p", entries.length);
+
+            for (boolean useSymbolFile: new boolean[] { false, true }) {
+                test(createFileManager(useJavaUtilZip, useSymbolFile), rt_jar, "java.lang.ref", -1);
+            }
+        }
+
+        // Verify that we hit all the impl classes we intended
+        checkCoverage("classes", foundClasses,
+                "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject");
+
+        // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar
+        checkCoverage("jar files", foundJars,
+                "ct.sym", "jar", "jar jar", "rt.jar");
+    }
+
+    // use a new file manager for each test
+    void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception {
+        JarURLConnection c;
+        System.err.println("Test " + f);
+        try {
+            fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
+
+            int count = 0;
+            for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH,
+                    pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
+                System.err.println("checking " + fo);
+                // record the file object class name for coverage checks later
+                foundClasses.add(fo.getClass().getSimpleName());
+                testFileObject(fo);
+                count++;
+            }
+
+            if (expect > 0 && count != expect)
+                throw new Exception("wrong number of entries found: "
+                        + count + ", expected " + expect);
+        } finally {
+            fm.close();
+        }
+    }
+
+    void testFileObject(JavaFileObject fo) throws Exception {
+        // test the validity of the result of toUri() by using URLConnection
+        // and comparing the results of reading from the connection with the
+        // result of reading from the file object directly.
+        URI uri = fo.toUri();
+        System.err.println("uri: " + uri);
+
+        URLConnection urlconn = uri.toURL().openConnection();
+        if (urlconn instanceof JarURLConnection) {
+            JarURLConnection jarconn = (JarURLConnection) urlconn;
+            File f = new File(jarconn.getJarFile().getName());
+            // record access to the jar file for coverage checks later
+            foundJars.add(f.getName());
+        }
+
+        try {
+            byte[] uriData = read(urlconn.getInputStream());
+            byte[] foData = read(fo.openInputStream());
+            if (!Arrays.equals(uriData, foData)) {
+                if (uriData.length != foData.length)
+                    throw new Exception("data size differs: uri data "
+                            + uriData.length + " bytes, fo data " + foData.length+ " bytes");
+                for (int i = 0; i < uriData.length; i++) {
+                    if (uriData[i] != foData[i])
+                    throw new Exception("unexpected data returned at offset " + i
+                            + ", uri data " + uriData[i] + ", fo data " + foData[i]);
+                }
+                throw new AssertionError("cannot find difference");
+            }
+        } finally {
+            // In principle, simply closing the result of urlconn.getInputStream()
+            // should have been sufficient. But the internal JarURLConnection
+            // does not close the JarFile in an expeditious manner, thus preventing
+            // jtreg from deleting the jar file before starting the next test.
+            // Therefore we force access to the JarURLConnection to close the
+            // JarFile when necessary.
+            if (urlconn instanceof JarURLConnection) {
+                JarURLConnection jarconn = (JarURLConnection) urlconn;
+                jarconn.getJarFile().close();
+            }
+        }
+    }
+
+    void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
+        Set<String> e = new TreeSet<String>(Arrays.asList(expect));
+        if (!found.equals(e)) {
+            e.removeAll(found);
+            throw new Exception("expected " + label + " not used: " + e);
+        }
+    }
+
+    JavacFileManager createFileManager() {
+        return createFileManager(false, false);
+    }
+
+    JavacFileManager createFileManager(boolean useJavaUtilZip) {
+        return createFileManager(useJavaUtilZip, false);
+    }
+
+    JavacFileManager createFileManager(boolean useJavaUtilZip, boolean useSymbolFile) {
+        // javac should really not be using system properties like this
+        // -- it should really be using (hidden) options -- but until then
+        // take care to leave system properties as we find them, so as not
+        // to adversely affect other tests that might follow.
+        String prev = System.getProperty("useJavaUtilZip");
+        boolean resetProperties = false;
+        try {
+            if (useJavaUtilZip) {
+                System.setProperty("useJavaUtilZip", "true");
+                resetProperties = true;
+            } else if (System.getProperty("useJavaUtilZip") != null) {
+                System.getProperties().remove("useJavaUtilZip");
+                resetProperties = true;
+            }
+
+            Context c = new Context();
+            if (!useSymbolFile) {
+                Options options = Options.instance(c);
+                options.put("ignore.symbol.file", "true");
+            }
+
+            return new JavacFileManager(c, false, null);
+        } finally {
+            if (resetProperties) {
+                if (prev == null) {
+                    System.getProperties().remove("useJavaUtilZip");
+                } else {
+                    System.setProperty("useJavaUtilZip", prev);
+                }
+            }
+        }
+    }
+
+    File createDir(String name, String... entries) throws Exception {
+        File dir = new File(name);
+        if (!dir.mkdirs())
+            throw new Exception("cannot create directories " + dir);
+        for (String e: entries) {
+            writeFile(new File(dir, e), e);
+        }
+        return dir;
+    }
+
+    File createJar(String name, String... entries) throws IOException {
+        File jar = new File(name);
+        OutputStream out = new FileOutputStream(jar);
+        try {
+            JarOutputStream jos = new JarOutputStream(out);
+            for (String e: entries) {
+                jos.putNextEntry(new ZipEntry(e));
+                jos.write(e.getBytes());
+            }
+            jos.close();
+        } finally {
+            out.close();
+        }
+        return jar;
+    }
+
+    File findRtJar() throws Exception {
+        File java_home = new File(System.getProperty("java.home"));
+        if (java_home.getName().equals("jre"))
+            java_home = java_home.getParentFile();
+        File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
+        if (!rt_jar.exists())
+            throw new Exception("can't find rt.jar");
+        return rt_jar;
+    }
+
+    byte[] read(InputStream in) throws IOException {
+        byte[] data = new byte[1024];
+        int offset = 0;
+        try {
+            int n;
+            while ((n = in.read(data, offset, data.length - offset)) != -1) {
+                offset += n;
+                if (offset == data.length)
+                    data = Arrays.copyOf(data, 2 * data.length);
+            }
+        } finally {
+            in.close();
+        }
+        return Arrays.copyOf(data, offset);
+    }
+
+    void writeFile(File f, String s) throws IOException {
+        f.getParentFile().mkdirs();
+        FileWriter out = new FileWriter(f);
+        try {
+            out.write(s);
+        } finally {
+            out.close();
+        }
+    }
+}