Support getComment() on a nested JarFile

Previously, calling getComment() on a nested jar file would result
in the outer jar file's comment being returned.

This commit updates the loader's JarFile to read the file's comment
from the central directory end record and return it from getComment().

Fixes gh-18128
This commit is contained in:
Andy Wilkinson
2019-09-06 14:29:48 +01:00
parent 5d346410fa
commit 795c2f225f
4 changed files with 23 additions and 0 deletions

View File

@@ -123,4 +123,10 @@ class CentralDirectoryEndRecord {
return (int) numberOfRecords;
}
String getComment() {
int commentLength = (int) Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
AsciiBytes comment = new AsciiBytes(this.block, this.offset + COMMENT_LENGTH_OFFSET + 2, commentLength);
return comment.toString();
}
}

View File

@@ -80,6 +80,8 @@ public class JarFile extends java.util.jar.JarFile {
private boolean signed;
private String comment;
/**
* Create a new {@link JarFile} backed by the specified file.
* @param file the root jar file
@@ -146,6 +148,7 @@ public class JarFile extends java.util.jar.JarFile {
@Override
public void visitStart(CentralDirectoryEndRecord endRecord, RandomAccessData centralDirectoryData) {
JarFile.this.comment = endRecord.getComment();
}
@Override
@@ -290,6 +293,11 @@ public class JarFile extends java.util.jar.JarFile {
JarFileType.NESTED_JAR);
}
@Override
public String getComment() {
return this.comment;
}
@Override
public int size() {
return this.entries.getSize();