|
32 | 32 | import java.util.function.Function;
|
33 | 33 | import java.util.stream.Collectors;
|
34 | 34 | import java.util.zip.CRC32;
|
| 35 | +import java.util.zip.ZipEntry; |
35 | 36 |
|
36 | 37 | import org.apache.commons.compress.archivers.zip.UnixStat;
|
37 | 38 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
|
53 | 54 | import org.springframework.boot.loader.tools.Layer;
|
54 | 55 | import org.springframework.boot.loader.tools.LayersIndex;
|
55 | 56 | import org.springframework.util.Assert;
|
56 |
| -import org.springframework.util.FileCopyUtils; |
57 | 57 | import org.springframework.util.StreamUtils;
|
58 | 58 | import org.springframework.util.StringUtils;
|
59 | 59 |
|
@@ -376,12 +376,7 @@ private void prepareStoredEntry(FileCopyDetails details, ZipArchiveEntry archive
|
376 | 376 | }
|
377 | 377 |
|
378 | 378 | 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); |
385 | 380 | }
|
386 | 381 |
|
387 | 382 | private Long getTime() {
|
@@ -464,29 +459,39 @@ static ZipEntryContentWriter fromLines(String encoding, Collection<String> lines
|
464 | 459 | }
|
465 | 460 |
|
466 | 461 | /**
|
467 |
| - * An {@code OutputStream} that provides a CRC-32 of the data that is written to it. |
| 462 | + * Data holder for CRC and Size. |
468 | 463 | */
|
469 |
| - private static final class Crc32OutputStream extends OutputStream { |
| 464 | + private static class CrcAndSize { |
| 465 | + |
| 466 | + private static final int BUFFER_SIZE = 32 * 1024; |
470 | 467 |
|
471 | 468 | private final CRC32 crc = new CRC32();
|
472 | 469 |
|
473 |
| - @Override |
474 |
| - public void write(int b) throws IOException { |
475 |
| - this.crc.update(b); |
476 |
| - } |
| 470 | + private long size; |
477 | 471 |
|
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 | + } |
481 | 479 | }
|
482 | 480 |
|
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 | + } |
486 | 488 | }
|
487 | 489 |
|
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); |
490 | 495 | }
|
491 | 496 |
|
492 | 497 | }
|
|
0 commit comments