From: PJ Fanning Date: Thu, 26 Oct 2023 17:37:46 +0000 (+0000) Subject: [bug-67579] revert changes - POI will again close the InputStream X-Git-Tag: REL_5_2_5~32 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ff5fc9d582844c7737582de8e20671e7c16faaf0;p=poi.git [bug-67579] revert changes - POI will again close the InputStream git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913356 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java index cf0202e3ad..2eb59e32f9 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -131,9 +131,6 @@ public final class ZipPackage extends OPCPackage { super(access); try (ZipArchiveThresholdInputStream zis = ZipHelper.openZipStream(in)) { this.zipArchive = new ZipInputStreamZipEntrySource(zis); - } catch (final IOException | RuntimeException e) { - IOUtils.closeQuietly(in); - throw e; } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/NoCloseInputStream.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/NoCloseInputStream.java deleted file mode 100644 index b27685aab2..0000000000 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/NoCloseInputStream.java +++ /dev/null @@ -1,33 +0,0 @@ -/* ==================================================================== - 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.poi.openxml4j.opc.internal; - -import org.apache.poi.util.Internal; - -import java.io.FilterInputStream; -import java.io.InputStream; - -@Internal -final class NoCloseInputStream extends FilterInputStream { - NoCloseInputStream(InputStream stream) { - super(stream); - } - - @Override - public void close() {} -} diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java index 7622b28b5c..26f3cffae1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java @@ -176,7 +176,7 @@ public final class ZipHelper { verifyZipHeader(checkedStream); // Open as a proper zip stream - return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(new NoCloseInputStream(checkedStream))); + return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(checkedStream)); } /** diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 2641862e74..f5eb9f18aa 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -1449,17 +1449,26 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { } @Test - void testWorkbookCloseDoesNotCloseInputStream() throws Exception { + void testWorkbookCloseClosesInputStream() throws Exception { try (WrappedStream stream = new WrappedStream( HSSFTestDataSamples.openSampleFileStream("github-321.xlsx"))) { try (XSSFWorkbook wb = new XSSFWorkbook(stream)) { XSSFSheet xssfSheet = wb.getSheetAt(0); assertNotNull(xssfSheet); } - assertFalse(stream.isClosed(), "stream should noy be closed by XSSFWorkbook"); + assertTrue(stream.isClosed(), "stream should be closed by XSSFWorkbook"); } } + static class NoCloseInputStream extends FilterInputStream { + NoCloseInputStream(InputStream stream) { + super(stream); + } + + @Override + public void close() {} + } + @Test void readFromZipStream() throws IOException { File tempFile = TempFile.createTempFile("poitest", ".zip"); @@ -1485,7 +1494,9 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { try (ZipArchiveInputStream zis = new ZipArchiveInputStream(Files.newInputStream(tempFile.toPath()))) { ZipArchiveEntry entry; while ((entry = zis.getNextZipEntry()) != null) { - XSSFWorkbook wb = new XSSFWorkbook(zis); + // NoCloseInputStream is needed to stop XSSFWorkbook closing the underlying InputStream + // this might not sound great but POI has worked like this for years and we can't just change it + XSSFWorkbook wb = new XSSFWorkbook(new NoCloseInputStream(zis)); assertNotNull(wb); count++; }