[ 
https://issues.apache.org/jira/browse/COMPRESS-514?focusedWorklogId=727535&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-727535
 ]

ASF GitHub Bot logged work on COMPRESS-514:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 15/Feb/22 18:59
            Start Date: 15/Feb/22 18:59
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on a change in pull request #98:
URL: https://github.com/apache/commons-compress/pull/98#discussion_r805984987



##########
File path: 
src/main/java/org/apache/commons/compress/archivers/sevenz/HeaderChannelBuffer.java
##########
@@ -0,0 +1,174 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.sevenz;
+
+import org.apache.commons.compress.utils.IOUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.util.zip.CRC32;
+
+/**
+ * Enables little-endian primitive type reads from a {@link 
ReadableByteChannel}
+ * or {@link InputStream}, internally using a paged-in {@link ByteBuffer}.
+ * <br>
+ * Access is serial only but does allow a
+ * virtual buffer capacity of {@code Long.MAX_VALUE}.
+ * If the requested capacity is within the maximum page size (default 16MiB)
+ * the buffer will be fully read and held in a {@link HeaderInMemoryBuffer}.
+ *
+ * @NotThreadSafe
+ * @since 1.21
+ */
+class HeaderChannelBuffer implements HeaderBuffer {
+    private static final int DEFAULT_PAGE_MAX = 16_777_216;
+    // This must match the largest get<Element> (currently getLong)
+    private static final int MAX_GET_ELEMENT_SIZE = 8;
+    private final ReadableByteChannel channel;
+    private final ByteBuffer buffer;
+    private long remaining;
+
+    private HeaderChannelBuffer(final ReadableByteChannel channel, final long 
capacity, final int maxPageBytes) {
+        this.channel = channel;
+        int limit = (int) Math.min(maxPageBytes, capacity);
+        this.buffer = 
ByteBuffer.allocate(limit).order(ByteOrder.LITTLE_ENDIAN);
+        this.remaining = capacity;
+    }
+
+    public static HeaderBuffer create(final ReadableByteChannel channel, final 
long capacity, final int maxPageBytes)
+            throws IOException {
+        if (maxPageBytes < MAX_GET_ELEMENT_SIZE) {
+            throw new IllegalArgumentException("Page size must be at least " + 
MAX_GET_ELEMENT_SIZE);
+        }
+        if (capacity <= maxPageBytes) {
+            ByteBuffer buf = ByteBuffer.allocate((int) 
capacity).order(ByteOrder.LITTLE_ENDIAN);
+            IOUtils.readFully(channel, buf);
+            buf.flip();
+            return new HeaderInMemoryBuffer(buf);
+        }
+        HeaderChannelBuffer channelBuffer = new HeaderChannelBuffer(channel, 
capacity, maxPageBytes);
+        channelBuffer.fill();
+        return channelBuffer;
+    }
+
+    public static HeaderBuffer create(final ReadableByteChannel channel, final 
long capacity) throws IOException {
+        return HeaderChannelBuffer.create(channel, capacity, DEFAULT_PAGE_MAX);
+    }
+
+    public static HeaderBuffer create(final InputStream inputStream, final 
long capacity, final int maxPageBytes)
+            throws IOException {
+        return create(Channels.newChannel(inputStream), capacity, 
maxPageBytes);
+    }
+
+    public static HeaderBuffer create(final InputStream inputStream, final 
long capacity) throws IOException {
+        return create(Channels.newChannel(inputStream), capacity, 
DEFAULT_PAGE_MAX);
+    }
+
+    @Override
+    public boolean hasCRC() {
+        return false;
+    }
+
+    @Override
+    public CRC32 getCRC() throws IOException {
+        throw new IOException("CRC is not implemented for this header type");
+    }
+
+    @Override
+    public void get(byte[] dst) throws IOException {
+        int remainingBytes = dst.length;
+        do {
+            int length = Math.min(buffer.remaining(), remainingBytes);
+            buffer.get(dst, dst.length - remainingBytes, length);
+            remainingBytes -= length;
+        } while (refilled(remainingBytes));
+    }
+
+    private boolean refilled(final int remainingBytes) throws IOException {
+        if (remainingBytes <= 0) {
+            return false;
+               }

Review comment:
       Fix formatting please.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 727535)
    Time Spent: 2h 20m  (was: 2h 10m)

> SevenZFile fails with encoded header over 2GiB
> ----------------------------------------------
>
>                 Key: COMPRESS-514
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-514
>             Project: Commons Compress
>          Issue Type: Bug
>          Components: Archivers
>    Affects Versions: 1.20
>            Reporter: A Kelday
>            Priority: Minor
>         Attachments: HeaderChannelBuffer.java
>
>          Time Spent: 2h 20m
>  Remaining Estimate: 0h
>
> When reading what some may call a large encrypted 7zip file (1.2TB with 22 
> million files), the read fails at the header stage with the trace below. Is 
> this within the spec? I've written some code to handle it, because I did 
> actually need to extract the file in java. If that's of any use I can provide 
> it (it's a naive wrapper that just pages in a buffer at a time).
>  
> {code:java}
> Exception in thread "main" java.io.IOException: Cannot handle 
> unpackSize2416988886
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.assertFitsIntoInt(SevenZFile.java:1523)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.readEncodedHeader(SevenZFile.java:622)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.initializeArchive(SevenZFile.java:532)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.readHeaders(SevenZFile.java:468)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:337)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:129)
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:116)
> {code}
> 7zip itself can also open it (and display/extract etc.), here are the stats:
>  
>  
> {code:java}
> Size: 2 489 903 580 875
> Packed Size: 1 349 110 308 832
> Folders: 40 005
> Files: 22 073 957
> CRC: E26F6A96
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to