Use IOUtils.closeQuietly() in more places

Avoid two possible file-handle leaks when opening files fails with an exception
Make tests close resources properly to not spam the output when running with file-leak-detector

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1763485 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-10-05 20:00:07 +00:00
parent 89f68a6a47
commit cdef5dc16c
5 changed files with 96 additions and 66 deletions

View File

@ -54,6 +54,7 @@ import org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnma
import org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext; import org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext;
import org.apache.poi.openxml4j.util.Nullable; import org.apache.poi.openxml4j.util.Nullable;
import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.openxml4j.util.ZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.NotImplemented; import org.apache.poi.util.NotImplemented;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
@ -221,18 +222,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
// pack.originalPackagePath = file.getAbsolutePath(); // pack.originalPackagePath = file.getAbsolutePath();
return pack; return pack;
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
try { IOUtils.closeQuietly(pack);
pack.close();
} catch (IOException e1) {
throw new IllegalStateException(e);
}
throw e; throw e;
} catch (RuntimeException e) { } catch (RuntimeException e) {
try { IOUtils.closeQuietly(pack);
pack.close();
} catch (IOException e1) {
throw new IllegalStateException(e);
}
throw e; throw e;
} }
} }
@ -277,6 +270,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
} }
} }
} }
pack.originalPackagePath = new File(path).getAbsolutePath(); pack.originalPackagePath = new File(path).getAbsolutePath();
return pack; return pack;
} }
@ -310,18 +304,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
pack.originalPackagePath = file.getAbsolutePath(); pack.originalPackagePath = file.getAbsolutePath();
return pack; return pack;
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
try { IOUtils.closeQuietly(pack);
pack.close();
} catch (IOException e1) {
throw new IllegalStateException(e);
}
throw e; throw e;
} catch (RuntimeException e) { } catch (RuntimeException e) {
try { IOUtils.closeQuietly(pack);
pack.close();
} catch (IOException e1) {
throw new IllegalStateException(e);
}
throw e; throw e;
} }
} }
@ -340,9 +326,17 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
public static OPCPackage open(InputStream in) throws InvalidFormatException, public static OPCPackage open(InputStream in) throws InvalidFormatException,
IOException { IOException {
OPCPackage pack = new ZipPackage(in, PackageAccess.READ_WRITE); OPCPackage pack = new ZipPackage(in, PackageAccess.READ_WRITE);
try {
if (pack.partList == null) { if (pack.partList == null) {
pack.getParts(); pack.getParts();
} }
} catch (InvalidFormatException e) {
IOUtils.closeQuietly(pack);
throw e;
} catch (RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
}
return pack; return pack;
} }
@ -357,13 +351,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
* Throws if the specified file exist and is not valid. * Throws if the specified file exist and is not valid.
*/ */
public static OPCPackage openOrCreate(File file) throws InvalidFormatException { public static OPCPackage openOrCreate(File file) throws InvalidFormatException {
OPCPackage retPackage = null;
if (file.exists()) { if (file.exists()) {
retPackage = open(file.getAbsolutePath()); return open(file.getAbsolutePath());
} else { } else {
retPackage = create(file); return create(file);
} }
return retPackage;
} }
/** /**

View File

@ -17,11 +17,7 @@
package org.apache.poi.hwpf.usermodel; package org.apache.poi.hwpf.usermodel;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocument;
@ -90,10 +86,17 @@ public final class TestHWPFWrite extends HWPFTestCase {
public void testInPlaceWrite() throws Exception { public void testInPlaceWrite() throws Exception {
// Setup as a copy of a known-good file // Setup as a copy of a known-good file
final File file = TempFile.createTempFile("TestDocument", ".doc"); final File file = TempFile.createTempFile("TestDocument", ".doc");
IOUtils.copy( InputStream inputStream = POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc");
POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"), try {
new FileOutputStream(file) FileOutputStream outputStream = new FileOutputStream(file);
); try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
}
} finally {
inputStream.close();
}
// Open from the temp file in read-write mode // Open from the temp file in read-write mode
HWPFDocument doc = new HWPFDocument(new NPOIFSFileSystem(file, false).getRoot()); HWPFDocument doc = new HWPFDocument(new NPOIFSFileSystem(file, false).getRoot());
@ -108,7 +111,9 @@ public final class TestHWPFWrite extends HWPFTestCase {
doc.close(); doc.close();
doc = new HWPFDocument(new NPOIFSFileSystem(file).getRoot()); doc = new HWPFDocument(new NPOIFSFileSystem(file).getRoot());
r = doc.getRange();
assertEquals("X XX a test document\r", r.getParagraph(0).text()); assertEquals("X XX a test document\r", r.getParagraph(0).text());
doc.close();
} }
@SuppressWarnings("resource") @SuppressWarnings("resource")
@ -121,7 +126,10 @@ public final class TestHWPFWrite extends HWPFTestCase {
try { try {
doc.write(); doc.write();
fail("Shouldn't work for InputStream"); fail("Shouldn't work for InputStream");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
doc.close();
// Can't work for OPOIFS // Can't work for OPOIFS
OPOIFSFileSystem ofs = new OPOIFSFileSystem( OPOIFSFileSystem ofs = new OPOIFSFileSystem(
@ -130,7 +138,10 @@ public final class TestHWPFWrite extends HWPFTestCase {
try { try {
doc.write(); doc.write();
fail("Shouldn't work for OPOIFSFileSystem"); fail("Shouldn't work for OPOIFSFileSystem");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
doc.close();
// Can't work for Read-Only files // Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem( NPOIFSFileSystem fs = new NPOIFSFileSystem(
@ -139,6 +150,9 @@ public final class TestHWPFWrite extends HWPFTestCase {
try { try {
doc.write(); doc.write();
fail("Shouldn't work for Read Only"); fail("Shouldn't work for Read Only");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
doc.close();
} }
} }

View File

@ -1222,7 +1222,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
try { try {
wb.write(); wb.write();
fail("Shouldn't work for new files"); fail("Shouldn't work for new files");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for InputStream opened files // Can't work for InputStream opened files
wb = new HSSFWorkbook( wb = new HSSFWorkbook(
@ -1230,7 +1233,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
try { try {
wb.write(); wb.write();
fail("Shouldn't work for InputStream"); fail("Shouldn't work for InputStream");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for OPOIFS // Can't work for OPOIFS
OPOIFSFileSystem ofs = new OPOIFSFileSystem( OPOIFSFileSystem ofs = new OPOIFSFileSystem(
@ -1239,7 +1245,10 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
try { try {
wb.write(); wb.write();
fail("Shouldn't work for OPOIFSFileSystem"); fail("Shouldn't work for OPOIFSFileSystem");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
wb.close();
// Can't work for Read-Only files // Can't work for Read-Only files
NPOIFSFileSystem fs = new NPOIFSFileSystem( NPOIFSFileSystem fs = new NPOIFSFileSystem(
@ -1248,17 +1257,27 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
try { try {
wb.write(); wb.write();
fail("Shouldn't work for Read Only"); fail("Shouldn't work for Read Only");
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// expected here
}
wb.close();
} }
@Test @Test
public void inPlaceWrite() throws Exception { public void inPlaceWrite() throws Exception {
// Setup as a copy of a known-good file // Setup as a copy of a known-good file
final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls"); final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
IOUtils.copy( InputStream inputStream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls");
POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"), try {
new FileOutputStream(file) FileOutputStream outputStream = new FileOutputStream(file);
); try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
}
} finally {
inputStream.close();
}
// Open from the temp file in read-write mode // Open from the temp file in read-write mode
HSSFWorkbook wb = new HSSFWorkbook(new NPOIFSFileSystem(file, false)); HSSFWorkbook wb = new HSSFWorkbook(new NPOIFSFileSystem(file, false));
@ -1276,6 +1295,8 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb = new HSSFWorkbook(new NPOIFSFileSystem(file)); wb = new HSSFWorkbook(new NPOIFSFileSystem(file));
assertEquals(1, wb.getNumberOfSheets()); assertEquals(1, wb.getNumberOfSheets());
assertEquals("Changed!", wb.getSheetAt(0).getRow(0).getCell(0).toString()); assertEquals("Changed!", wb.getSheetAt(0).getRow(0).getCell(0).toString());
wb.close();
} }
@Test @Test

View File

@ -24,11 +24,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Iterator; import java.util.Iterator;
@ -103,10 +99,15 @@ public final class TestNPOIFSFileSystem {
original.close(); original.close();
return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray())); return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
} }
protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException { protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException {
final File file = TempFile.createTempFile("TestPOIFS", ".ole2"); final File file = TempFile.createTempFile("TestPOIFS", ".ole2");
final FileOutputStream fout = new FileOutputStream(file); final OutputStream fout = new FileOutputStream(file);
try {
original.writeFilesystem(fout); original.writeFilesystem(fout);
} finally {
fout.close();
}
original.close(); original.close();
return new NPOIFSFileSystem(file, false); return new NPOIFSFileSystem(file, false);
} }

View File

@ -1017,7 +1017,7 @@ public abstract class BaseTestCell {
} }
@Test @Test
public void primitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() { public void primitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() throws IOException {
// bug 59836 // bug 59836
// until we have changes POI from working on primitives (int) to enums, // until we have changes POI from working on primitives (int) to enums,
// we should make sure we minimize backwards compatibility breakages. // we should make sure we minimize backwards compatibility breakages.
@ -1046,5 +1046,7 @@ public abstract class BaseTestCell {
default: default:
fail("unexpected cell type: " + cell.getCellType()); fail("unexpected cell type: " + cell.getCellType());
} }
wb.close();
} }
} }