pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1789716 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption: PublicKeyProtectionPolicy.java PublicKeySecurityHandler.java SecurityHandler.java SecurityHandlerFactory.java SecurityProvider.java
Date Fri, 31 Mar 2017 17:22:38 GMT
Author: tilman
Date: Fri Mar 31 17:22:38 2017
New Revision: 1789716

URL: http://svn.apache.org/viewvc?rev=1789716&view=rev
Log:
PDFBOX-2852: use JDK7 syntax

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeyProtectionPolicy.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeyProtectionPolicy.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeyProtectionPolicy.java?rev=1789716&r1=1789715&r2=1789716&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeyProtectionPolicy.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeyProtectionPolicy.java
Fri Mar 31 17:22:38 2017
@@ -59,7 +59,7 @@ import java.util.List;
  */
 public final class PublicKeyProtectionPolicy extends ProtectionPolicy
 {
-    private final List<PublicKeyRecipient> recipients = new ArrayList<PublicKeyRecipient>();
+    private final List<PublicKeyRecipient> recipients = new ArrayList<>();
     private X509Certificate decryptionCertificate;
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java?rev=1789716&r1=1789715&r2=1789716&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
Fri Mar 31 17:22:38 2017
@@ -231,15 +231,7 @@ public final class PublicKeySecurityHand
             encryptionKey = new byte[this.keyLength / 8];
             System.arraycopy(mdResult, 0, encryptionKey, 0, this.keyLength / 8);
         }
-        catch (CMSException e)
-        {
-            throw new IOException(e);
-        }
-        catch (KeyStoreException e)
-        {
-            throw new IOException(e);
-        }
-        catch (CertificateEncodingException e)
+        catch (CMSException | KeyStoreException | CertificateEncodingException e)
         {
             throw new IOException(e);
         }
@@ -431,9 +423,11 @@ public final class PublicKeySecurityHand
 
         AlgorithmParameters parameters = apg.generateParameters();
 
-        ASN1InputStream input = new ASN1InputStream(parameters.getEncoded("ASN.1"));
-        ASN1Primitive object = input.readObject();
-        input.close();
+        ASN1Primitive object;
+        try (ASN1InputStream input = new ASN1InputStream(parameters.getEncoded("ASN.1")))
+        {
+            object = input.readObject();
+        }
 
         keygen.init(128);
         SecretKey secretkey = keygen.generateKey();
@@ -457,9 +451,11 @@ public final class PublicKeySecurityHand
         throws IOException, CertificateEncodingException, InvalidKeyException,
             BadPaddingException, IllegalBlockSizeException
     {
-        ASN1InputStream input = new ASN1InputStream(x509certificate.getTBSCertificate());
-        TBSCertificateStructure certificate = TBSCertificateStructure.getInstance(input.readObject());
-        input.close();
+        TBSCertificateStructure certificate;
+        try (ASN1InputStream input = new ASN1InputStream(x509certificate.getTBSCertificate()))
+        {
+            certificate = TBSCertificateStructure.getInstance(input.readObject());
+        }
 
         AlgorithmIdentifier algorithmId = certificate.getSubjectPublicKeyInfo().getAlgorithm();
 
@@ -473,12 +469,7 @@ public final class PublicKeySecurityHand
             cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId(),
                     SecurityProvider.getProvider());
         }
-        catch (NoSuchAlgorithmException e)
-        {
-            // should never happen, if this happens throw IOException instead
-            throw new RuntimeException("Could not find a suitable javax.crypto provider",
e);
-        }
-        catch (NoSuchPaddingException e)
+        catch (NoSuchAlgorithmException | NoSuchPaddingException e)
         {
             // should never happen, if this happens throw IOException instead
             throw new RuntimeException("Could not find a suitable javax.crypto provider",
e);

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java?rev=1789716&r1=1789715&r2=1789716&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
Fri Mar 31 17:22:38 2017
@@ -275,23 +275,8 @@ public abstract class SecurityHandler
             }
             output.write(decryptCipher.doFinal());
         }
-        catch (InvalidKeyException e)
-        {
-            throw new IOException(e);
-        }
-        catch (InvalidAlgorithmParameterException e)
-        {
-            throw new IOException(e);
-        }
-        catch (NoSuchPaddingException e)
-        {
-            throw new IOException(e);
-        }
-        catch (IllegalBlockSizeException e)
-        {
-            throw new IOException(e);
-        }
-        catch (BadPaddingException e)
+        catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException
| 
+                IllegalBlockSizeException | BadPaddingException e)
         {
             throw new IOException(e);
         }
@@ -328,8 +313,7 @@ public abstract class SecurityHandler
             throw new IOException(e);
         }
 
-        CipherInputStream cis = new CipherInputStream(data, cipher);
-        try
+        try (CipherInputStream cis = new CipherInputStream(data, cipher))
         {
             IOUtils.copy(cis, output);
         }
@@ -343,10 +327,6 @@ public abstract class SecurityHandler
             }
             LOG.debug("A GeneralSecurityException occured when decrypting some stream data",
exception);
         }
-        finally
-        {
-            cis.close();
-        }
     }
 
     private boolean prepareAESInitializationVector(boolean decrypt, byte[] iv, InputStream
data, OutputStream output) throws IOException
@@ -433,11 +413,13 @@ public abstract class SecurityHandler
         }
         if (COSName.METADATA.equals(type))
         {
+            byte[] buf;
             // PDFBOX-3229 check case where metadata is not encrypted despite /EncryptMetadata
missing
-            InputStream is = stream.createRawInputStream();
-            byte buf[] = new byte[10];
-            is.read(buf);
-            is.close();
+            try (InputStream is = stream.createRawInputStream())
+            {
+                buf = new byte[10];
+                is.read(buf);
+            }
             if (Arrays.equals(buf, "<?xpacket ".getBytes(Charsets.ISO_8859_1)))
             {
                 LOG.warn("Metadata is not encrypted, but was expected to be");
@@ -448,14 +430,9 @@ public abstract class SecurityHandler
         decryptDictionary(stream, objNum, genNum);
         byte[] encrypted = IOUtils.toByteArray(stream.createRawInputStream());
         ByteArrayInputStream encryptedStream = new ByteArrayInputStream(encrypted);
-        OutputStream output = stream.createRawOutputStream();
-        try
+        try (OutputStream output = stream.createRawOutputStream())
         {
-           encryptData(objNum, genNum, encryptedStream, output, true /* decrypt */);
-        }
-        finally
-        {
-            output.close();
+            encryptData(objNum, genNum, encryptedStream, output, true /* decrypt */);
         }
     }
 
@@ -474,15 +451,10 @@ public abstract class SecurityHandler
     {
         byte[] rawData = IOUtils.toByteArray(stream.createRawInputStream());
         ByteArrayInputStream encryptedStream = new ByteArrayInputStream(rawData);
-        OutputStream output = stream.createRawOutputStream();
-        try
+        try (OutputStream output = stream.createRawOutputStream())
         {
             encryptData(objNum, genNum, encryptedStream, output, false /* encrypt */);
         }
-        finally
-        {
-            output.close();
-        }
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java?rev=1789716&r1=1789715&r2=1789716&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java
Fri Mar 31 17:22:38 2017
@@ -19,7 +19,6 @@ package org.apache.pdfbox.pdmodel.encryp
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
-import java.security.Security;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -37,13 +36,10 @@ public final class SecurityHandlerFactor
     /** Singleton instance */
     public static final SecurityHandlerFactory INSTANCE = new SecurityHandlerFactory();
 
-    private final Map<String, Class<? extends SecurityHandler>> nameToHandler
=
-            new HashMap<String, Class<? extends SecurityHandler>>();
+    private final Map<String, Class<? extends SecurityHandler>> nameToHandler
= new HashMap<>();
 
     private final Map<Class<? extends ProtectionPolicy>,
-                      Class<? extends SecurityHandler>> policyToHandler =
-            new HashMap<Class<? extends ProtectionPolicy>,
-                        Class<? extends SecurityHandler>>();
+                      Class<? extends SecurityHandler>> policyToHandler = new HashMap<>();
 
     private SecurityHandlerFactory()
     {
@@ -132,22 +128,8 @@ public final class SecurityHandlerFactor
                     handlerClass.getDeclaredConstructor(argsClasses);
             return ctor.newInstance(args);
         }
-        catch(NoSuchMethodException e)
-        {
-            // should not happen in normal operation
-            throw new RuntimeException(e);
-        }
-        catch(IllegalAccessException e)
-        {
-            // should not happen in normal operation
-            throw new RuntimeException(e);
-        }
-        catch(InstantiationException e)
-        {
-            // should not happen in normal operation
-            throw new RuntimeException(e);
-        }
-        catch(InvocationTargetException e)
+        catch(NoSuchMethodException | IllegalAccessException | InstantiationException |
+                InvocationTargetException e)
         {
             // should not happen in normal operation
             throw new RuntimeException(e);

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java?rev=1789716&r1=1789715&r2=1789716&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java
Fri Mar 31 17:22:38 2017
@@ -49,15 +49,7 @@ public class SecurityProvider
                         .forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
                 provider = providerClass.newInstance();
             }
-            catch (ClassNotFoundException ex)
-            {
-                throw new IOException(ex);
-            }
-            catch (InstantiationException ex)
-            {
-                throw new IOException(ex);
-            }
-            catch (IllegalAccessException ex)
+            catch (ClassNotFoundException | InstantiationException | IllegalAccessException
ex)
             {
                 throw new IOException(ex);
             }



Mime
View raw message