commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [commons-compress] 02/03: COMPRESS-497 review cosmetics
Date Sat, 02 Nov 2019 16:21:43 GMT
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 21ee2655b2a01c251855382d8d3f69f2fe813349
Author: Stefan Bodewig <bodewig@apache.org>
AuthorDate: Sat Nov 2 17:18:32 2019 +0100

    COMPRESS-497 review cosmetics
---
 .../compress/archivers/sevenz/SevenZFile.java      | 71 ++++++++++++----------
 1 file changed, 38 insertions(+), 33 deletions(-)

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 f85d202..1eb182e 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
@@ -465,43 +465,48 @@ public class SevenZFile implements Closeable {
             return initializeArchive(startHeader, password, true);
         } else {
             // No valid header found - probably first file of multipart archive was removed
too early. Scan for end header.
-            ByteBuffer nidBuf = ByteBuffer.allocate(1);
-            final long searchLimit = 1024 * 1024 * 1;
-            final long previousDataSize = channel.position() + 20;  // Main header, plus
bytes that readStartHeader would read
-            final long minPos;
-            // Determine minimal position - can't start before current position
-            if (channel.position() + searchLimit > channel.size()) {
-                minPos = channel.position();
-            } else {
-                minPos = channel.size() - searchLimit;
-            }
-            long pos = channel.size() - 1;
-            // Loop: Try from end of archive
-            while (pos > minPos) {
-                pos--;
-                channel.position(pos);
-                nidBuf.rewind();
-                channel.read(nidBuf);
-                int nid = nidBuf.array()[0];
-                // First indicator: Byte equals one of these header identifiers
-                if ((nid == NID.kEncodedHeader) || (nid == NID.kHeader)) {
-                    try {
-                        // Try to initialize Archive structure from here
-                        final StartHeader startHeader = new StartHeader();
-                        startHeader.nextHeaderOffset = pos - previousDataSize;
-                        startHeader.nextHeaderSize = channel.size() - pos;
-                        Archive result = initializeArchive(startHeader, password, false);
-                        // Sanity check: There must be some data...
-                        if (result.packSizes!=null && result.files.length>0) {
-                            return result;
-                        }
-                    } catch (Exception ignore) {
-                        // Wrong guess...
+            return tryToLocateEndHeader(password);
+        }
+    }
+
+    private Archive tryToLocateEndHeader(final byte[] password) throws IOException {
+        ByteBuffer nidBuf = ByteBuffer.allocate(1);
+        final long searchLimit = 1024 * 1024 * 1;
+        // Main header, plus bytes that readStartHeader would read
+        final long previousDataSize = channel.position() + 20;
+        final long minPos;
+        // Determine minimal position - can't start before current position
+        if (channel.position() + searchLimit > channel.size()) {
+            minPos = channel.position();
+        } else {
+            minPos = channel.size() - searchLimit;
+        }
+        long pos = channel.size() - 1;
+        // Loop: Try from end of archive
+        while (pos > minPos) {
+            pos--;
+            channel.position(pos);
+            nidBuf.rewind();
+            channel.read(nidBuf);
+            int nid = nidBuf.array()[0];
+            // First indicator: Byte equals one of these header identifiers
+            if (nid == NID.kEncodedHeader || nid == NID.kHeader) {
+                try {
+                    // Try to initialize Archive structure from here
+                    final StartHeader startHeader = new StartHeader();
+                    startHeader.nextHeaderOffset = pos - previousDataSize;
+                    startHeader.nextHeaderSize = channel.size() - pos;
+                    Archive result = initializeArchive(startHeader, password, false);
+                    // Sanity check: There must be some data...
+                    if (result.packSizes != null && result.files.length > 0) {
+                        return result;
                     }
+                } catch (Exception ignore) {
+                    // Wrong guess...
                 }
             }
-            throw new IOException("Start header corrupt and unable to guess end header");
         }
+        throw new IOException("Start header corrupt and unable to guess end header");
     }
 
     private Archive initializeArchive(StartHeader startHeader, final byte[] password, boolean
verifyCrc) throws IOException {


Mime
View raw message