You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2017/06/20 08:13:59 UTC
svn commit: r1799316 - in /poi/trunk/src:
java/org/apache/poi/util/IOUtils.java
ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java
Author: onealj
Date: Tue Jun 20 08:13:58 2017
New Revision: 1799316
URL: http://svn.apache.org/viewvc?rev=1799316&view=rev
Log:
bug 57919: close opened resources
Modified:
poi/trunk/src/java/org/apache/poi/util/IOUtils.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java
Modified: poi/trunk/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/IOUtils.java?rev=1799316&r1=1799315&r2=1799316&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/IOUtils.java Tue Jun 20 08:13:58 2017
@@ -31,6 +31,7 @@ import java.util.zip.Checksum;
import org.apache.poi.EmptyFileException;
import org.apache.poi.POIDocument;
+import org.apache.poi.ss.usermodel.Workbook;
public final class IOUtils {
private static final POILogger logger = POILogFactory.getLogger( IOUtils.class );
@@ -209,6 +210,14 @@ public final class IOUtils {
}
}
+ public static void write(Workbook doc, OutputStream out) throws IOException {
+ try {
+ doc.write(out);
+ } finally {
+ closeQuietly(out);
+ }
+ }
+
/**
* Write a POI Document ({@link org.apache.poi.ss.usermodel.Workbook}, {@link org.apache.poi.sl.usermodel.SlideShow}, etc) to an output stream and close the output stream.
* This will attempt to close the output stream at the end even if there was a problem writing the document to the stream.
@@ -264,6 +273,16 @@ public final class IOUtils {
} finally {
closeQuietly(doc);
}
+ }
+
+ // Since the Workbook interface doesn't derive from POIDocument
+ // We'll likely need one of these for each document container interface
+ public static void writeAndClose(Workbook doc, OutputStream out) throws IOException {
+ try {
+ doc.write(out);
+ } finally {
+ closeQuietly(doc);
+ }
}
/**
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java?rev=1799316&r1=1799315&r2=1799316&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java Tue Jun 20 08:13:58 2017
@@ -24,11 +24,11 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -39,7 +39,7 @@ import org.apache.poi.xssf.usermodel.XSS
*/
public class XSSFTestDataSamples {
/**
- * Used by {@link writeOutAndReadBack(R wb, String testName)}. If a
+ * Used by {@link #writeOutAndReadBack(Workbook, String)}. If a
* value is set for this in the System Properties, the xlsx file
* will be written out to that directory.
*/
@@ -74,6 +74,21 @@ public class XSSFTestDataSamples {
* @throws IOException
*/
public static <R extends Workbook> File writeOut(R wb, String testName) throws IOException {
+ final File file = getOutputFile(testName);
+ writeOut(wb, file);
+ return file;
+ }
+
+ private static <R extends Workbook> void writeOut(R wb, File file) throws IOException {
+ IOUtils.write(wb, new FileOutputStream(file));
+ }
+
+ // Anticipates the location of where a workbook will be written to
+ // Note that if TEST_OUTPUT_DIR is not set, this will create temporary files
+ // with unique names. Subsequent calls with the same argument may return a different file.
+ // Gets a test data sample file, deleting the file if it exists.
+ // This is used in preparation for writing a workbook out to the returned output file.
+ private static File getOutputFile(String testName) throws IOException {
final String testOutputDir = System.getProperty(TEST_OUTPUT_DIR);
final File file;
if (testOutputDir != null) {
@@ -85,12 +100,6 @@ public class XSSFTestDataSamples {
if (file.exists()) {
file.delete();
}
- final OutputStream out = new FileOutputStream(file);
- try {
- wb.write(out);
- } finally {
- out.close();
- }
return file;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org