Author: onealj
Date: Tue Jun 20 07:28:14 2017
New Revision: 1799308

URL: http://svn.apache.org/viewvc?rev=1799308&view=rev
Log:
bug 57919: add functions to write and close a POIDocument, guaranteeing closure 
of the output stream and document.

Modified:
    poi/trunk/src/java/org/apache/poi/util/IOUtils.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=1799308&r1=1799307&r2=1799308&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 07:28:14 2017
@@ -19,6 +19,7 @@ package org.apache.poi.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -29,6 +30,7 @@ import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
 import org.apache.poi.EmptyFileException;
+import org.apache.poi.POIDocument;
 
 public final class IOUtils {
     private static final POILogger logger = POILogFactory.getLogger( 
IOUtils.class );
@@ -187,6 +189,82 @@ public final class IOUtils {
             }
         }
     }
+    
+    /**
+     * 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.
+     * 
+     * If you are using Java 7 or higher, you may prefer to use a 
try-with-resources statement instead.
+     * This function exists for Java 6 code.
+     *
+     * @param doc  a writeable document to write to the output stream
+     * @param out  the output stream that the document is written to
+     * @throws IOException 
+     */
+    public static void write(POIDocument 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.
+     * This will also attempt to close the document, even if an error occurred 
while writing the document or closing the output stream.
+     * 
+     * If you are using Java 7 or higher, you may prefer to use a 
try-with-resources statement instead.
+     * This function exists for Java 6 code.
+     *
+     * @param doc  a writeable and closeable document to write to the output 
stream, then close
+     * @param out  the output stream that the document is written to
+     * @throws IOException 
+     */
+    public static void writeAndClose(POIDocument doc, OutputStream out) throws 
IOException {
+        try {
+            write(doc, out);
+        } finally {
+            closeQuietly(doc);
+        }
+    }
+    
+    /**
+     * Like {@link #writeAndClose(POIDocument, OutputStream)}, but for writing 
to a File instead of an OutputStream.
+     * This will attempt to close the document, even if an error occurred 
while writing the document.
+     * 
+     * If you are using Java 7 or higher, you may prefer to use a 
try-with-resources statement instead.
+     * This function exists for Java 6 code.
+     *
+     * @param doc  a writeable and closeable document to write to the output 
file, then close
+     * @param out  the output file that the document is written to
+     * @throws IOException 
+     */
+    public static void writeAndClose(POIDocument doc, File out) throws 
IOException {
+        try {
+            doc.write(out);
+        } finally {
+            closeQuietly(doc);
+        }
+    }
+    
+    /**
+     * Like {@link #write(POIDocument, File)}, but for writing a POI Document 
in place (to the same file that it was opened from).
+     * This will attempt to close the document, even if an error occurred 
while writing the document.
+     * 
+     * If you are using Java 7 or higher, you may prefer to use a 
try-with-resources statement instead.
+     * This function exists for Java 6 code.
+     *
+     * @param doc  a writeable document to write in-place
+     * @throws IOException 
+     */
+    public static void writeAndClose(POIDocument doc) throws IOException {
+        try {
+            doc.write();
+        } finally {
+            closeQuietly(doc);
+        }
+    }
 
     /**
      * Copies all the data from the given InputStream to the OutputStream. It



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to