You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2018/04/04 19:41:11 UTC
svn commit: r1828377 - in /poi/trunk/src:
java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java
Author: centic
Date: Wed Apr 4 19:41:10 2018
New Revision: 1828377
URL: http://svn.apache.org/viewvc?rev=1828377&view=rev
Log:
Bug 62165: Do not close stream when opening succeeds
Modified:
poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java
Modified: poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java?rev=1828377&r1=1828376&r2=1828377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java Wed Apr 4 19:41:10 2018
@@ -114,8 +114,14 @@ public class OldExcelExtractor implement
: new BufferedInputStream(biffStream, 8);
if (FileMagic.valueOf(bis) == FileMagic.OLE2) {
- try (NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis)) {
+ NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis);
+ try {
open(poifs);
+ toClose = poifs; // Fixed by GR, we should not close it here
+ } finally {
+ if (toClose == null) {
+ poifs.close();
+ }
}
} else {
ris = new RecordInputStream(bis);
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java?rev=1828377&r1=1828376&r2=1828377&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java Wed Apr 4 19:41:10 2018
@@ -218,6 +218,22 @@ public final class TestOldExcelExtractor
}
}
+ @Test
+ public void testFromInputStream() throws IOException {
+ for (String ver : new String[] {"4", "5", "95"}) {
+ String filename = "testEXCEL_"+ver+".xls";
+ File f = HSSFTestDataSamples.getSampleFile(filename);
+
+ try (InputStream stream = new FileInputStream(f)) {
+ OldExcelExtractor extractor = new OldExcelExtractor(stream);
+ String text = extractor.getText();
+ assertNotNull(text);
+ assertTrue(text.length() > 100);
+ extractor.close();
+ }
+ }
+ }
+
@Test(expected=OfficeXmlFileException.class)
public void testOpenInvalidFile1() throws IOException {
// a file that exists, but is a different format
@@ -234,11 +250,8 @@ public final class TestOldExcelExtractor
@Test(expected=FileNotFoundException.class)
public void testOpenInvalidFile3() throws IOException {
// a POIFS file which is not a Workbook
- InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("47304.doc");
- try {
+ try (InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream("47304.doc")) {
new OldExcelExtractor(is).close();
- } finally {
- is.close();
}
}
@@ -252,65 +265,50 @@ public final class TestOldExcelExtractor
@Test
public void testInputStream() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls");
- InputStream stream = new FileInputStream(file);
- try {
+ try (InputStream stream = new FileInputStream(file)) {
OldExcelExtractor extractor = new OldExcelExtractor(stream);
String text = extractor.getText();
assertNotNull(text);
extractor.close();
- } finally {
- stream.close();
}
}
@Test
public void testInputStreamNPOIHeader() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
- InputStream stream = new FileInputStream(file);
- try {
+ try (InputStream stream = new FileInputStream(file)) {
OldExcelExtractor extractor = new OldExcelExtractor(stream);
extractor.close();
- } finally {
- stream.close();
}
}
@Test
public void testNPOIFSFileSystem() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
- try {
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
OldExcelExtractor extractor = new OldExcelExtractor(fs);
extractor.close();
- } finally {
- fs.close();
}
}
@Test
public void testDirectoryNode() throws IOException {
File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls");
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
- try {
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
extractor.close();
- } finally {
- fs.close();
}
}
@Test
public void testDirectoryNodeInvalidFile() throws IOException {
File file = POIDataSamples.getDocumentInstance().getFile("test.doc");
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
- try {
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file)) {
OldExcelExtractor extractor = new OldExcelExtractor(fs.getRoot());
extractor.close();
fail("Should catch exception here");
} catch (FileNotFoundException e) {
// expected here
- } finally {
- fs.close();
}
}
@@ -319,13 +317,10 @@ public final class TestOldExcelExtractor
public void testMainUsage() throws IOException {
PrintStream save = System.err;
try {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- try {
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
PrintStream str = new PrintStream(out, false, "UTF-8");
System.setErr(str);
- OldExcelExtractor.main(new String[] {});
- } finally {
- out.close();
+ OldExcelExtractor.main(new String[]{});
}
} finally {
System.setErr(save);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org