diff options
author | Nick Burch <nick@apache.org> | 2015-05-04 09:15:48 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2015-05-04 09:15:48 +0000 |
commit | a874e223af546e5d32d2bc9aae4520a9a97c2def (patch) | |
tree | e139d5e5f9966976c4c29f1aa728c627afa7c778 /src/ooxml | |
parent | 7c44ed1f03a643bb60955f0707813f8107a377fd (diff) | |
download | poi-a874e223af546e5d32d2bc9aae4520a9a97c2def.tar.gz poi-a874e223af546e5d32d2bc9aae4520a9a97c2def.zip |
If an empty stream or file is given to WorkbookFactory.create, give a more informative exception - EmptyFileException
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1677562 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
-rw-r--r-- | src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java | 10 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java | 25 |
2 files changed, 34 insertions, 1 deletions
diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index bdf37eafca..80d9b76d4f 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.PushbackInputStream; import java.security.GeneralSecurityException; +import org.apache.poi.EmptyFileException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.POIXMLDocument; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; @@ -35,6 +36,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** @@ -153,14 +155,19 @@ public class WorkbookFactory { * from an InputStream requires more memory than loading * from a File, so prefer {@link #create(File)} where possible. * @throws EncryptedDocumentException If the wrong password is given for a protected file + * @throws EmptyFileException If an empty stream is given */ public static Workbook create(InputStream inp, String password) throws IOException, InvalidFormatException, EncryptedDocumentException { // If clearly doesn't do mark/reset, wrap up if (! inp.markSupported()) { inp = new PushbackInputStream(inp, 8); } + + // Ensure that there is at least some data there + byte[] header8 = IOUtils.peekFirst8Bytes(inp); - if (POIFSFileSystem.hasPOIFSHeader(inp)) { + // Try to create + if (POIFSFileSystem.hasPOIFSHeader(header8)) { NPOIFSFileSystem fs = new NPOIFSFileSystem(inp); return create(fs, password); } @@ -187,6 +194,7 @@ public class WorkbookFactory { * <p>Note that in order to properly release resources the * Workbook should be closed after use. * @throws EncryptedDocumentException If the wrong password is given for a protected file + * @throws EmptyFileException If an empty stream is given */ public static Workbook create(File file, String password) throws IOException, InvalidFormatException, EncryptedDocumentException { if (! file.exists()) { diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java index 582c8c3646..27d6d9ec1a 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java +++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java @@ -17,14 +17,18 @@ package org.apache.poi.ss; +import java.io.ByteArrayInputStream; +import java.io.File; import java.io.InputStream; +import org.apache.poi.EmptyFileException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.util.TempFile; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.openxml4j.opc.OPCPackage; @@ -254,4 +258,25 @@ public final class TestWorkbookFactory extends TestCase { fail("Shouldn't be able to open with the wrong password"); } catch (EncryptedDocumentException e) {} } + + /** + * Check that a helpful exception is given on an empty file / stream + */ + public void testEmptyFile() throws Exception { + InputStream emptyStream = new ByteArrayInputStream(new byte[0]); + File emptyFile = TempFile.createTempFile("empty", ".poi"); + + try { + WorkbookFactory.create(emptyStream); + fail("Shouldn't be able to create for an empty stream"); + } catch (EmptyFileException e) { + } + + try { + WorkbookFactory.create(emptyFile); + fail("Shouldn't be able to create for an empty file"); + } catch (EmptyFileException e) { + } + emptyFile.delete(); + } } |