Skip to content

Commit 9975050

Browse files
committed
Handle files larger than Integer.MAX_VALUE bytes in bootJar and bootWar
Fixes gh-24618
1 parent f02951f commit 9975050

File tree

1 file changed

+26
-21
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling

1 file changed

+26
-21
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.function.Function;
3333
import java.util.stream.Collectors;
3434
import java.util.zip.CRC32;
35+
import java.util.zip.ZipEntry;
3536

3637
import org.apache.commons.compress.archivers.zip.UnixStat;
3738
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -53,7 +54,6 @@
5354
import org.springframework.boot.loader.tools.Layer;
5455
import org.springframework.boot.loader.tools.LayersIndex;
5556
import org.springframework.util.Assert;
56-
import org.springframework.util.FileCopyUtils;
5757
import org.springframework.util.StreamUtils;
5858
import org.springframework.util.StringUtils;
5959

@@ -376,12 +376,7 @@ private void prepareStoredEntry(FileCopyDetails details, ZipArchiveEntry archive
376376
}
377377

378378
private void prepareStoredEntry(InputStream input, ZipArchiveEntry archiveEntry) throws IOException {
379-
archiveEntry.setMethod(java.util.zip.ZipEntry.STORED);
380-
Crc32OutputStream crcStream = new Crc32OutputStream();
381-
int size = FileCopyUtils.copy(input, crcStream);
382-
archiveEntry.setSize(size);
383-
archiveEntry.setCompressedSize(size);
384-
archiveEntry.setCrc(crcStream.getCrc());
379+
new CrcAndSize(input).setUpStoredEntry(archiveEntry);
385380
}
386381

387382
private Long getTime() {
@@ -464,29 +459,39 @@ static ZipEntryContentWriter fromLines(String encoding, Collection<String> lines
464459
}
465460

466461
/**
467-
* An {@code OutputStream} that provides a CRC-32 of the data that is written to it.
462+
* Data holder for CRC and Size.
468463
*/
469-
private static final class Crc32OutputStream extends OutputStream {
464+
private static class CrcAndSize {
465+
466+
private static final int BUFFER_SIZE = 32 * 1024;
470467

471468
private final CRC32 crc = new CRC32();
472469

473-
@Override
474-
public void write(int b) throws IOException {
475-
this.crc.update(b);
476-
}
470+
private long size;
477471

478-
@Override
479-
public void write(byte[] b) throws IOException {
480-
this.crc.update(b);
472+
CrcAndSize(InputStream inputStream) throws IOException {
473+
try {
474+
load(inputStream);
475+
}
476+
finally {
477+
inputStream.close();
478+
}
481479
}
482480

483-
@Override
484-
public void write(byte[] b, int off, int len) throws IOException {
485-
this.crc.update(b, off, len);
481+
private void load(InputStream inputStream) throws IOException {
482+
byte[] buffer = new byte[BUFFER_SIZE];
483+
int bytesRead;
484+
while ((bytesRead = inputStream.read(buffer)) != -1) {
485+
this.crc.update(buffer, 0, bytesRead);
486+
this.size += bytesRead;
487+
}
486488
}
487489

488-
private long getCrc() {
489-
return this.crc.getValue();
490+
void setUpStoredEntry(ZipArchiveEntry entry) {
491+
entry.setSize(this.size);
492+
entry.setCompressedSize(this.size);
493+
entry.setCrc(this.crc.getValue());
494+
entry.setMethod(ZipEntry.STORED);
490495
}
491496

492497
}

0 commit comments

Comments
 (0)