import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.model.RecordStream;
-import org.apache.poi.hssf.record.*;
+import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
+import org.apache.poi.hssf.record.BackupRecord;
+import org.apache.poi.hssf.record.DrawingGroupRecord;
+import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.FontRecord;
+import org.apache.poi.hssf.record.LabelRecord;
+import org.apache.poi.hssf.record.LabelSSTRecord;
+import org.apache.poi.hssf.record.NameRecord;
+import org.apache.poi.hssf.record.RecalcIdRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RecordFactory;
+import org.apache.poi.hssf.record.SSTRecord;
+import org.apache.poi.hssf.record.UnknownRecord;
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.EntryUtils;
import org.apache.poi.poifs.filesystem.FilteringDirectoryNode;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.Ole10Native;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.FormulaShifter;
return style;
}
+
+ /**
+ * Closes the underlying {@link NPOIFSFileSystem} from which
+ * the Workbook was read, if any. Has no effect on Workbooks
+ * opened from an InputStream, or newly created ones.
+ */
+ @Override
+ public void close() throws IOException
+ {
+ if (directory != null) {
+ if (directory.getNFileSystem() != null) {
+ directory.getNFileSystem().close();
+ directory = null;
+ }
+ }
+ }
/**
* Method write - write out this workbook to an Outputstream. Constructs
* @exception IOException if anything can't be written.
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem
*/
-
@Override
public void write(OutputStream stream)
throws IOException
package org.apache.poi.ss.usermodel;
+import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
* will construct whether they are reading or writing a workbook. It is also the
* top level object for creating new sheets/etc.
*/
-public interface Workbook {
+public interface Workbook extends Closeable {
/** Extended windows meta file */
public static final int PICTURE_TYPE_EMF = 2;
*/
void write(OutputStream stream) throws IOException;
+ /**
+ * Close the underlying input resource (File or Stream),
+ * from which the Workbook was read. After closing, the
+ * Workbook should no longer be used.
+ * <p>This will have no effect newly created Workbooks.
+ */
+ void close() throws IOException;
+
/**
* @return the total number of defined names in this workbook
*/
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
onDocumentRead();
context.clear();
}
+
+ /**
+ * Closes the underlying {@link OPCPackage} from which this
+ * document was read, if there is one
+ */
+ protected void close() throws IOException {
+ if (pkg != null) {
+ if (pkg.getPackageAccess() == PackageAccess.READ) {
+ pkg.revert();
+ } else {
+ pkg.close();
+ }
+ pkg = null;
+ }
+ }
/**
* Write out this document to an Outputstream.
* Constructs a XSSFWorkbook object given a OpenXML4J <code>Package</code> object,
* see <a href="http://poi.apache.org/oxml4j/">http://poi.apache.org/oxml4j/</a>.
*
- * Once you have finished working with the Workbook, you should close the package
- * by calling pkg.close, to avoid leaving file handles open.
+ * <p>Once you have finished working with the Workbook, you should close the package
+ * by calling either {@link #close()} or {@link OPCPackage#close()}, to avoid
+ * leaving file handles open.
*
- * Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory
+ * <p>Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory
* footprint than an InputStream backed one.
*
* @param pkg the OpenXML4J <code>OPC Package</code> object.
* Constructs a XSSFWorkbook object, by buffering the whole stream into memory
* and then opening an {@link OPCPackage} object for it.
*
- * Using an {@link InputStream} requires more memory than using a File, so
+ * <p>Using an {@link InputStream} requires more memory than using a File, so
* if a {@link File} is available then you should instead do something like
* <pre><code>
* OPCPackage pkg = OPCPackage.open(path);
workbook.save(out, xmlOptions);
out.close();
}
+
+ /**
+ * Closes the underlying {@link OPCPackage} from which
+ * the Workbook was read, if any. Has no effect on newly
+ * created Workbooks.
+ */
+ @Override
+ public void close() throws IOException {
+ super.close();
+ }
/**
* Returns SharedStringsTable - tha cache of string for this workbook