diff --git a/spring-batch-excel/pom.xml b/spring-batch-excel/pom.xml
index bbf8d65..4840fd6 100644
--- a/spring-batch-excel/pom.xml
+++ b/spring-batch-excel/pom.xml
@@ -28,7 +28,7 @@
UTF-8
3.0.3.RELEASE
2.6.12
- 3.10.1
+ 3.11
diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java
index b666215..651ea55 100644
--- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java
+++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/AbstractExcelItemReader.java
@@ -136,27 +136,6 @@ public abstract class AbstractExcelItemReader extends AbstractItemCountingIte
}
- @Override
- protected final void doClose() throws Exception {
- doCloseWorkbook();
- if (this.resource != null) {
- try {
- InputStream is = this.resource.getInputStream();
- is.close();
- } catch (IOException ioe) {
- logger.warn("Exception whilst obtaining or closing the inputstream.", ioe);
- }
- }
- }
-
- /**
- * Method which can be overriden by subclasses to do cleanup additional resources.
- *
- * @throws Exception
- */
- protected void doCloseWorkbook() throws Exception {
- }
-
public void setResource(final Resource resource) {
this.resource = resource;
}
diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java
index add08f0..24dfc1a 100644
--- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java
+++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/jxl/JxlItemReader.java
@@ -51,7 +51,7 @@ public class JxlItemReader extends AbstractExcelItemReader {
}
@Override
- protected void doCloseWorkbook() throws Exception {
+ protected void doClose() throws Exception {
if (this.workbook != null) {
this.workbook.close();
}
diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java
index f94f749..718c705 100644
--- a/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java
+++ b/spring-batch-excel/src/main/java/org/springframework/batch/item/excel/poi/PoiItemReader.java
@@ -23,6 +23,10 @@ import org.springframework.batch.item.excel.AbstractExcelItemReader;
import org.springframework.batch.item.excel.Sheet;
import org.springframework.core.io.Resource;
+import java.io.Closeable;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+
/**
* {@link org.springframework.batch.item.ItemReader} implementation which uses apache POI to read an Excel
* file. It will read the file sheet for sheet and row for row. It is based on
@@ -36,6 +40,8 @@ public class PoiItemReader extends AbstractExcelItemReader {
private Workbook workbook;
+ private InputStream workbookStream;
+
@Override
protected Sheet getSheet(final int sheet) {
return new PoiSheet(this.workbook.getSheetAt(sheet));
@@ -46,9 +52,36 @@ public class PoiItemReader extends AbstractExcelItemReader {
return this.workbook.getNumberOfSheets();
}
+ @Override
+ protected void doClose() throws Exception {
+ // As of Apache POI 3.11 there is a close method on the Workbook, prior version
+ // lack this method.
+ if (workbook instanceof Closeable) {
+ this.workbook.close();
+ }
+
+ if (workbookStream != null) {
+ workbookStream.close();
+ }
+ this.workbook=null;
+ this.workbookStream=null;
+ }
+
+ /**
+ * Open the underlying file using the {@code WorkbookFactory}. We keep track of the used {@code InputStream} so that
+ * it can be closed cleanly on the end of reading the file. This to be able to release the resources used by
+ * Apache POI.
+ *
+ * @param resource
+ * @throws Exception is thrown for any errors.
+ */
@Override
protected void openExcelFile(final Resource resource) throws Exception {
- this.workbook = WorkbookFactory.create(resource.getInputStream());
+ workbookStream = resource.getInputStream();
+ if (!workbookStream.markSupported() && !(workbookStream instanceof PushbackInputStream)) {
+ throw new IllegalStateException("InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream");
+ }
+ this.workbook = WorkbookFactory.create(workbookStream);
this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
}