poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1677562 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/ java/org/apache/poi/util/ ooxml/java/org/apache/poi/ss/usermodel/ ooxml/testcases/org/apache/poi/ss/
Date Mon, 04 May 2015 09:15:48 GMT
Author: nick
Date: Mon May  4 09:15:48 2015
New Revision: 1677562

URL: http://svn.apache.org/r1677562
Log:
If an empty stream or file is given to WorkbookFactory.create, give a more informative exception
- EmptyFileException

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/util/IOUtils.java
    poi/trunk/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java?rev=1677562&r1=1677561&r2=1677562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java Mon May  4 09:15:48
2015
@@ -36,6 +36,7 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.poi.EmptyFileException;
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.dev.POIFSViewable;
@@ -210,6 +211,9 @@ public class NPOIFSFileSystem extends Bl
        try {
           // Initialize the datasource
           if (srcFile != null) {
+              if (srcFile.length() == 0)
+                  throw new EmptyFileException();
+              
               FileBackedDataSource d = new FileBackedDataSource(srcFile, readOnly);
               channel = d.getChannel();
               _data = d;
@@ -236,7 +240,10 @@ public class NPOIFSFileSystem extends Bl
           // TODO Decide if we can handle these better whilst
           //  still sticking to the iterator contract
           if(closeChannelOnError) {
-             channel.close();
+              if (channel != null) {
+                  channel.close();
+                  channel = null;
+              }
           }
           throw e;
        }

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java?rev=1677562&r1=1677561&r2=1677562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java Mon May  4 09:15:48
2015
@@ -25,7 +25,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.PushbackInputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -37,7 +36,17 @@ import org.apache.poi.poifs.dev.POIFSVie
 import org.apache.poi.poifs.property.DirectoryProperty;
 import org.apache.poi.poifs.property.Property;
 import org.apache.poi.poifs.property.PropertyTable;
-import org.apache.poi.poifs.storage.*;
+import org.apache.poi.poifs.storage.BATBlock;
+import org.apache.poi.poifs.storage.BlockAllocationTableReader;
+import org.apache.poi.poifs.storage.BlockAllocationTableWriter;
+import org.apache.poi.poifs.storage.BlockList;
+import org.apache.poi.poifs.storage.BlockWritable;
+import org.apache.poi.poifs.storage.HeaderBlock;
+import org.apache.poi.poifs.storage.HeaderBlockConstants;
+import org.apache.poi.poifs.storage.HeaderBlockWriter;
+import org.apache.poi.poifs.storage.RawDataBlockList;
+import org.apache.poi.poifs.storage.SmallBlockTableReader;
+import org.apache.poi.poifs.storage.SmallBlockTableWriter;
 import org.apache.poi.util.CloseIgnoringInputStream;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LongField;
@@ -201,19 +210,15 @@ public class POIFSFileSystem
      */
     public static boolean hasPOIFSHeader(InputStream inp) throws IOException {
         // We want to peek at the first 8 bytes
-        inp.mark(8);
-
-        byte[] header = new byte[8];
-        IOUtils.readFully(inp, header);
-        LongField signature = new LongField(HeaderBlockConstants._signature_offset, header);
-
-        // Wind back those 8 bytes
-        if(inp instanceof PushbackInputStream) {
-            PushbackInputStream pin = (PushbackInputStream)inp;
-            pin.unread(header);
-        } else {
-            inp.reset();
-        }
+        byte[] header = IOUtils.peekFirst8Bytes(inp);
+        return hasPOIFSHeader(header);
+    }
+    /**
+     * Checks if the supplied first 8 bytes of a stream / file
+     *  has a POIFS (OLE2) header.
+     */
+    public static boolean hasPOIFSHeader(byte[] header8Bytes) {
+        LongField signature = new LongField(HeaderBlockConstants._signature_offset, header8Bytes);
 
         // Did it match the signature?
         return (signature.get() == HeaderBlockConstants._signature);

Modified: poi/trunk/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/IOUtils.java?rev=1677562&r1=1677561&r2=1677562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/IOUtils.java Mon May  4 09:15:48 2015
@@ -22,11 +22,14 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PushbackInputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.ReadableByteChannel;
 import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
+import org.apache.poi.EmptyFileException;
+
 public final class IOUtils {
 
     private static final POILogger logger = POILogFactory
@@ -35,6 +38,34 @@ public final class IOUtils {
 	private IOUtils() {
 		// no instances of this class
 	}
+	
+	/**
+	 * Peeks at the first 8 bytes of the stream. Returns those bytes, but
+	 *  with the stream unaffected. Requires a stream that supports mark/reset,
+	 *  or a PushbackInputStream. If the stream has >0 but <8 bytes, 
+	 *  remaining bytes will be zero.
+	 * @throws EmptyFileException if the stream is empty
+	 */
+	public static byte[] peekFirst8Bytes(InputStream stream) throws IOException, EmptyFileException
{
+        // We want to peek at the first 8 bytes
+	    stream.mark(8);
+
+        byte[] header = new byte[8];
+        int read = IOUtils.readFully(stream, header);
+        
+        if (read < 1)
+            throw new EmptyFileException();
+
+        // Wind back those 8 bytes
+        if(stream instanceof PushbackInputStream) {
+            PushbackInputStream pin = (PushbackInputStream)stream;
+            pin.unread(header);
+        } else {
+            stream.reset();
+        }
+        
+        return header;
+	}
 
 	/**
 	 * Reads all the data from the input stream, and returns the bytes read.

Modified: poi/trunk/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java?rev=1677562&r1=1677561&r2=1677562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java Mon May  4 09:15:48
2015
@@ -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.D
 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()) {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java?rev=1677562&r1=1677561&r2=1677562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java Mon May  4 09:15:48
2015
@@ -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 e
             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();
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message