[ 
https://issues.apache.org/jira/browse/COMPRESS-510?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17087411#comment-17087411
 ] 

Robin Schimpf commented on COMPRESS-510:
----------------------------------------

Sorry to come back at this issue. The testcase I provided works now fine but my 
application testcase now fails with a different exception.
I generate a 7z file with commons-compress and then try to read the content 
multiple times. So this error seems to be archive dependent.

Some reduced sample code to recreate the error
{code:java}
@Test
public void retrieveInputStreamForAllEntriesMultipleTimes() throws IOException {
    try (final SevenZOutputFile out = new SevenZOutputFile(new File(dir, 
"test.7z"))) {
        final Path inputFile = Files.createTempFile("SevenZTestTemp", "");

        SevenZArchiveEntry entry = out.createArchiveEntry(inputFile.toFile(), 
"test.txt");
        out.putArchiveEntry(entry);
        out.write("Test".getBytes(StandardCharsets.UTF_8));
        out.closeArchiveEntry();

        Files.deleteIfExists(inputFile);
    }

    try (SevenZFile sevenZFile = new SevenZFile(new File(dir, "test.7z"))) {
        for (SevenZArchiveEntry entry : sevenZFile.getEntries()) {
            byte[] firstRead = 
IOUtils.toByteArray(sevenZFile.getInputStream(entry));
            byte[] secondRead = 
IOUtils.toByteArray(sevenZFile.getInputStream(entry));
            assertArrayEquals(firstRead, secondRead);
        }
    }
}
{code}
This fails now with the following exception on Java 8
{code}
java.lang.ClassCastException: 
org.apache.commons.compress.utils.BoundedInputStream cannot be cast to 
org.apache.commons.compress.utils.CRC32VerifyingInputStream

        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.skipEntriesWhenNeeded(SevenZFile.java:1278)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecodingStream(SevenZFile.java:1204)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.getInputStream(SevenZFile.java:1437)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFileTest.retrieveInputStreamForAllEntriesMultipleTimes(SevenZFileTest.java:712)
{code}
And with the following exception on Java 11
{code}
java.lang.ClassCastException: class 
org.apache.commons.compress.utils.BoundedInputStream cannot be cast to class 
org.apache.commons.compress.utils.CRC32VerifyingInputStream 
(org.apache.commons.compress.utils.BoundedInputStream and 
org.apache.commons.compress.utils.CRC32VerifyingInputStream are in unnamed 
module of loader 'app')

        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.skipEntriesWhenNeeded(SevenZFile.java:1278)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecodingStream(SevenZFile.java:1204)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFile.getInputStream(SevenZFile.java:1437)
        at 
org.apache.commons.compress.archivers.sevenz.SevenZFileTest.retrieveInputStreamForAllEntriesMultipleTimes(SevenZFileTest.java:712)
{code}

> Multiple retrievals of InputStream for same SevenZFile entry fails
> ------------------------------------------------------------------
>
>                 Key: COMPRESS-510
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-510
>             Project: Commons Compress
>          Issue Type: Bug
>    Affects Versions: 1.20
>            Reporter: Robin Schimpf
>            Priority: Major
>
> I was trying out the new random access for the 7z files and have one of our 
> tests failing where we are trying to read the same entry multiple times 
> without closing the archive.
> Reproducing test case (I added this locally to the SevenZFileTest class)
> {code:java}
> @Test
> public void retrieveInputStreamForEntryMultipleTimes() throws IOException {
>     try (SevenZFile sevenZFile = new SevenZFile(getFile("bla.7z"))) {
>         for (SevenZArchiveEntry entry : sevenZFile.getEntries()) {
>             byte[] firstRead = 
> IOUtils.toByteArray(sevenZFile.getInputStream(entry));
>             byte[] secondRead = 
> IOUtils.toByteArray(sevenZFile.getInputStream(entry));
>             assertArrayEquals(firstRead, secondRead);
>         }
>     }
> }
> {code}
> The Exception thrown is
> {code:java}
> java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 2 
> at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecodingStream(SevenZFile.java:1183)
>       at 
> org.apache.commons.compress.archivers.sevenz.SevenZFile.getInputStream(SevenZFile.java:1436)
>       at 
> org.apache.commons.compress.archivers.sevenz.SevenZFileTest.retrieveInputStreamForEntryMultipleTimes(SevenZFileTest.java:688)
>       ...
> {code}
> A similar test case for e.g. zip works fine
> {code:java}
> @Test
> public void retrieveInputStreamForEntryMultipleTimes() throws IOException {
>     try (ZipFile zipFile = new ZipFile(getFile("bla.zip"))) {
>         Enumeration<ZipArchiveEntry> entry = zipFile.getEntries();
>         while (entry.hasMoreElements()) {
>             ZipArchiveEntry e = entry.nextElement();
>             byte[] firstRead = IOUtils.toByteArray(zipFile.getInputStream(e));
>             byte[] secondRead = 
> IOUtils.toByteArray(zipFile.getInputStream(e));
>             assertArrayEquals(firstRead, secondRead);
>         }
>     }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to