Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Mar 1, 2021
1 parent c3651eb commit 639b7e3
Showing 1 changed file with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand Down Expand Up @@ -120,26 +117,25 @@ public final void extractSourcesFromArtifact(Artifact artifact, java.util.List<J
requireNonNull( artifact, "artifact argument is null!");
requireNonNull( allSources, "allSources argument is null!");

final File f = artifact.getFile();
final int size = allSources.size();

try {
final File fileZip = artifact.getFile();

final ZipFile zipFile = new ZipFile(f);
try {
final ZipFile zipFile = new ZipFile(fileZip);
final Enumeration<? extends ZipEntry> 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);
}
}
Expand All @@ -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;
Expand All @@ -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<? extends ZipEntry> entries = zipFile.entries();

while (entries.hasMoreElements()) {

while (zipEntry != null) {
final ZipEntry zipEntry = entries.nextElement();

final Path newFile = Paths.get(root.toString(), zipEntry.getName());

Expand All @@ -175,26 +175,25 @@ 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));
log.debug(ex);

}

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));
}
}

0 comments on commit 639b7e3

Please sign in to comment.