--- old/make/tools/classanalyzer/src/com/sun/classanalyzer/Modularizer.java Fri Jul 20 11:31:11 2012 +++ new/make/tools/classanalyzer/src/com/sun/classanalyzer/Modularizer.java Fri Jul 20 11:31:11 2012 @@ -190,6 +190,9 @@ * @return the number of bytes copied */ long writeResource(ResourceFile res, Filter filter) throws IOException { + if (res.isService()) + return writeService(res, filter); + String pathname = res.getPathname(); Copier visitor = new Copier(classDir, filter); if (lastVisitedClassPath != null) { @@ -215,6 +218,33 @@ } /** + * Write the service descriptor file if not filtered + * + * @param res a ResourceFile + * @param filter a Filter + * @return the number of bytes copied + */ + long writeService(ResourceFile res, Filter filter) throws IOException { + String pathname = res.getPathname(); + Copier visitor = new Copier(classDir, filter); + boolean foundOne = false; + int bytes = 0; + + // scan all class path entries for services + for (ClassPathEntry cp : cpath.entries()) { + ClassPathEntry src = cp.accept(visitor, pathname); + if (src != null) { + bytes += visitor.bytes; + if (foundOne == false) { + foundOne = true; + visitor = new Copier(classDir, null, true); // append subsequent + } + } + } + return bytes; + } + + /** * A ClassPathEntry visitor to copy a file to the given destination * if not filtered. */ @@ -221,13 +251,23 @@ class Copier implements ClassPathEntry.Visitor { final Filter filter; final File dest; + final boolean append; long bytes = 0; Copier(File dest, Filter filter) { + this(dest, filter, false); + } + + Copier(File dest, Filter filter, boolean append) { this.filter = filter; this.dest = dest; + this.append = append; } + private boolean isService(String name) { + return name.startsWith("META-INF/services") ? true : false; + } + @Override public ClassPathEntry visitFile(File src, ClassPathEntry cp, String pathname) throws IOException { String name = pathname.replace(File.separatorChar, '/'); @@ -274,20 +314,18 @@ } boolean matches(File src, String name) throws IOException { - if (!name.startsWith("META-INF/services")) { + if (!isService(name)) { return true; } - BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); - try { + try (FileInputStream fis = new FileInputStream(src); + BufferedInputStream in = new BufferedInputStream(fis)) { return matches(in, name); - } finally { - in.close(); } } boolean matches(JarFile jf, JarEntry e, String name) throws IOException { - if (!name.startsWith("META-INF/services")) { + if (!isService(name)) { return true; } return matches(jf.getInputStream(e), name); @@ -316,21 +354,14 @@ } byte[] buf = new byte[8192]; - InputStream in = jf.getInputStream(e); long bytes = 0; - try { - FileOutputStream out = new FileOutputStream(dst); - try { - int n; - while ((n = in.read(buf)) > 0) { - out.write(buf, 0, n); - bytes += n; - } - } finally { - out.close(); + try (InputStream in = jf.getInputStream(e); + FileOutputStream out = new FileOutputStream(dst, append)) { + int n; + while ((n = in.read(buf)) > 0) { + out.write(buf, 0, n); + bytes += n; } - } finally { - in.close(); } long lastModified = e.getTime(); @@ -348,22 +379,16 @@ Files.createFile(dst); } - BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); byte[] buf = new byte[8192]; long bytes = 0; - try { - FileOutputStream out = new FileOutputStream(dst); - try { - int n; - while ((n = in.read(buf)) > 0) { - out.write(buf, 0, n); - bytes += n; - } - } finally { - out.close(); + try (InputStream fin = new FileInputStream(src); + BufferedInputStream in = new BufferedInputStream(fin); + FileOutputStream out = new FileOutputStream(dst, append)) { + int n; + while ((n = in.read(buf)) > 0) { + out.write(buf, 0, n); + bytes += n; } - } finally { - in.close(); } dst.setLastModified(src.lastModified()); if (src.canExecute()) { --- old/make/tools/classanalyzer/src/com/sun/classanalyzer/ResourceFile.java Fri Jul 20 11:31:12 2012 +++ new/make/tools/classanalyzer/src/com/sun/classanalyzer/ResourceFile.java Fri Jul 20 11:31:12 2012 @@ -76,6 +76,10 @@ return filesize; } + boolean isService() { + return pathname.startsWith("META-INF/services") ? true : false; + } + @Override public String toString() { return name; @@ -105,7 +109,7 @@ return true; } - static ResourceFile addResource(String fname, InputStream in, long size) { + static ResourceFile getResource(String fname, InputStream in, long size) { ResourceFile res; fname = fname.replace(File.separatorChar, '/'); if (fname.startsWith("META-INF/services")) { @@ -113,10 +117,16 @@ } else { res = new ResourceFile(fname, size); } + return res; + } + + static ResourceFile addResource(String fname, InputStream in, long size) { + ResourceFile res = getResource(fname, in, size); resources.add(res); return res; } + static Set getAllResources() { return Collections.unmodifiableSet(resources); } @@ -135,6 +145,11 @@ this.service = name.substring("META-INF/services".length() + 1, name.length()); } + @Override + boolean isService() { + return true; + } + @Override String getName() { if (providers.isEmpty()) {