< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java

Print this page
rev 15157 : 8134779: (jmod) ZipException is thrown if there are duplicate resources
8134847: (jmod) module-info encountered in the cmds, libs or config is not added to jmod file
Reviewed-by:


 649             throws IOException
 650         {
 651             if (paths == null)
 652                 return;
 653 
 654             for (Path p : paths)
 655                 processSection(zos, section, p);
 656         }
 657 
 658         void processSection(ZipOutputStream zos, Section section, Path top)
 659             throws IOException
 660         {
 661             final String prefix = section.jmodDir();
 662 
 663             Files.walkFileTree(top, new SimpleFileVisitor<Path>() {
 664                 @Override
 665                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
 666                     throws IOException
 667                 {
 668                     Path relPath = top.relativize(file);




 669                     if (!relPath.toString().equals(MODULE_INFO)
 670                             && !matches(relPath, excludes)) {
 671                         try (InputStream in = Files.newInputStream(file)) {
 672                             writeZipEntry(zos, in, prefix, relPath.toString());
 673                         }
 674                     }
 675                     return FileVisitResult.CONTINUE;
 676                 }
 677             });
 678         }
 679 
 680         boolean matches(Path path, List<PathMatcher> matchers) {
 681             if (matchers != null) {
 682                 for (PathMatcher pm : matchers) {
 683                     if (pm.matches(path))
 684                         return true;
 685                 }
 686             }
 687             return false;
 688         }
 689 
 690         void writeZipEntry(ZipOutputStream zos, InputStream in, String prefix, String other)
 691             throws IOException
 692         {
 693             String name = Paths.get(prefix, other).toString()
 694                                .replace(File.separatorChar, '/');
 695             ZipEntry ze = new ZipEntry(name);

 696             zos.putNextEntry(ze);
 697             in.transferTo(zos);
 698             zos.closeEntry();







 699         }
 700 
 701         class JarEntryConsumer implements Consumer<JarEntry>, Predicate<JarEntry> {
 702             final ZipOutputStream zos;
 703             final JarFile jarfile;
 704             JarEntryConsumer(ZipOutputStream zos, JarFile jarfile) {
 705                 this.zos = zos;
 706                 this.jarfile = jarfile;
 707             }
 708             @Override
 709             public void accept(JarEntry je) {
 710                 try (InputStream in = jarfile.getInputStream(je)) {
 711                     writeZipEntry(zos, in, Section.CLASSES.jmodDir(), je.getName());
 712                 } catch (IOException e) {
 713                     throw new UncheckedIOException(e);
 714                 }
 715             }
 716             @Override
 717             public boolean test(JarEntry je) {
 718                 String name = je.getName();




 649             throws IOException
 650         {
 651             if (paths == null)
 652                 return;
 653 
 654             for (Path p : paths)
 655                 processSection(zos, section, p);
 656         }
 657 
 658         void processSection(ZipOutputStream zos, Section section, Path top)
 659             throws IOException
 660         {
 661             final String prefix = section.jmodDir();
 662 
 663             Files.walkFileTree(top, new SimpleFileVisitor<Path>() {
 664                 @Override
 665                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
 666                     throws IOException
 667                 {
 668                     Path relPath = top.relativize(file);
 669                     if (relPath.toString().equals(MODULE_INFO)
 670                             && !Section.CLASSES.equals(section))
 671                         warning("warn.ignore.entry", MODULE_INFO, section);
 672 
 673                     if (!relPath.toString().equals(MODULE_INFO)
 674                             && !matches(relPath, excludes)) {
 675                         try (InputStream in = Files.newInputStream(file)) {
 676                             writeZipEntry(zos, in, prefix, relPath.toString());
 677                         }
 678                     }
 679                     return FileVisitResult.CONTINUE;
 680                 }
 681             });
 682         }
 683 
 684         boolean matches(Path path, List<PathMatcher> matchers) {
 685             if (matchers != null) {
 686                 for (PathMatcher pm : matchers) {
 687                     if (pm.matches(path))
 688                         return true;
 689                 }
 690             }
 691             return false;
 692         }
 693 
 694         void writeZipEntry(ZipOutputStream zos, InputStream in, String prefix, String other)
 695             throws IOException
 696         {
 697             String name = Paths.get(prefix, other).toString()
 698                                .replace(File.separatorChar, '/');
 699             ZipEntry ze = new ZipEntry(name);
 700             try {
 701                 zos.putNextEntry(ze);
 702                 in.transferTo(zos);
 703                 zos.closeEntry();
 704             } catch (ZipException x) {
 705                 if (x.getMessage().contains("duplicate entry")) {
 706                     warning("warn.ignore.duplicate.entry", name, prefix);
 707                     return;
 708                 }
 709                 throw x;
 710             }
 711         }
 712 
 713         class JarEntryConsumer implements Consumer<JarEntry>, Predicate<JarEntry> {
 714             final ZipOutputStream zos;
 715             final JarFile jarfile;
 716             JarEntryConsumer(ZipOutputStream zos, JarFile jarfile) {
 717                 this.zos = zos;
 718                 this.jarfile = jarfile;
 719             }
 720             @Override
 721             public void accept(JarEntry je) {
 722                 try (InputStream in = jarfile.getInputStream(je)) {
 723                     writeZipEntry(zos, in, Section.CLASSES.jmodDir(), je.getName());
 724                 } catch (IOException e) {
 725                     throw new UncheckedIOException(e);
 726                 }
 727             }
 728             @Override
 729             public boolean test(JarEntry je) {
 730                 String name = je.getName();


< prev index next >