< prev index next >

src/java.base/share/classes/javax/crypto/Cipher.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


 514      * See the Cipher section in the <a href=
 515      *   "{@docRoot}/../specs/security/standard-names.html#cipher-algorithm-names">
 516      * Java Security Standard Algorithm Names Specification</a>
 517      * for information about standard transformation names.
 518      *
 519      * @return a cipher that implements the requested transformation
 520      *
 521      * @throws NoSuchAlgorithmException if {@code transformation}
 522      *         is {@code null}, empty, in an invalid format,
 523      *         or if no {@code Provider} supports a {@code CipherSpi}
 524      *         implementation for the specified algorithm
 525      *
 526      * @throws NoSuchPaddingException if {@code transformation}
 527      *         contains a padding scheme that is not available
 528      *
 529      * @see java.security.Provider
 530      */
 531     public static final Cipher getInstance(String transformation)
 532             throws NoSuchAlgorithmException, NoSuchPaddingException
 533     {
 534         if ((transformation == null) || transformation.equals("")) {
 535             throw new NoSuchAlgorithmException("Null or empty transformation");
 536         }
 537         List<Transform> transforms = getTransforms(transformation);
 538         List<ServiceId> cipherServices = new ArrayList<>(transforms.size());
 539         for (Transform transform : transforms) {
 540             cipherServices.add(new ServiceId("Cipher", transform.transform));
 541         }
 542         List<Service> services = GetInstance.getServices(cipherServices);
 543         // make sure there is at least one service from a signed provider
 544         // and that it can use the specified mode and padding
 545         Iterator<Service> t = services.iterator();
 546         Exception failure = null;
 547         while (t.hasNext()) {
 548             Service s = t.next();
 549             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 550                 continue;
 551             }
 552             Transform tr = getTransform(s, transforms);
 553             if (tr == null) {
 554                 // should never happen


 614      *
 615      * @throws NoSuchAlgorithmException if {@code transformation}
 616      *         is {@code null}, empty, in an invalid format,
 617      *         or if a {@code CipherSpi} implementation for the
 618      *         specified algorithm is not available from the specified
 619      *         provider
 620      *
 621      * @throws NoSuchPaddingException if {@code transformation}
 622      *         contains a padding scheme that is not available
 623      *
 624      * @throws NoSuchProviderException if the specified provider is not
 625      *         registered in the security provider list
 626      *
 627      * @see java.security.Provider
 628      */
 629     public static final Cipher getInstance(String transformation,
 630                                            String provider)
 631             throws NoSuchAlgorithmException, NoSuchProviderException,
 632             NoSuchPaddingException
 633     {
 634         if ((transformation == null) || transformation.equals("")) {
 635             throw new NoSuchAlgorithmException("Null or empty transformation");
 636         }
 637         if ((provider == null) || (provider.length() == 0)) {
 638             throw new IllegalArgumentException("Missing provider");
 639         }
 640         Provider p = Security.getProvider(provider);
 641         if (p == null) {
 642             throw new NoSuchProviderException("No such provider: " +
 643                                               provider);
 644         }
 645         return getInstance(transformation, p);
 646     }
 647 
 648     private String getProviderName() {
 649         return (provider == null)  ? "(no provider)" : provider.getName();
 650     }
 651 
 652     /**
 653      * Returns a {@code Cipher} object that implements the specified
 654      * transformation.


 681      * @return a cipher that implements the requested transformation
 682      *
 683      * @throws IllegalArgumentException if the {@code provider}
 684      *         is {@code null}
 685      *
 686      * @throws NoSuchAlgorithmException if {@code transformation}
 687      *         is {@code null}, empty, in an invalid format,
 688      *         or if a {@code CipherSpi} implementation for the
 689      *         specified algorithm is not available from the specified
 690      *         {@code Provider} object
 691      *
 692      * @throws NoSuchPaddingException if {@code transformation}
 693      *         contains a padding scheme that is not available
 694      *
 695      * @see java.security.Provider
 696      */
 697     public static final Cipher getInstance(String transformation,
 698                                            Provider provider)
 699             throws NoSuchAlgorithmException, NoSuchPaddingException
 700     {
 701         if ((transformation == null) || transformation.equals("")) {
 702             throw new NoSuchAlgorithmException("Null or empty transformation");
 703         }
 704         if (provider == null) {
 705             throw new IllegalArgumentException("Missing provider");
 706         }
 707         Exception failure = null;
 708         List<Transform> transforms = getTransforms(transformation);
 709         boolean providerChecked = false;
 710         String paddingError = null;
 711         for (Transform tr : transforms) {
 712             Service s = provider.getService("Cipher", tr.transform);
 713             if (s == null) {
 714                 continue;
 715             }
 716             if (providerChecked == false) {
 717                 // for compatibility, first do the lookup and then verify
 718                 // the provider. this makes the difference between a NSAE
 719                 // and a SecurityException if the
 720                 // provider does not support the algorithm.
 721                 Exception ve = JceSecurity.getVerificationResult(provider);




 514      * See the Cipher section in the <a href=
 515      *   "{@docRoot}/../specs/security/standard-names.html#cipher-algorithm-names">
 516      * Java Security Standard Algorithm Names Specification</a>
 517      * for information about standard transformation names.
 518      *
 519      * @return a cipher that implements the requested transformation
 520      *
 521      * @throws NoSuchAlgorithmException if {@code transformation}
 522      *         is {@code null}, empty, in an invalid format,
 523      *         or if no {@code Provider} supports a {@code CipherSpi}
 524      *         implementation for the specified algorithm
 525      *
 526      * @throws NoSuchPaddingException if {@code transformation}
 527      *         contains a padding scheme that is not available
 528      *
 529      * @see java.security.Provider
 530      */
 531     public static final Cipher getInstance(String transformation)
 532             throws NoSuchAlgorithmException, NoSuchPaddingException
 533     {
 534         if ((transformation == null) || transformation.isEmpty()) {
 535             throw new NoSuchAlgorithmException("Null or empty transformation");
 536         }
 537         List<Transform> transforms = getTransforms(transformation);
 538         List<ServiceId> cipherServices = new ArrayList<>(transforms.size());
 539         for (Transform transform : transforms) {
 540             cipherServices.add(new ServiceId("Cipher", transform.transform));
 541         }
 542         List<Service> services = GetInstance.getServices(cipherServices);
 543         // make sure there is at least one service from a signed provider
 544         // and that it can use the specified mode and padding
 545         Iterator<Service> t = services.iterator();
 546         Exception failure = null;
 547         while (t.hasNext()) {
 548             Service s = t.next();
 549             if (JceSecurity.canUseProvider(s.getProvider()) == false) {
 550                 continue;
 551             }
 552             Transform tr = getTransform(s, transforms);
 553             if (tr == null) {
 554                 // should never happen


 614      *
 615      * @throws NoSuchAlgorithmException if {@code transformation}
 616      *         is {@code null}, empty, in an invalid format,
 617      *         or if a {@code CipherSpi} implementation for the
 618      *         specified algorithm is not available from the specified
 619      *         provider
 620      *
 621      * @throws NoSuchPaddingException if {@code transformation}
 622      *         contains a padding scheme that is not available
 623      *
 624      * @throws NoSuchProviderException if the specified provider is not
 625      *         registered in the security provider list
 626      *
 627      * @see java.security.Provider
 628      */
 629     public static final Cipher getInstance(String transformation,
 630                                            String provider)
 631             throws NoSuchAlgorithmException, NoSuchProviderException,
 632             NoSuchPaddingException
 633     {
 634         if ((transformation == null) || transformation.isEmpty()) {
 635             throw new NoSuchAlgorithmException("Null or empty transformation");
 636         }
 637         if ((provider == null) || (provider.length() == 0)) {
 638             throw new IllegalArgumentException("Missing provider");
 639         }
 640         Provider p = Security.getProvider(provider);
 641         if (p == null) {
 642             throw new NoSuchProviderException("No such provider: " +
 643                                               provider);
 644         }
 645         return getInstance(transformation, p);
 646     }
 647 
 648     private String getProviderName() {
 649         return (provider == null)  ? "(no provider)" : provider.getName();
 650     }
 651 
 652     /**
 653      * Returns a {@code Cipher} object that implements the specified
 654      * transformation.


 681      * @return a cipher that implements the requested transformation
 682      *
 683      * @throws IllegalArgumentException if the {@code provider}
 684      *         is {@code null}
 685      *
 686      * @throws NoSuchAlgorithmException if {@code transformation}
 687      *         is {@code null}, empty, in an invalid format,
 688      *         or if a {@code CipherSpi} implementation for the
 689      *         specified algorithm is not available from the specified
 690      *         {@code Provider} object
 691      *
 692      * @throws NoSuchPaddingException if {@code transformation}
 693      *         contains a padding scheme that is not available
 694      *
 695      * @see java.security.Provider
 696      */
 697     public static final Cipher getInstance(String transformation,
 698                                            Provider provider)
 699             throws NoSuchAlgorithmException, NoSuchPaddingException
 700     {
 701         if ((transformation == null) || transformation.isEmpty()) {
 702             throw new NoSuchAlgorithmException("Null or empty transformation");
 703         }
 704         if (provider == null) {
 705             throw new IllegalArgumentException("Missing provider");
 706         }
 707         Exception failure = null;
 708         List<Transform> transforms = getTransforms(transformation);
 709         boolean providerChecked = false;
 710         String paddingError = null;
 711         for (Transform tr : transforms) {
 712             Service s = provider.getService("Cipher", tr.transform);
 713             if (s == null) {
 714                 continue;
 715             }
 716             if (providerChecked == false) {
 717                 // for compatibility, first do the lookup and then verify
 718                 // the provider. this makes the difference between a NSAE
 719                 // and a SecurityException if the
 720                 // provider does not support the algorithm.
 721                 Exception ve = JceSecurity.getVerificationResult(provider);


< prev index next >