aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-16 10:30:43 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-16 10:30:43 +0000
commit0042a4717239718390a3e23e9bb1e38592f07186 (patch)
tree5371073df7ba3a6f67bbb7e05ea7125577cebd1e /poi-ooxml
parentdfbab3ff6af87898de6101ea827db7e75cecca61 (diff)
downloadpoi-0042a4717239718390a3e23e9bb1e38592f07186.tar.gz
poi-0042a4717239718390a3e23e9bb1e38592f07186.zip
record size of encrypted temp data
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894303 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java5
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java14
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java5
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java2
4 files changed, 17 insertions, 9 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java
index a9119eaf42..a64326c32a 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java
@@ -98,12 +98,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart {
return tempFile.getOutputStream();
}
- /**
- * @return EncryptedTempData.getSize() always returns -1
- */
@Override
public long getSize() {
- return -1;
+ return tempFile.getByteCount();
}
@Override
diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java
index 9f219f1a80..d0fe43a9c8 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java
@@ -32,6 +32,7 @@ import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
+import org.apache.commons.io.output.CountingOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.poifs.crypt.ChainingMode;
@@ -52,7 +53,8 @@ public class EncryptedTempData {
private final SecretKeySpec skeySpec;
private final byte[] ivBytes;
private final File tempFile;
-
+ private CountingOutputStream outputStream;
+
public EncryptedTempData() throws IOException {
SecureRandom sr = new SecureRandom();
ivBytes = new byte[16];
@@ -72,7 +74,8 @@ public class EncryptedTempData {
*/
public OutputStream getOutputStream() throws IOException {
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
- return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc);
+ outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc));
+ return outputStream;
}
/**
@@ -87,6 +90,13 @@ public class EncryptedTempData {
}
/**
+ * @return number of bytes stored in the temp data file (the number you should expect after you decrypt the data)
+ */
+ public long getByteCount() {
+ return outputStream == null ? 0 : outputStream.getByteCount();
+ }
+
+ /**
* Removes the temporarily backing file
*/
public void dispose() {
diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java
index 4f0524b2ea..1d9f3d411e 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java
@@ -33,15 +33,16 @@ public class TestEncryptedTempFilePackagePart {
@Test
void testRoundTrip() throws Exception {
String text = UUID.randomUUID().toString();
+ byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) {
PackagePartName name = new PackagePartName("/test.txt", true);
EncryptedTempFilePackagePart part = new EncryptedTempFilePackagePart(p, name, "text/plain");
try (OutputStream os = part.getOutputStream()) {
- os.write(text.getBytes(StandardCharsets.UTF_8));
+ os.write(bytes);
}
- assertEquals(-1, part.getSize());
+ assertEquals(bytes.length, part.getSize());
try (InputStream is = part.getInputStream()) {
assertEquals(text, new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8));
}
diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java
index c30dbcf18b..768054acec 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java
@@ -40,7 +40,7 @@ public class TestTempFilePackagePart {
PackagePartName name = new PackagePartName("/test.txt", true);
TempFilePackagePart part = new TempFilePackagePart(p, name, "text/plain");
try (OutputStream os = part.getOutputStream()) {
- os.write(text.getBytes(StandardCharsets.UTF_8));
+ os.write(bytes);
}
assertEquals(bytes.length, part.getSize());
try (InputStream is = part.getInputStream()) {