Skip to content

Commit 911819c

Browse files
authored
Merge pull request #32 from TheWakz/fix/adapting-reader-loop
fix: update offset increment in AdaptingZipReader buffer loop
2 parents ade1b35 + ec28ab0 commit 911819c

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/main/java/software/coley/lljzip/format/read/AdaptingZipReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void read(@Nonnull ZipArchive zip, @Nonnull MemorySegment data) throws IO
3838
int copyable = (int) Math.min(BUFFER_SIZE, length - offset);
3939
MemorySegment.copy(data, offset, wrapper, 0, copyable);
4040
os.write(buf, 0, copyable);
41-
offset += length;
41+
offset += copyable;
4242
}
4343
}
4444
fill(zip, temp);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package software.coley.lljzip;
2+
3+
import org.junit.jupiter.api.Test;
4+
import software.coley.lljzip.format.model.ZipArchive;
5+
import software.coley.lljzip.format.read.AdaptingZipReader;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.util.zip.CRC32;
10+
import java.util.zip.ZipEntry;
11+
import java.util.zip.ZipOutputStream;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
/**
16+
* Tests for {@link AdaptingZipReader} targeting buffer loop regressions.
17+
*
18+
* @author TheWakz
19+
*/
20+
public class AdaptingZipReaderTests {
21+
22+
@Test
23+
public void testReadArchiveExceedingBufferSize() throws IOException {
24+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
25+
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
26+
byte[] dummyData = new byte[20 * 1024];
27+
28+
ZipEntry entry = new ZipEntry("large_entry.txt");
29+
entry.setMethod(ZipEntry.STORED);
30+
entry.setSize(dummyData.length);
31+
entry.setCompressedSize(dummyData.length);
32+
33+
CRC32 crc = new CRC32();
34+
crc.update(dummyData);
35+
entry.setCrc(crc.getValue());
36+
37+
zos.putNextEntry(entry);
38+
zos.write(dummyData);
39+
zos.closeEntry();
40+
}
41+
byte[] zipBytes = baos.toByteArray();
42+
43+
assertTrue(zipBytes.length > 16384, "Test archive size must exceed 16KB");
44+
45+
ZipArchive archive = assertDoesNotThrow(() -> ZipIO.read(zipBytes, new AdaptingZipReader()),
46+
"Failed to read with read(adapting) when size exceeds buffer");
47+
48+
assertNotNull(archive, "Archive is null");
49+
assertNotNull(archive.getLocalFileByName("large_entry.txt"),
50+
"Missing 'large_entry.txt' file due to truncation");
51+
}
52+
}

0 commit comments

Comments
 (0)