poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fannin...@apache.org
Subject svn commit: r1857461 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java
Date Sat, 13 Apr 2019 10:03:03 GMT
Author: fanningpj
Date: Sat Apr 13 10:03:02 2019
New Revision: 1857461

URL: http://svn.apache.org/viewvc?rev=1857461&view=rev
Log:
[bug-63344] add TempFilePOIFSFileSystem

Added:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java   (with
props)
Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java

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=1857461&r1=1857460&r2=1857461&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 Sat Apr 13 10:03:02
2019
@@ -87,7 +87,7 @@ public class POIFSFileSystem extends Blo
     private HeaderBlock _header;
     private DirectoryNode _root;
 
-    private DataSource _data;
+    protected DataSource _data;
 
     /**
      * What big block size the file uses. Most files
@@ -105,13 +105,17 @@ public class POIFSFileSystem extends Blo
         _root = null;
 
         if (newFS) {
-            // Data needs to initially hold just the header block,
-            //  a single bat block, and an empty properties section
-            _data = new ByteArrayBackedDataSource(IOUtils.safelyAllocate(
-                    bigBlockSize.getBigBlockSize() * 3, MAX_RECORD_LENGTH));
+            createNewDataSource();
         }
     }
 
+    protected void createNewDataSource() {
+        // Data needs to initially hold just the header block,
+        //  a single bat block, and an empty properties section
+        _data = new ByteArrayBackedDataSource(IOUtils.safelyAllocate(
+                bigBlockSize.getBigBlockSize() * 3, MAX_RECORD_LENGTH));
+    }
+
     /**
      * Constructor, intended for writing
      */

Added: poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java?rev=1857461&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java (added)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java Sat Apr
13 10:03:02 2019
@@ -0,0 +1,33 @@
+package org.apache.poi.poifs.filesystem;
+
+import org.apache.poi.poifs.nio.FileBackedDataSource;
+import org.apache.poi.util.Beta;
+import org.apache.poi.util.TempFile;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * An experimental POIFSFileSystem to support the encryption of large files
+ *
+ * @since 4.1.1
+ */
+@Beta
+public class TempFilePOIFSFileSystem extends POIFSFileSystem {
+    File tempFile;
+
+    protected void createNewDataSource() {
+        try {
+            tempFile = TempFile.createTempFile("poifs", ".tmp");
+            _data = new FileBackedDataSource(tempFile, false);
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to create data source", e);
+        }
+    }
+
+    public void close() throws IOException {
+        tempFile.delete();
+        super.close();
+    }
+
+}

Propchange: poi/trunk/src/java/org/apache/poi/poifs/filesystem/TempFilePOIFSFileSystem.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java?rev=1857461&r1=1857460&r2=1857461&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java Sat Apr 13
10:03:02 2019
@@ -33,11 +33,7 @@ import org.apache.poi.openxml4j.opc.OPCP
 import org.apache.poi.poifs.crypt.agile.AgileDecryptor;
 import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader;
 import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
-import org.apache.poi.poifs.filesystem.DirectoryNode;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.DocumentNode;
-import org.apache.poi.poifs.filesystem.Entry;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.poifs.filesystem.*;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
@@ -88,6 +84,43 @@ public class TestEncryptor {
         assertArrayEquals(payloadExpected, payloadActual);
     }
 
+    @Test
+    public void tempFileAgileEncryption() throws Exception {
+        String password = "pass";
+
+        final byte[] payloadExpected;
+        try (InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleMultiCell.xlsx"))
{
+            payloadExpected = IOUtils.toByteArray(is);
+        }
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (POIFSFileSystem fs = new TempFilePOIFSFileSystem()) {
+            EncryptionInfo ei = new EncryptionInfo(EncryptionMode.agile);
+            Encryptor enc = ei.getEncryptor();
+            enc.confirmPassword(password);
+
+            try (OutputStream os = enc.getDataStream(fs.getRoot())) {
+                os.write(payloadExpected);
+            }
+
+            fs.writeFilesystem(bos);
+        }
+
+        final byte[] payloadActual;
+        try (POIFSFileSystem fs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())))
{
+            EncryptionInfo ei = new EncryptionInfo(fs);
+            Decryptor dec = ei.getDecryptor();
+            boolean b = dec.verifyPassword(password);
+            assertTrue(b);
+
+            try (InputStream is = dec.getDataStream(fs.getRoot())) {
+                payloadActual = IOUtils.toByteArray(is);
+            }
+        }
+
+        assertArrayEquals(payloadExpected, payloadActual);
+    }
+
     @Test
     public void agileEncryption() throws Exception {
         int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");



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


Mime
View raw message