< prev index next >

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

Print this page
rev 51919 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: dfuchs, alanb


 324         String[] parts = new String[3];
 325         int count = 0;
 326         StringTokenizer parser = new StringTokenizer(transformation, "/");
 327         try {
 328             while (parser.hasMoreTokens() && count < 3) {
 329                 parts[count++] = parser.nextToken().trim();
 330             }
 331             if (count == 0 || count == 2) {
 332                 throw new NoSuchAlgorithmException("Invalid transformation"
 333                                                + " format:" +
 334                                                transformation);
 335             }
 336             // treats all subsequent tokens as part of padding
 337             if (count == 3 && parser.hasMoreTokens()) {
 338                 parts[2] = parts[2] + parser.nextToken("\r\n");
 339             }
 340         } catch (NoSuchElementException e) {
 341             throw new NoSuchAlgorithmException("Invalid transformation " +
 342                                            "format:" + transformation);
 343         }
 344         if ((parts[0] == null) || (parts[0].length() == 0)) {
 345             throw new NoSuchAlgorithmException("Invalid transformation:" +
 346                                    "algorithm not specified-"
 347                                    + transformation);
 348         }
 349         return parts;
 350     }
 351 
 352     // Provider attribute name for supported chaining mode
 353     private static final String ATTR_MODE = "SupportedModes";
 354     // Provider attribute name for supported padding names
 355     private static final String ATTR_PAD  = "SupportedPaddings";
 356 
 357     // constants indicating whether the provider supports
 358     // a given mode or padding
 359     private static final int S_NO    = 0;       // does not support
 360     private static final int S_MAYBE = 1;       // unable to determine
 361     private static final int S_YES   = 2;       // does support
 362 
 363     /**
 364      * Nested class to deal with modes and paddings.


 428             new ConcurrentHashMap<String, Pattern>();
 429 
 430         private static boolean matches(String regexp, String str) {
 431             Pattern pattern = patternCache.get(regexp);
 432             if (pattern == null) {
 433                 pattern = Pattern.compile(regexp);
 434                 patternCache.putIfAbsent(regexp, pattern);
 435             }
 436             return pattern.matcher(str.toUpperCase(Locale.ENGLISH)).matches();
 437         }
 438 
 439     }
 440 
 441     private static List<Transform> getTransforms(String transformation)
 442             throws NoSuchAlgorithmException {
 443         String[] parts = tokenizeTransformation(transformation);
 444 
 445         String alg = parts[0];
 446         String mode = parts[1];
 447         String pad = parts[2];
 448         if ((mode != null) && (mode.length() == 0)) {
 449             mode = null;
 450         }
 451         if ((pad != null) && (pad.length() == 0)) {
 452             pad = null;
 453         }
 454 
 455         if ((mode == null) && (pad == null)) {
 456             // AES
 457             Transform tr = new Transform(alg, "", null, null);
 458             return Collections.singletonList(tr);
 459         } else { // if ((mode != null) && (pad != null)) {
 460             // AES/CBC/PKCS5Padding
 461             List<Transform> list = new ArrayList<>(4);
 462             list.add(new Transform(alg, "/" + mode + "/" + pad, null, null));
 463             list.add(new Transform(alg, "/" + mode, null, pad));
 464             list.add(new Transform(alg, "//" + pad, mode, null));
 465             list.add(new Transform(alg, "", mode, pad));
 466             return list;
 467         }
 468     }
 469 
 470     // get the transform matching the specified service
 471     private static Transform getTransform(Service s,


 597      *         or if a {@code CipherSpi} implementation for the
 598      *         specified algorithm is not available from the specified
 599      *         provider
 600      *
 601      * @throws NoSuchPaddingException if {@code transformation}
 602      *         contains a padding scheme that is not available
 603      *
 604      * @throws NoSuchProviderException if the specified provider is not
 605      *         registered in the security provider list
 606      *
 607      * @see java.security.Provider
 608      */
 609     public static final Cipher getInstance(String transformation,
 610                                            String provider)
 611             throws NoSuchAlgorithmException, NoSuchProviderException,
 612             NoSuchPaddingException
 613     {
 614         if ((transformation == null) || transformation.equals("")) {
 615             throw new NoSuchAlgorithmException("Null or empty transformation");
 616         }
 617         if ((provider == null) || (provider.length() == 0)) {
 618             throw new IllegalArgumentException("Missing provider");
 619         }
 620         Provider p = Security.getProvider(provider);
 621         if (p == null) {
 622             throw new NoSuchProviderException("No such provider: " +
 623                                               provider);
 624         }
 625         return getInstance(transformation, p);
 626     }
 627 
 628     private String getProviderName() {
 629         return (provider == null)  ? "(no provider)" : provider.getName();
 630     }
 631 
 632     /**
 633      * Returns a {@code Cipher} object that implements the specified
 634      * transformation.
 635      *
 636      * <p> A new Cipher object encapsulating the
 637      * CipherSpi implementation from the specified Provider




 324         String[] parts = new String[3];
 325         int count = 0;
 326         StringTokenizer parser = new StringTokenizer(transformation, "/");
 327         try {
 328             while (parser.hasMoreTokens() && count < 3) {
 329                 parts[count++] = parser.nextToken().trim();
 330             }
 331             if (count == 0 || count == 2) {
 332                 throw new NoSuchAlgorithmException("Invalid transformation"
 333                                                + " format:" +
 334                                                transformation);
 335             }
 336             // treats all subsequent tokens as part of padding
 337             if (count == 3 && parser.hasMoreTokens()) {
 338                 parts[2] = parts[2] + parser.nextToken("\r\n");
 339             }
 340         } catch (NoSuchElementException e) {
 341             throw new NoSuchAlgorithmException("Invalid transformation " +
 342                                            "format:" + transformation);
 343         }
 344         if ((parts[0] == null) || (parts[0].isEmpty())) {
 345             throw new NoSuchAlgorithmException("Invalid transformation:" +
 346                                    "algorithm not specified-"
 347                                    + transformation);
 348         }
 349         return parts;
 350     }
 351 
 352     // Provider attribute name for supported chaining mode
 353     private static final String ATTR_MODE = "SupportedModes";
 354     // Provider attribute name for supported padding names
 355     private static final String ATTR_PAD  = "SupportedPaddings";
 356 
 357     // constants indicating whether the provider supports
 358     // a given mode or padding
 359     private static final int S_NO    = 0;       // does not support
 360     private static final int S_MAYBE = 1;       // unable to determine
 361     private static final int S_YES   = 2;       // does support
 362 
 363     /**
 364      * Nested class to deal with modes and paddings.


 428             new ConcurrentHashMap<String, Pattern>();
 429 
 430         private static boolean matches(String regexp, String str) {
 431             Pattern pattern = patternCache.get(regexp);
 432             if (pattern == null) {
 433                 pattern = Pattern.compile(regexp);
 434                 patternCache.putIfAbsent(regexp, pattern);
 435             }
 436             return pattern.matcher(str.toUpperCase(Locale.ENGLISH)).matches();
 437         }
 438 
 439     }
 440 
 441     private static List<Transform> getTransforms(String transformation)
 442             throws NoSuchAlgorithmException {
 443         String[] parts = tokenizeTransformation(transformation);
 444 
 445         String alg = parts[0];
 446         String mode = parts[1];
 447         String pad = parts[2];
 448         if ((mode != null) && (mode.isEmpty())) {
 449             mode = null;
 450         }
 451         if ((pad != null) && (pad.isEmpty())) {
 452             pad = null;
 453         }
 454 
 455         if ((mode == null) && (pad == null)) {
 456             // AES
 457             Transform tr = new Transform(alg, "", null, null);
 458             return Collections.singletonList(tr);
 459         } else { // if ((mode != null) && (pad != null)) {
 460             // AES/CBC/PKCS5Padding
 461             List<Transform> list = new ArrayList<>(4);
 462             list.add(new Transform(alg, "/" + mode + "/" + pad, null, null));
 463             list.add(new Transform(alg, "/" + mode, null, pad));
 464             list.add(new Transform(alg, "//" + pad, mode, null));
 465             list.add(new Transform(alg, "", mode, pad));
 466             return list;
 467         }
 468     }
 469 
 470     // get the transform matching the specified service
 471     private static Transform getTransform(Service s,


 597      *         or if a {@code CipherSpi} implementation for the
 598      *         specified algorithm is not available from the specified
 599      *         provider
 600      *
 601      * @throws NoSuchPaddingException if {@code transformation}
 602      *         contains a padding scheme that is not available
 603      *
 604      * @throws NoSuchProviderException if the specified provider is not
 605      *         registered in the security provider list
 606      *
 607      * @see java.security.Provider
 608      */
 609     public static final Cipher getInstance(String transformation,
 610                                            String provider)
 611             throws NoSuchAlgorithmException, NoSuchProviderException,
 612             NoSuchPaddingException
 613     {
 614         if ((transformation == null) || transformation.equals("")) {
 615             throw new NoSuchAlgorithmException("Null or empty transformation");
 616         }
 617         if ((provider == null) || (provider.isEmpty())) {
 618             throw new IllegalArgumentException("Missing provider");
 619         }
 620         Provider p = Security.getProvider(provider);
 621         if (p == null) {
 622             throw new NoSuchProviderException("No such provider: " +
 623                                               provider);
 624         }
 625         return getInstance(transformation, p);
 626     }
 627 
 628     private String getProviderName() {
 629         return (provider == null)  ? "(no provider)" : provider.getName();
 630     }
 631 
 632     /**
 633      * Returns a {@code Cipher} object that implements the specified
 634      * transformation.
 635      *
 636      * <p> A new Cipher object encapsulating the
 637      * CipherSpi implementation from the specified Provider


< prev index next >