Commit 9f7ba643 authored by Matija Obreza's avatar Matija Obreza

Fixed #1 - XLSX files are not closed

parent dbfbab40
......@@ -27,6 +27,7 @@ import org.apache.log4j.Logger;
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.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
......@@ -59,7 +60,7 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
OPCPackage pkg;
try {
pkg = OPCPackage.open(sourceFile);
pkg = OPCPackage.open(sourceFile, PackageAccess.READ);
} catch (InvalidFormatException e) {
throw new UnsupportedDataFormatException("Could not open " + sourceFile.getAbsolutePath(), e);
}
......@@ -89,6 +90,12 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
} finally {
_log.debug("Closing OPCPackage, no save");
pkg.revert();
try {
pkg.close();
} catch (IOException e) {
_log.error(e);
}
}
return sheets;
}
......@@ -107,7 +114,7 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
OPCPackage pkg;
try {
pkg = OPCPackage.open(sourceFile);
pkg = OPCPackage.open(sourceFile, PackageAccess.READ);
} catch (InvalidFormatException e) {
throw new UnsupportedDataFormatException("Could not open " + sourceFile.getAbsolutePath(), e);
}
......@@ -167,6 +174,12 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
e1.printStackTrace();
} finally {
_log.debug("Closing OPCPackage");
pkg.revert();
try {
pkg.close();
} catch (IOException e) {
_log.error(e.getMessage(), e);
}
}
throw new UnsupportedDataFormatException("Could not parse " + sourceFile.getAbsolutePath(), null);
......
......@@ -50,6 +50,7 @@ public class XlsxRowReader implements RowReader {
OPCPackage pkg;
try {
// pkg is passed to threadedReader where it is also closed
pkg = OPCPackage.open(sourceFile, PackageAccess.READ);
} catch (InvalidFormatException e) {
......
......@@ -42,6 +42,7 @@ import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
......@@ -129,7 +130,7 @@ public final class XSSFReadWrite {
}
public static void processAllSheets(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
try {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
......@@ -152,6 +153,7 @@ public final class XSSFReadWrite {
} finally {
_log.debug("Closing OPCPackage");
pkg.revert();
pkg.close();
}
}
......
......@@ -41,6 +41,7 @@ import java.util.List;
import org.apache.log4j.Logger;
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.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
......@@ -78,7 +79,7 @@ public final class XSSFRowReader {
}
public static void processAllSheets(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
try {
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
......@@ -103,6 +104,8 @@ public final class XSSFRowReader {
} finally {
_log.debug("Closing OPCPackage, no save");
pkg.revert();
pkg.close();
}
}
......@@ -181,7 +184,7 @@ public final class XSSFRowReader {
}
public static List<Object[]> getRows(String filename, int maxRows) throws IOException, SAXException, OpenXML4JException {
OPCPackage pkg = OPCPackage.open(filename);
OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
try {
XSSFReader r = new XSSFReader(pkg);
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
......@@ -217,6 +220,8 @@ public final class XSSFRowReader {
} finally {
_log.debug("Closing OPCPackage");
pkg.revert();
pkg.close();
}
return null;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment