Skip to content

Commit

Permalink
putting revert call in the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Apr 24, 2016
1 parent 582d525 commit b1cdfcd
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/main/java/com/monitorjbl/xlsx/StreamingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public class StreamingReader implements Iterable<Row>, AutoCloseable {
private StreamingCell currentCell;

private File tmp;
private OPCPackage pkg;

private StreamingReader(SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser, int rowCacheSize) {
private StreamingReader(OPCPackage pkg, SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser, int rowCacheSize) {
this.pkg = pkg;
this.sst = sst;
this.stylesTable = stylesTable;
this.parser = parser;
Expand Down Expand Up @@ -263,6 +265,7 @@ public Iterator<Row> iterator() {
public void close() {
try {
parser.close();
pkg.revert();
} catch(XMLStreamException e) {
throw new CloseException(e);
}
Expand Down Expand Up @@ -411,9 +414,8 @@ public StreamingReader read(InputStream is) {
* @throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the file
*/
public StreamingReader read(File f) {
OPCPackage pkg = null;

try {
OPCPackage pkg;
if(password != null) {
// Based on: https://poi.apache.org/encryption.html
POIFSFileSystem poifs = new POIFSFileSystem(f);
Expand All @@ -435,22 +437,13 @@ public StreamingReader read(File f) {
}

XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(sheet);
return new StreamingReader(sst, styles, parser, rowCacheSize);
return new StreamingReader(pkg, sst, styles, parser, rowCacheSize);
} catch(IOException e) {
throw new OpenException("Failed to open file", e);
} catch(OpenXML4JException | XMLStreamException e) {
throw new ReadException("Unable to read workbook", e);
} catch(GeneralSecurityException e) {
throw new ReadException("Unable to read workbook - Decryption failed", e);
} finally {
//close package to prevent locking issues
try {
if(pkg != null) {
pkg.revert();
}
} catch(Exception e) {
log.debug("Could not close file", e);
}
}
}

Expand Down

0 comments on commit b1cdfcd

Please sign in to comment.