From 639b7e34b7cdefd0a2b01d31b7f97ba7b422a3d4 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 1 Mar 2021 18:29:26 +0100 Subject: [PATCH] clean code --- .../maven/plugin/processor/UnzipService.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/UnzipService.java b/processor/src/main/java/org/bsc/maven/plugin/processor/UnzipService.java index 8781dc2..8d3b762 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/UnzipService.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/UnzipService.java @@ -12,14 +12,11 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Enumeration; -import java.util.Optional; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; import static java.lang.String.format; import static java.util.Objects.requireNonNull; -import static java.util.Optional.empty; /** * @@ -120,26 +117,25 @@ public final void extractSourcesFromArtifact(Artifact artifact, java.util.List entries = zipFile.entries(); - int sourceCount = 0; while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (isJavaEntry(entry)) { - ++sourceCount; allSources.add(ZipFileObject.create(zipFile, entry)); } } - log.debug(format("** Discovered %d java sources in %s", sourceCount, f.getAbsolutePath())); + log.info(format("Discovered [%d] java sources in artifact [%s]", allSources.size()-size, artifact)); } catch (Exception ex) { - log.warn(format("Problem reading source archive [%s]", f.getPath())); + log.warn(format("Problem reading source archive [%s]", fileZip.getPath())); log.debug(ex); } } @@ -148,6 +144,8 @@ public final void extractSourcesFromArtifactToTempDirectory(Artifact artifact, j requireNonNull( artifact, "artifact argument is null!"); requireNonNull( allSources, "allSources argument is null!"); + final int size = allSources.size(); + final File fileZip = artifact.getFile(); final Path root; @@ -159,13 +157,15 @@ public final void extractSourcesFromArtifactToTempDirectory(Artifact artifact, j return; } - try( final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)) ) { + try( final ZipFile zipFile = new ZipFile(fileZip) ) { final byte[] buffer = new byte[4096]; - ZipEntry zipEntry = zis.getNextEntry(); + final Enumeration entries = zipFile.entries(); + + while (entries.hasMoreElements()) { - while (zipEntry != null) { + final ZipEntry zipEntry = entries.nextElement(); final Path newFile = Paths.get(root.toString(), zipEntry.getName()); @@ -175,19 +175,18 @@ public final void extractSourcesFromArtifactToTempDirectory(Artifact artifact, j } else if (isJavaEntry(zipEntry)) { - try (final FileOutputStream fos = new FileOutputStream(newFile.toFile())) { + try (final FileOutputStream fos = new FileOutputStream(newFile.toFile()); + final InputStream zis = zipFile.getInputStream(zipEntry) ) + { int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } allSources.add( new ZipFileObjectExtracted(newFile.toUri(), JavaFileObject.Kind.SOURCE)); - } } - zipEntry = zis.getNextEntry(); } - zis.closeEntry(); } catch( Exception ex ) { log.warn(format("Problem reading source archive [%s]", fileZip)); @@ -195,6 +194,6 @@ public final void extractSourcesFromArtifactToTempDirectory(Artifact artifact, j } - log.info( format( "artifact [%s] succesfully extracted to [%s]", artifact, root)); + log.info( format( "[%d] sources succesfully extracted from artifact [%s] to:\n [%s]", allSources.size()-size, artifact, root)); } }