From 6fad9523b1ada32b55ce33e4a3fa996d28cd9543 Mon Sep 17 00:00:00 2001 From: kaloyane <kaloyane> Date: Fri, 5 Aug 2011 13:10:28 +0000 Subject: [PATCH] minor bugfix: close excel files after reading their contents SVN: 22353 --- .../generic/shared/utils/ExcelFileReader.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ExcelFileReader.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ExcelFileReader.java index 1350ef9b5d1..3334e7b4316 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ExcelFileReader.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ExcelFileReader.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.List; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -104,17 +105,24 @@ public class ExcelFileReader { final String extension = FilenameUtils.getExtension(file.getName()).toLowerCase(); final FileInputStream stream = new FileInputStream(file); - if ("xls".equals(extension)) + try { - POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); - return new HSSFWorkbook(poifsFileSystem); - } else if ("xlsx".equals(extension)) - { - return new XSSFWorkbook(stream); - } else + if ("xls".equals(extension)) + { + POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream); + return new HSSFWorkbook(poifsFileSystem); + } else if ("xlsx".equals(extension)) + { + return new XSSFWorkbook(stream); + } else + { + throw new IllegalArgumentException( + "Expected an Excel file with 'xls' or 'xlsx' extension, got " + + file.getName()); + } + } finally { - throw new IllegalArgumentException( - "Expected an Excel file with 'xls' or 'xlsx' extension, got " + file.getName()); + IOUtils.closeQuietly(stream); } } -- GitLab