This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit d30b19694cecd9d8b2d476e756d869becbda7875 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jan 28 17:26:41 2024 -0500 Use final Reduce block nesting --- .../compress/archivers/zip/ZipArchiveInputStream.java | 11 ++++++----- .../archivers/zip/ZipArchiveInputStreamTest.java | 16 ++++++++-------- .../commons/compress/archivers/zip/ZipFileTest.java | 8 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index 75d935fab..7b017bdee 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -1008,11 +1008,11 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i private boolean readFirstLocalFileHeader() throws IOException { try { // for empty archive, we may get only EOCD size: - byte[] header = new byte[Math.min(LFH_LEN, ZipFile.MIN_EOCD_SIZE)]; + final byte[] header = new byte[Math.min(LFH_LEN, ZipFile.MIN_EOCD_SIZE)]; readFully(header); READ_LOOP: for (int i = 0; ; ) { for (int j = 0; i <= PREAMBLE_GARBAGE_MAX_SIZE - 4 && j <= header.length - 4; ++j, ++i) { - ZipLong sig = new ZipLong(header, j); + final ZipLong sig = new ZipLong(header, j); if ( sig.equals(ZipLong.LFH_SIG) || sig.equals(ZipLong.SINGLE_SEGMENT_SPLIT_MARKER) || @@ -1021,7 +1021,8 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i System.arraycopy(header, j, header, 0, header.length - j); readFully(header, header.length - j); break READ_LOOP; - } else if ( + } + if ( sig.equals(new ZipLong(ZipArchiveOutputStream.EOCD_SIG)) ) { // empty archive: @@ -1037,7 +1038,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i } System.arraycopy(header, 0, lfhBuf, 0, header.length); readFully(lfhBuf, header.length); - } catch (EOFException ex) { + } catch (final EOFException ex) { throw new ZipException("Cannot find zip signature within the file"); } final ZipLong sig = new ZipLong(lfhBuf); @@ -1278,7 +1279,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i /** * Reads the stream until it find the "End of central directory record" and consumes it as well. */ - private void skipRemainderOfArchive(boolean read) throws IOException { + private void skipRemainderOfArchive(final boolean read) throws IOException { // skip over central directory. One LFH has been read too much // already. The calculation discounts file names and extra // data, so it will be too short. diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java index f24b811ac..253759bb6 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java @@ -715,12 +715,12 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testZipWithLongerBeginningGarbage() throws IOException { - Path path = createTempPath("preamble", ".zip"); + final Path path = createTempPath("preamble", ".zip"); try (OutputStream fos = Files.newOutputStream(path)) { fos.write("#!/usr/bin/env some-program with quite a few arguments to make it longer than the local header\n".getBytes(StandardCharsets.UTF_8)); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) { - ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); + final ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); entry.setMethod(ZipEntry.DEFLATED); zos.putArchiveEntry(entry); zos.write("entry-content\n".getBytes(StandardCharsets.UTF_8)); @@ -729,21 +729,21 @@ public class ZipArchiveInputStreamTest extends AbstractTest { } try (InputStream is = Files.newInputStream(path); ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) { - ZipArchiveEntry entry = zis.getNextEntry(); + final ZipArchiveEntry entry = zis.getNextEntry(); assertEquals("file-1.txt", entry.getName()); - byte[] content = IOUtils.toByteArray(zis); + final byte[] content = IOUtils.toByteArray(zis); assertArrayEquals("entry-content\n".getBytes(StandardCharsets.UTF_8), content); } } @Test public void testZipWithShortBeginningGarbage() throws IOException { - Path path = createTempPath("preamble", ".zip"); + final Path path = createTempPath("preamble", ".zip"); try (OutputStream fos = Files.newOutputStream(path)) { fos.write("#!/usr/bin/unzip\n".getBytes(StandardCharsets.UTF_8)); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) { - ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); + final ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); entry.setMethod(ZipEntry.DEFLATED); zos.putArchiveEntry(entry); zos.write("entry-content\n".getBytes(StandardCharsets.UTF_8)); @@ -752,9 +752,9 @@ public class ZipArchiveInputStreamTest extends AbstractTest { } try (InputStream is = Files.newInputStream(path); ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) { - ZipArchiveEntry entry = zis.getNextEntry(); + final ZipArchiveEntry entry = zis.getNextEntry(); assertEquals("file-1.txt", entry.getName()); - byte[] content = IOUtils.toByteArray(zis); + final byte[] content = IOUtils.toByteArray(zis); assertArrayEquals("entry-content\n".getBytes(StandardCharsets.UTF_8), content); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java index 72418bfa2..f5979c3d6 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java @@ -960,12 +960,12 @@ public class ZipFileTest extends AbstractTest { @Test public void testZipWithShortBeginningGarbage() throws IOException { - Path path = createTempPath("preamble", ".zip"); + final Path path = createTempPath("preamble", ".zip"); try (OutputStream fos = Files.newOutputStream(path)) { fos.write("#!/usr/bin/unzip\n".getBytes(StandardCharsets.UTF_8)); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) { - ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); + final ZipArchiveEntry entry = new ZipArchiveEntry("file-1.txt"); entry.setMethod(ZipEntry.DEFLATED); zos.putArchiveEntry(entry); zos.write("entry-content\n".getBytes(StandardCharsets.UTF_8)); @@ -974,9 +974,9 @@ public class ZipFileTest extends AbstractTest { } try (ZipFile zipFile = ZipFile.builder().setPath(path).get()) { - ZipArchiveEntry entry = zipFile.getEntry("file-1.txt"); + final ZipArchiveEntry entry = zipFile.getEntry("file-1.txt"); assertEquals("file-1.txt", entry.getName()); - byte[] content = IOUtils.toByteArray(zipFile.getInputStream(entry)); + final byte[] content = IOUtils.toByteArray(zipFile.getInputStream(entry)); assertArrayEquals("entry-content\n".getBytes(StandardCharsets.UTF_8), content); } }