Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 7, 2024
1 parent ab556a2 commit c1bc942
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
Expand Down Expand Up @@ -38,7 +39,6 @@ public class InjectFromZipFileSource implements InjectSource {
/**
* This function can modify the content that is being copied.
*/
@Nullable
private final ContentFilter contentFilter;

public InjectFromZipFileSource(ZipFile zf, String sourcePath) {
Expand All @@ -53,7 +53,7 @@ public InjectFromZipFileSource(ZipFile zf, String sourcePath, @Nullable Pattern
this.zf = zf;
this.sourcePath = sanitizeSourcePath(sourcePath);
this.includeFilterPattern = includeFilterPattern;
this.contentFilter = contentFilter;
this.contentFilter = Objects.requireNonNullElse(contentFilter, ContentFilter.NONE);
}

private static String sanitizeSourcePath(String sourcePath) {
Expand Down Expand Up @@ -82,11 +82,7 @@ public CacheKey.AnnotatedValue getCacheKey(FileHashService fileHashService) thro
if ((sourcePath.isEmpty() || entry.getName().startsWith(sourcePath)) && matchesIncludeFilter(entry)) {
digestStream.write(entry.getName().getBytes());
try (var in = zf.getInputStream(entry)) {
if (contentFilter != null) {
contentFilter.copy(entry, in, digestStream);
} else {
in.transferTo(digestStream);
}
contentFilter.copy(entry, in, digestStream);
}
}
}
Expand Down Expand Up @@ -123,11 +119,7 @@ public void copyTo(ZipOutputStream out) throws IOException {
copiedEntry.setMethod(entry.getMethod());

out.putNextEntry(copiedEntry);
if (contentFilter != null) {
contentFilter.copy(entry, in, out);
} else {
in.transferTo(out);
}
contentFilter.copy(entry, in, out);
out.closeEntry();
} catch (ZipException e) {
if (!e.getMessage().startsWith("duplicate entry:")) {
Expand All @@ -147,6 +139,8 @@ private boolean matchesIncludeFilter(ZipEntry entry) {

@FunctionalInterface
public interface ContentFilter {
ContentFilter NONE = (entry, in, out) -> in.transferTo(out);

void copy(ZipEntry entry, InputStream in, OutputStream out) throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class StripManifestDigestContentFilter implements InjectFromZipFileSource

public static final StripManifestDigestContentFilter INSTANCE = new StripManifestDigestContentFilter();

private StripManifestDigestContentFilter() {
}

@Override
public void copy(ZipEntry entry, InputStream in, OutputStream out) throws IOException {
if (!entry.getName().equals("META-INF/MANIFEST.MF")) {
Expand Down

0 comments on commit c1bc942

Please sign in to comment.