Skip to content

Commit 49ba8aa

Browse files
committed
Merge pull request #46520 from academey
* gh-46520: Polish "Remove unused SHA-1 hash from UNPACK markers" Remove unused SHA-1 hash from UNPACK markers Closes gh-46520
2 parents 79ad5e6 + 53cda6a commit 49ba8aa

File tree

8 files changed

+17
-48
lines changed

8 files changed

+17
-48
lines changed

build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
import java.io.OutputStream;
2424
import java.io.OutputStreamWriter;
2525
import java.nio.charset.StandardCharsets;
26-
import java.security.MessageDigest;
27-
import java.security.NoSuchAlgorithmException;
2826
import java.time.OffsetDateTime;
2927
import java.time.ZoneOffset;
3028
import java.util.Collection;
3129
import java.util.HashMap;
32-
import java.util.HexFormat;
3330
import java.util.LinkedHashMap;
3431
import java.util.LinkedHashSet;
3532
import java.util.List;
@@ -585,36 +582,24 @@ private static class StoredEntryPreparator {
585582

586583
private static final int BUFFER_SIZE = 32 * 1024;
587584

588-
private final @Nullable MessageDigest messageDigest;
585+
private final boolean unpack;
589586

590587
private final CRC32 crc = new CRC32();
591588

592589
private long size;
593590

594591
StoredEntryPreparator(InputStream inputStream, boolean unpack) throws IOException {
595-
this.messageDigest = (unpack) ? sha1Digest() : null;
592+
this.unpack = unpack;
596593
try (inputStream) {
597594
load(inputStream);
598595
}
599596
}
600597

601-
private static MessageDigest sha1Digest() {
602-
try {
603-
return MessageDigest.getInstance("SHA-1");
604-
}
605-
catch (NoSuchAlgorithmException ex) {
606-
throw new IllegalStateException(ex);
607-
}
608-
}
609-
610598
private void load(InputStream inputStream) throws IOException {
611599
byte[] buffer = new byte[BUFFER_SIZE];
612600
int bytesRead;
613601
while ((bytesRead = inputStream.read(buffer)) != -1) {
614602
this.crc.update(buffer, 0, bytesRead);
615-
if (this.messageDigest != null) {
616-
this.messageDigest.update(buffer, 0, bytesRead);
617-
}
618603
this.size += bytesRead;
619604
}
620605
}
@@ -624,8 +609,8 @@ void prepareStoredEntry(ZipArchiveEntry entry) {
624609
entry.setCompressedSize(this.size);
625610
entry.setCrc(this.crc.getValue());
626611
entry.setMethod(ZipEntry.STORED);
627-
if (this.messageDigest != null) {
628-
entry.setComment("UNPACK:" + HexFormat.of().formatHex(this.messageDigest.digest()));
612+
if (this.unpack) {
613+
entry.setComment("UNPACK");
629614
}
630615
}
631616

build-plugin/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/PackagingDocumentationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void bootJarRequiresUnpack() throws IOException {
124124
try (JarFile jar = new JarFile(file)) {
125125
JarEntry entry = jar.getJarEntry("BOOT-INF/lib/jruby-complete-1.7.25.jar");
126126
assertThat(entry).isNotNull();
127-
assertThat(entry.getComment()).startsWith("UNPACK:");
127+
assertThat(entry.getComment()).isEqualTo("UNPACK");
128128
}
129129
}
130130

build-plugin/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ void unpackCommentIsAddedToEntryIdentifiedByAPattern() throws IOException {
289289
this.task.requiresUnpack("**/one.jar");
290290
executeTask();
291291
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
292-
assertThat(jarFile.getEntry(this.libPath + "one.jar").getComment()).startsWith("UNPACK:");
292+
assertThat(jarFile.getEntry(this.libPath + "one.jar").getComment()).isEqualTo("UNPACK");
293293
assertThat(jarFile.getEntry(this.libPath + "two.jar").getComment()).isNull();
294294
}
295295
}
@@ -301,7 +301,7 @@ void unpackCommentIsAddedToEntryIdentifiedByASpec() throws IOException {
301301
this.task.requiresUnpack((element) -> element.getName().endsWith("two.jar"));
302302
executeTask();
303303
try (JarFile jarFile = new JarFile(this.task.getArchiveFile().get().getAsFile())) {
304-
assertThat(jarFile.getEntry(this.libPath + "two.jar").getComment()).startsWith("UNPACK:");
304+
assertThat(jarFile.getEntry(this.libPath + "two.jar").getComment()).isEqualTo("UNPACK");
305305
assertThat(jarFile.getEntry(this.libPath + "one.jar").getComment()).isNull();
306306
}
307307
}

build-plugin/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ JarAssert hasUnpackEntryWithNameStartingWith(String prefix) {
159159
Optional<JarEntry> match = entries.filter((entry) -> entry.getName().startsWith(prefix))
160160
.findFirst();
161161
assertThat(match).as("Name starting with %s", prefix)
162-
.hasValueSatisfying((entry) -> assertThat(entry.getComment()).startsWith("UNPACK:"));
162+
.hasValueSatisfying((entry) -> assertThat(entry.getComment()).isEqualTo("UNPACK"));
163163
});
164164
});
165165
return this;

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
import java.io.OutputStreamWriter;
2525
import java.net.URL;
2626
import java.nio.charset.StandardCharsets;
27-
import java.security.MessageDigest;
28-
import java.security.NoSuchAlgorithmException;
2927
import java.util.Collection;
3028
import java.util.Enumeration;
3129
import java.util.HashSet;
32-
import java.util.HexFormat;
3330
import java.util.Set;
3431
import java.util.function.Function;
3532
import java.util.jar.JarEntry;
@@ -320,36 +317,24 @@ private static class StoredEntryPreparator {
320317

321318
private static final int BUFFER_SIZE = 32 * 1024;
322319

323-
private final @Nullable MessageDigest messageDigest;
320+
private final boolean unpack;
324321

325322
private final CRC32 crc = new CRC32();
326323

327324
private long size;
328325

329326
StoredEntryPreparator(InputStream inputStream, boolean unpack) throws IOException {
330-
this.messageDigest = (unpack) ? sha1Digest() : null;
327+
this.unpack = unpack;
331328
try (inputStream) {
332329
load(inputStream);
333330
}
334331
}
335332

336-
private static MessageDigest sha1Digest() {
337-
try {
338-
return MessageDigest.getInstance("SHA-1");
339-
}
340-
catch (NoSuchAlgorithmException ex) {
341-
throw new IllegalStateException(ex);
342-
}
343-
}
344-
345333
private void load(InputStream inputStream) throws IOException {
346334
byte[] buffer = new byte[BUFFER_SIZE];
347335
int bytesRead;
348336
while ((bytesRead = inputStream.read(buffer)) != -1) {
349337
this.crc.update(buffer, 0, bytesRead);
350-
if (this.messageDigest != null) {
351-
this.messageDigest.update(buffer, 0, bytesRead);
352-
}
353338
this.size += bytesRead;
354339
}
355340
}
@@ -359,8 +344,8 @@ void prepareStoredEntry(ZipArchiveEntry entry) {
359344
entry.setCompressedSize(this.size);
360345
entry.setCrc(this.crc.getValue());
361346
entry.setMethod(ZipEntry.STORED);
362-
if (this.messageDigest != null) {
363-
entry.setComment("UNPACK:" + HexFormat.of().formatHex(this.messageDigest.digest()));
347+
if (this.unpack) {
348+
entry.setComment("UNPACK");
364349
}
365350
}
366351

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ void libraries() throws Exception {
208208
ZipEntry entry = getPackagedEntry("BOOT-INF/lib/" + libJarFile.getName());
209209
assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
210210
entry = getPackagedEntry("BOOT-INF/lib/" + libJarFileToUnpack.getName());
211-
assertThat(entry.getComment()).startsWith("UNPACK:");
212-
assertThat(entry.getComment()).hasSize(47);
211+
assertThat(entry.getComment()).isEqualTo("UNPACK");
213212
}
214213

215214
@Test
@@ -424,7 +423,7 @@ void unpackLibrariesTakePrecedenceOverExistingSourceEntries() throws Exception {
424423
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
425424
P packager = createPackager();
426425
execute(packager, (callback) -> callback.library(newLibrary(nestedFile, LibraryScope.COMPILE, true)));
427-
assertThat(getPackagedEntry(name).getComment()).startsWith("UNPACK:");
426+
assertThat(getPackagedEntry(name).getComment()).isEqualTo("UNPACK");
428427
}
429428

430429
@Test
@@ -539,7 +538,7 @@ void existingEntryThatMatchesUnpackLibraryIsMarkedForUnpack() throws IOException
539538
assertThat(getPackagedEntryNames()).containsSubsequence("org/springframework/boot/loader/",
540539
"WEB-INF/classes/com/example/Application.class", "WEB-INF/lib/" + library.getName());
541540
ZipEntry unpackLibrary = getPackagedEntry("WEB-INF/lib/" + library.getName());
542-
assertThat(unpackLibrary.getComment()).startsWith("UNPACK:");
541+
assertThat(unpackLibrary.getComment()).isEqualTo("UNPACK");
543542
}
544543

545544
@Test

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/launch/JarFileArchive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*/
4949
class JarFileArchive implements Archive {
5050

51-
private static final String UNPACK_MARKER = "UNPACK:";
51+
private static final String UNPACK_MARKER = "UNPACK";
5252

5353
private static final FileAttribute<?>[] NO_FILE_ATTRIBUTES = {};
5454

loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/testsupport/TestJar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static void writeNestedEntry(String name, boolean unpackNested, JarOutpu
9090
nestedEntry.setSize(nestedJarData.length);
9191
nestedEntry.setCompressedSize(nestedJarData.length);
9292
if (unpackNested) {
93-
nestedEntry.setComment("UNPACK:0000000000000000000000000000000000000000");
93+
nestedEntry.setComment("UNPACK");
9494
}
9595
CRC32 crc32 = new CRC32();
9696
crc32.update(nestedJarData);

0 commit comments

Comments
 (0)