Skip to content

Commit 795c2f2

Browse files
committed
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
1 parent 5d34641 commit 795c2f2

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/CentralDirectoryEndRecord.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@ public int getNumberOfRecords() {
123123
return (int) numberOfRecords;
124124
}
125125

126+
String getComment() {
127+
int commentLength = (int) Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
128+
AsciiBytes comment = new AsciiBytes(this.block, this.offset + COMMENT_LENGTH_OFFSET + 2, commentLength);
129+
return comment.toString();
130+
}
131+
126132
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class JarFile extends java.util.jar.JarFile {
8080

8181
private boolean signed;
8282

83+
private String comment;
84+
8385
/**
8486
* Create a new {@link JarFile} backed by the specified file.
8587
* @param file the root jar file
@@ -146,6 +148,7 @@ private CentralDirectoryVisitor centralDirectoryVisitor() {
146148

147149
@Override
148150
public void visitStart(CentralDirectoryEndRecord endRecord, RandomAccessData centralDirectoryData) {
151+
JarFile.this.comment = endRecord.getComment();
149152
}
150153

151154
@Override
@@ -290,6 +293,11 @@ private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
290293
JarFileType.NESTED_JAR);
291294
}
292295

296+
@Override
297+
public String getComment() {
298+
return this.comment;
299+
}
300+
293301
@Override
294302
public int size() {
295303
return this.entries.getSize();

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/TestJarCreator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static void createTestJar(File file) throws Exception {
4141
public static void createTestJar(File file, boolean unpackNested) throws Exception {
4242
FileOutputStream fileOutputStream = new FileOutputStream(file);
4343
try (JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream)) {
44+
jarOutputStream.setComment("outer");
4445
writeManifest(jarOutputStream, "j1");
4546
writeEntry(jarOutputStream, "1.dat", 1);
4647
writeEntry(jarOutputStream, "2.dat", 2);
@@ -88,6 +89,7 @@ private static void writeNestedEntry(String name, boolean unpackNested, JarOutpu
8889
private static byte[] getNestedJarData(boolean multiRelease) throws Exception {
8990
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
9091
JarOutputStream jarOutputStream = new JarOutputStream(byteArrayOutputStream);
92+
jarOutputStream.setComment("nested");
9193
writeManifest(jarOutputStream, "j2", multiRelease);
9294
if (multiRelease) {
9395
writeEntry(jarOutputStream, "multi-release.dat", 8);

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void setup() throws Exception {
7979
public void jdkJarFile() throws Exception {
8080
// Sanity checks to see how the default jar file operates
8181
java.util.jar.JarFile jarFile = new java.util.jar.JarFile(this.rootJarFile);
82+
assertThat(jarFile.getComment()).isEqualTo("outer");
8283
Enumeration<java.util.jar.JarEntry> entries = jarFile.entries();
8384
assertThat(entries.nextElement().getName()).isEqualTo("META-INF/");
8485
assertThat(entries.nextElement().getName()).isEqualTo("META-INF/MANIFEST.MF");
@@ -152,6 +153,11 @@ public void getJarEntry() {
152153
assertThat(entry.getName()).isEqualTo("1.dat");
153154
}
154155

156+
@Test
157+
public void getComment() {
158+
assertThat(this.jarFile.getComment()).isEqualTo("outer");
159+
}
160+
155161
@Test
156162
public void getInputStream() throws Exception {
157163
InputStream inputStream = this.jarFile.getInputStream(this.jarFile.getEntry("1.dat"));
@@ -245,6 +251,7 @@ public void getEntryUrlStream() throws Exception {
245251
@Test
246252
public void getNestedJarFile() throws Exception {
247253
JarFile nestedJarFile = this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
254+
assertThat(nestedJarFile.getComment()).isEqualTo("nested");
248255

249256
Enumeration<java.util.jar.JarEntry> entries = nestedJarFile.entries();
250257
assertThat(entries.nextElement().getName()).isEqualTo("META-INF/");

0 commit comments

Comments
 (0)