Skip to content

Commit

Permalink
Partial fix for #56537 - Have Workbook offer a close() method, which …
Browse files Browse the repository at this point in the history
…in turn closes the NPOIFS or OPC resource from which it was loaded

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1601901 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Gagravarr committed Jun 11, 2014
1 parent 1bcf519 commit 47a8f6c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
32 changes: 30 additions & 2 deletions src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,27 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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.DirectoryEntry;
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;
Expand Down Expand Up @@ -1178,6 +1191,22 @@ public HSSFCellStyle getCellStyleAt(short idx)

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
Expand All @@ -1189,7 +1218,6 @@ public HSSFCellStyle getCellStyleAt(short idx)
* @exception IOException if anything can't be written.
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem
*/

@Override
public void write(OutputStream stream)
throws IOException
Expand Down
11 changes: 10 additions & 1 deletion src/java/org/apache/poi/ss/usermodel/Workbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.ss.usermodel;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
Expand All @@ -30,7 +31,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
* 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;
Expand Down Expand Up @@ -350,6 +351,14 @@ public interface Workbook {
*/
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
*/
Expand Down
16 changes: 16 additions & 0 deletions src/ooxml/java/org/apache/poi/POIXMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -159,6 +160,21 @@ protected final void load(POIXMLFactory factory) throws IOException {
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.
Expand Down
19 changes: 15 additions & 4 deletions src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ public XSSFWorkbook() {
* 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.
Expand All @@ -218,7 +219,7 @@ public XSSFWorkbook(OPCPackage pkg) throws IOException {
* 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);
Expand Down Expand Up @@ -1350,6 +1351,16 @@ protected void commit() throws IOException {
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
Expand Down

0 comments on commit 47a8f6c

Please sign in to comment.