pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1788970 - in /pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption: TestPublicKeyEncryption.java TestSymmetricKeyEncryption.java
Date Mon, 27 Mar 2017 16:14:46 GMT
Author: tilman
Date: Mon Mar 27 16:14:46 2017
New Revision: 1788970

URL: http://svn.apache.org/viewvc?rev=1788970&view=rev
Log:
PDFBOX-2852: simplify and optimize code

Modified:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java?rev=1788970&r1=1788969&r2=1788970&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
Mon Mar 27 16:14:46 2017
@@ -16,8 +16,8 @@
  */
 package org.apache.pdfbox.encryption;
 
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.NoSuchAlgorithmException;
@@ -103,16 +103,7 @@ public class TestPublicKeyEncryption ext
         keyStore1 = "test1.pfx";
         keyStore2 = "test2.pfx";
         
-        InputStream input =
-            TestPublicKeyEncryption.class.getResourceAsStream("test.pdf");
-        try 
-        {
-            document = PDDocument.load(input);
-        } 
-        finally 
-        {
-            input.close();
-        }
+        document = PDDocument.load(new File(this.getClass().getResource("test.pdf").toURI()));
     }
 
     /**
@@ -260,9 +251,9 @@ public class TestPublicKeyEncryption ext
     private PDDocument reload(PDDocument doc, String decryptionPassword, InputStream keyStore)
             throws IOException, NoSuchAlgorithmException
     {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        doc.save(buffer);
-        return PDDocument.load(new ByteArrayInputStream(buffer.toByteArray()), decryptionPassword,
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        doc.save(baos);
+        return PDDocument.load(baos.toByteArray(), decryptionPassword,
                 keyStore, null, MemoryUsageSetting.setupMainMemoryOnly());
     }
 

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java?rev=1788970&r1=1788969&r2=1788970&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestSymmetricKeyEncryption.java
Mon Mar 27 16:14:46 2017
@@ -18,7 +18,6 @@ package org.apache.pdfbox.encryption;
 
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -160,11 +159,9 @@ public class TestSymmetricKeyEncryption
     private void checkPerms(byte[] inputFileAsByteArray, String password,
             AccessPermission expectedPermissions) throws IOException
     {
-        PDDocument doc = PDDocument.load(
-                    new ByteArrayInputStream(inputFileAsByteArray),
-                    password);
+        PDDocument doc = PDDocument.load(inputFileAsByteArray, password);
 
-            AccessPermission currentAccessPermission = doc.getCurrentAccessPermission();
+        AccessPermission currentAccessPermission = doc.getCurrentAccessPermission();
 
         // check permissions
         assertEquals(expectedPermissions.isOwnerPermission(), currentAccessPermission.isOwnerPermission());
@@ -243,7 +240,7 @@ public class TestSymmetricKeyEncryption
             String userpassword, String ownerpassword,
             AccessPermission permission) throws IOException
     {
-        PDDocument document = PDDocument.load(new ByteArrayInputStream(inputFileAsByteArray));
+        PDDocument document = PDDocument.load(inputFileAsByteArray);
         String prefix = "Simple-";
         int numSrcPages = document.getNumberOfPages();
         PDFRenderer pdfRenderer = new PDFRenderer(document);
@@ -361,7 +358,7 @@ public class TestSymmetricKeyEncryption
             File embeddedFilePriorToEncryption,
             String userpassword, String ownerpassword) throws IOException
     {
-        PDDocument document = PDDocument.load(new ByteArrayInputStream(inputFileWithEmbeddedFileAsByteArray));
+        PDDocument document = PDDocument.load(inputFileWithEmbeddedFileAsByteArray);
         PDDocument encryptedDoc = encrypt(keyLength, preferAES, sizePriorToEncr, document,
"ContainsEmbedded-", permission, userpassword, ownerpassword);
 
         File decryptedFile = new File(testResultsDir, "DecryptedContainsEmbedded-" + keyLength
+ "-bit-" + (preferAES ? "AES" : "RC4") + ".pdf");
@@ -380,22 +377,13 @@ public class TestSymmetricKeyEncryption
         encryptedDoc.close();
     }
 
-    private byte[] getStreamAsByteArray(InputStream is) throws IOException
-    {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        IOUtils.copy(is, baos);
-        is.close();
-        return baos.toByteArray();
-    }
-
     private byte[] getFileResourceAsByteArray(String testFileName) throws IOException
     {
-        return getStreamAsByteArray(TestSymmetricKeyEncryption.class.getResourceAsStream(testFileName));
+        return IOUtils.toByteArray(TestSymmetricKeyEncryption.class.getResourceAsStream(testFileName));
     }
 
     private byte[] getFileAsByteArray(File f) throws IOException
     {
-        return getStreamAsByteArray(new FileInputStream(f));
+        return IOUtils.toByteArray(new FileInputStream(f));
     }
-
 }



Mime
View raw message