Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,9 @@ private SevenZArchiveEntry computeIfAbsent(final Map<Integer, SevenZArchiveEntry
}

private InputStream getCurrentStream() throws IOException {
if (currentEntryIndex < 0) {
throw new ArchiveException("No current 7z entry (call getNextEntry() first).");
}
if (archive.files[currentEntryIndex].getSize() == 0) {
return new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,27 @@ void testSignatureCheck() {
final byte[] data3 = { '7', 'z', (byte) 0xBC, (byte) 0xAF, 0x27, 0x1D };
assertFalse(SevenZFile.matches(data3, data3.length));
}

/**
* Tests that reading before the first call to getNextEntry() reports the missing entry instead of an ArrayIndexOutOfBoundsException.
*/
@Test
void testReadBeforeGetNextEntry() throws Exception {
try (SevenZFile sevenZFile = getSevenZFile("bla.7z")) {
final ArchiveException e = assertThrows(ArchiveException.class, sevenZFile::read);
assertTrue(e.getMessage().contains("getNextEntry()"));
assertThrows(ArchiveException.class, () -> sevenZFile.read(new byte[1]));
}
try (SevenZFile sevenZFile = SevenZFile.builder().setByteArray(createEmptyArchive()).get()) {
assertThrows(ArchiveException.class, sevenZFile::read);
}
}

private static byte[] createEmptyArchive() throws IOException {
final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel();
try (SevenZOutputFile outArchive = new SevenZOutputFile(channel)) {
outArchive.finish();
}
return channel.toByteArray();
}
}