You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/10 11:32:41 UTC
(commons-compress) branch master updated: Refactor for reuse
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
The following commit(s) were added to refs/heads/master by this push:
new 67042103f Refactor for reuse
67042103f is described below
commit 67042103f1bdbfcbd211796cd8239929cb263764
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 10 07:32:37 2025 -0400
Refactor for reuse
- Javadoc
---
.../archivers/ar/ArArchiveInputStream.java | 8 ++++----
.../compress/archivers/sevenz/SevenZFile.java | 21 ++++++--------------
.../archivers/sevenz/SevenZOutputFile.java | 8 ++++----
.../archivers/zip/ZipArchiveInputStream.java | 23 +++++++---------------
.../compress/archivers/sevenz/SevenZFileTest.java | 16 +++++++++------
5 files changed, 31 insertions(+), 45 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
index 28daa87e8..2c6fba80e 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
@@ -110,11 +110,11 @@ private static boolean isGNUStringTable(final String name) {
/**
* Checks if the signature matches ASCII "!<arch>" followed by a single LF control character
*
- * @param buffer the bytes to check
- * @param length the number of bytes to check
- * @return true, if this stream is an Ar archive stream, false otherwise
+ * @param buffer the bytes to check.
+ * @param ignored ignored.
+ * @return true, if this stream is an Ar archive stream, false otherwise.
*/
- public static boolean matches(final byte[] buffer, final int length) {
+ public static boolean matches(final byte[] buffer, final int ignored) {
return ArrayUtils.startsWith(buffer, SIGNATURE);
}
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
index 6c809ed93..2e2accee6 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
@@ -340,8 +340,7 @@ public Builder setUseDefaultNameForUnnamedEntries(final boolean useDefaultNameFo
private static final String DEFAULT_FILE_NAME = "unknown archive";
/** Shared with SevenZOutputFile and tests, neither mutates it. */
- static final byte[] sevenZSignature = { // NOSONAR
- (byte) '7', (byte) 'z', (byte) 0xBC, (byte) 0xAF, (byte) 0x27, (byte) 0x1C };
+ static final byte[] SIGNATURE = { (byte) '7', (byte) 'z', (byte) 0xBC, (byte) 0xAF, (byte) 0x27, (byte) 0x1C };
/**
* Throws IOException if the given value is not in {@code [0, Integer.MAX_VALUE]}.
@@ -421,21 +420,13 @@ private static long kbToKiB(final long kilobytes) {
/**
* Checks if the signature matches what is expected for a 7z file.
*
- * @param signature the bytes to check
- * @param length the number of bytes to check
+ * @param buffer the bytes to check.
+ * @param ignored ignored.
* @return true, if this is the signature of a 7z archive.
* @since 1.8
*/
- public static boolean matches(final byte[] signature, final int length) {
- if (length < sevenZSignature.length) {
- return false;
- }
- for (int i = 0; i < sevenZSignature.length; i++) {
- if (signature[i] != sevenZSignature[i]) {
- return false;
- }
- }
- return true;
+ public static boolean matches(final byte[] buffer, final int ignored) {
+ return ArrayUtils.startsWith(buffer, SIGNATURE);
}
private static SeekableByteChannel newByteChannel(final File file) throws IOException {
@@ -1544,7 +1535,7 @@ private Archive readHeaders(final byte[] password) throws IOException {
readFully(buf);
final byte[] signature = new byte[6];
buf.get(signature);
- if (!Arrays.equals(signature, sevenZSignature)) {
+ if (!Arrays.equals(signature, SIGNATURE)) {
throw new ArchiveException("Bad 7z signature");
}
// 7zFormat.txt has it wrong - it's first major then minor
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
index 386c25edd..5a35824c1 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
@@ -309,14 +309,14 @@ public void finish() throws IOException {
final CRC32 crc32 = new CRC32();
crc32.update(headerBytes);
- final ByteBuffer bb = ByteBuffer.allocate(SevenZFile.sevenZSignature.length + 2 /* version */
+ final ByteBuffer bb = ByteBuffer.allocate(SevenZFile.SIGNATURE.length + 2 /* version */
+ 4 /* start header CRC */
+ 8 /* next header position */
+ 8 /* next header length */
+ 4 /* next header CRC */).order(ByteOrder.LITTLE_ENDIAN);
// signature header
channel.position(0);
- bb.put(SevenZFile.sevenZSignature);
+ bb.put(SevenZFile.SIGNATURE);
// version
bb.put((byte) 0).put((byte) 2);
@@ -326,8 +326,8 @@ public void finish() throws IOException {
// start header
bb.putLong(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE).putLong(0xffffFFFFL & headerBytes.length).putInt((int) crc32.getValue());
crc32.reset();
- crc32.update(bb.array(), SevenZFile.sevenZSignature.length + 6, 20);
- bb.putInt(SevenZFile.sevenZSignature.length + 2, (int) crc32.getValue());
+ crc32.update(bb.array(), SevenZFile.SIGNATURE.length + 6, 20);
+ bb.putInt(SevenZFile.SIGNATURE.length + 2, (int) crc32.getValue());
bb.flip();
channel.write(bb);
}
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 c543da87c..3108e0b04 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
@@ -50,6 +50,7 @@
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.utils.InputStreamStatistics;
import org.apache.commons.io.input.BoundedInputStream;
+import org.apache.commons.lang3.ArrayUtils;
/**
* Implements an input stream that can read Zip archives.
@@ -217,31 +218,21 @@ private <T extends InputStream> T checkInputStream() throws ZipException {
private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
- private static boolean checkSig(final byte[] expected, final byte[] signature) {
- for (int i = 0; i < expected.length; i++) {
- if (signature[i] != expected[i]) {
- return false;
- }
- }
- return true;
- }
-
/**
* Checks if the signature matches what is expected for a ZIP file. Does not currently handle self-extracting ZIPs which may have arbitrary leading content.
*
- * @param signature the bytes to check
+ * @param buffer the bytes to check
* @param length the number of bytes to check
* @return true, if this stream is a ZIP archive stream, false otherwise
*/
- public static boolean matches(final byte[] signature, final int length) {
+ public static boolean matches(final byte[] buffer, final int length) {
if (length < ZipArchiveOutputStream.LFH_SIG.length) {
return false;
}
-
- return checkSig(ZipArchiveOutputStream.LFH_SIG, signature) // normal file
- || checkSig(ZipArchiveOutputStream.EOCD_SIG, signature) // empty zip
- || checkSig(ZipArchiveOutputStream.DD_SIG, signature) // split zip
- || checkSig(ZipLong.SINGLE_SEGMENT_SPLIT_MARKER.getBytes(), signature);
+ return ArrayUtils.startsWith(buffer, ZipArchiveOutputStream.LFH_SIG) // normal file
+ || ArrayUtils.startsWith(buffer, ZipArchiveOutputStream.EOCD_SIG) // empty zip
+ || ArrayUtils.startsWith(buffer, ZipArchiveOutputStream.DD_SIG) // split zip
+ || ArrayUtils.startsWith(buffer, ZipLong.SINGLE_SEGMENT_SPLIT_MARKER.getBytes());
}
/** The ZIP encoding to use for file names and the file comment. */
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index 036663cc0..5b8930b65 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -1008,11 +1008,15 @@ void testSevenZWithEOS() throws IOException {
@Test
void testSignatureCheck() {
- assertTrue(SevenZFile.matches(SevenZFile.sevenZSignature, SevenZFile.sevenZSignature.length));
- assertTrue(SevenZFile.matches(SevenZFile.sevenZSignature, SevenZFile.sevenZSignature.length + 1));
- assertFalse(SevenZFile.matches(SevenZFile.sevenZSignature, SevenZFile.sevenZSignature.length - 1));
- assertFalse(SevenZFile.matches(new byte[] { 1, 2, 3, 4, 5, 6 }, 6));
- assertTrue(SevenZFile.matches(new byte[] { '7', 'z', (byte) 0xBC, (byte) 0xAF, 0x27, 0x1C }, 6));
- assertFalse(SevenZFile.matches(new byte[] { '7', 'z', (byte) 0xBC, (byte) 0xAF, 0x27, 0x1D }, 6));
+ assertTrue(SevenZFile.matches(SevenZFile.SIGNATURE, SevenZFile.SIGNATURE.length));
+ assertTrue(SevenZFile.matches(SevenZFile.SIGNATURE, SevenZFile.SIGNATURE.length + 1));
+ final byte[] data0 = Arrays.copyOf(SevenZFile.SIGNATURE, SevenZFile.SIGNATURE.length - 1);
+ assertFalse(SevenZFile.matches(data0, data0.length));
+ final byte[] data1 = new byte[] { 1, 2, 3, 4, 5, 6 };
+ assertFalse(SevenZFile.matches(data1, data1.length));
+ final byte[] data2 = new byte[] { '7', 'z', (byte) 0xBC, (byte) 0xAF, 0x27, 0x1C };
+ assertTrue(SevenZFile.matches(data2, data2.length));
+ final byte[] data3 = new byte[] { '7', 'z', (byte) 0xBC, (byte) 0xAF, 0x27, 0x1D };
+ assertFalse(SevenZFile.matches(data3, data3.length));
}
}