Skip to content

Commit 3518a8e

Browse files
committed
Remove unused SHA-1 hash from UNPACK markers
In BootZipCopyAction and AbstractJarWriter, SHA-1 hash is calculated for stored entries requiring unpack and set as entry comment. However, the hash isn't used anywhere, just the marker prefix 'UNPACK:' is checked. This commit removes the unnecessary SHA-1 hash calculation which reads the file completely in memory, potentially three times in extreme cases. Now the comment is simply set to 'UNPACK:' without any hash, improving performance. Fixes gh-46183 Signed-off-by: Hyunjoon Choi <hyunjoon@example.com> Signed-off-by: academey <academey@gmail.com>
1 parent 81a4a33 commit 3518a8e

File tree

3 files changed

+9
-40
lines changed

3 files changed

+9
-40
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
@@ -22,13 +22,10 @@
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
2424
import java.io.OutputStreamWriter;
25-
import java.security.MessageDigest;
26-
import java.security.NoSuchAlgorithmException;
2725
import java.time.OffsetDateTime;
2826
import java.time.ZoneOffset;
2927
import java.util.Collection;
3028
import java.util.HashMap;
31-
import java.util.HexFormat;
3229
import java.util.LinkedHashMap;
3330
import java.util.LinkedHashSet;
3431
import java.util.List;
@@ -578,36 +575,24 @@ private static class StoredEntryPreparator {
578575

579576
private static final int BUFFER_SIZE = 32 * 1024;
580577

581-
private final MessageDigest messageDigest;
578+
private final boolean unpack;
582579

583580
private final CRC32 crc = new CRC32();
584581

585582
private long size;
586583

587584
StoredEntryPreparator(InputStream inputStream, boolean unpack) throws IOException {
588-
this.messageDigest = (unpack) ? sha1Digest() : null;
585+
this.unpack = unpack;
589586
try (inputStream) {
590587
load(inputStream);
591588
}
592589
}
593590

594-
private static MessageDigest sha1Digest() {
595-
try {
596-
return MessageDigest.getInstance("SHA-1");
597-
}
598-
catch (NoSuchAlgorithmException ex) {
599-
throw new IllegalStateException(ex);
600-
}
601-
}
602-
603591
private void load(InputStream inputStream) throws IOException {
604592
byte[] buffer = new byte[BUFFER_SIZE];
605593
int bytesRead;
606594
while ((bytesRead = inputStream.read(buffer)) != -1) {
607595
this.crc.update(buffer, 0, bytesRead);
608-
if (this.messageDigest != null) {
609-
this.messageDigest.update(buffer, 0, bytesRead);
610-
}
611596
this.size += bytesRead;
612597
}
613598
}
@@ -617,8 +602,8 @@ void prepareStoredEntry(ZipArchiveEntry entry) {
617602
entry.setCompressedSize(this.size);
618603
entry.setCrc(this.crc.getValue());
619604
entry.setMethod(ZipEntry.STORED);
620-
if (this.messageDigest != null) {
621-
entry.setComment("UNPACK:" + HexFormat.of().formatHex(this.messageDigest.digest()));
605+
if (this.unpack) {
606+
entry.setComment("UNPACK:");
622607
}
623608
}
624609

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 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: 1 addition & 2 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

0 commit comments

Comments
 (0)