Skip to content

Commit

Permalink
8274050: Unnecessary Vector usage in javax.crypto
Browse files Browse the repository at this point in the history
Reviewed-by: valeriep
  • Loading branch information
turbanoff authored and Valerie Peng committed Sep 29, 2021
1 parent 97b2874 commit 79cebe2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
26 changes: 11 additions & 15 deletions src/java.base/share/classes/javax/crypto/CryptoPermissions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,9 +26,9 @@
package javax.crypto;

import java.security.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.io.Serializable;
Expand Down Expand Up @@ -307,7 +307,7 @@ CryptoPermissions getMinimum(CryptoPermissions other) {
*/
private CryptoPermission[] getMinimum(PermissionCollection thisPc,
PermissionCollection thatPc) {
Vector<CryptoPermission> permVector = new Vector<>(2);
ArrayList<CryptoPermission> permList = new ArrayList<>(2);

Enumeration<Permission> thisPcPermissions = thisPc.elements();

Expand All @@ -334,18 +334,16 @@ private CryptoPermission[] getMinimum(PermissionCollection thisPc,
(CryptoPermission)thatPcPermissions.nextElement();

if (thatCp.implies(thisCp)) {
permVector.addElement(thisCp);
permList.add(thisCp);
break;
}
if (thisCp.implies(thatCp)) {
permVector.addElement(thatCp);
permList.add(thatCp);
}
}
}

CryptoPermission[] ret = new CryptoPermission[permVector.size()];
permVector.copyInto(ret);
return ret;
return permList.toArray(new CryptoPermission[0]);
}

/**
Expand All @@ -363,34 +361,32 @@ private CryptoPermission[] getMinimum(PermissionCollection thisPc,
*/
private CryptoPermission[] getMinimum(int maxKeySize,
PermissionCollection pc) {
Vector<CryptoPermission> permVector = new Vector<>(1);
ArrayList<CryptoPermission> permList = new ArrayList<>(1);

Enumeration<Permission> enum_ = pc.elements();

while (enum_.hasMoreElements()) {
CryptoPermission cp =
(CryptoPermission)enum_.nextElement();
if (cp.getMaxKeySize() <= maxKeySize) {
permVector.addElement(cp);
permList.add(cp);
} else {
if (cp.getCheckParam()) {
permVector.addElement(
permList.add(
new CryptoPermission(cp.getAlgorithm(),
maxKeySize,
cp.getAlgorithmParameterSpec(),
cp.getExemptionMechanism()));
} else {
permVector.addElement(
permList.add(
new CryptoPermission(cp.getAlgorithm(),
maxKeySize,
cp.getExemptionMechanism()));
}
}
}

CryptoPermission[] ret = new CryptoPermission[permVector.size()];
permVector.copyInto(ret);
return ret;
return permList.toArray(new CryptoPermission[0]);
}

/**
Expand Down
25 changes: 11 additions & 14 deletions src/java.base/share/classes/javax/crypto/CryptoPolicyParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,7 @@
package javax.crypto;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
Expand Down Expand Up @@ -254,24 +255,23 @@ private CryptoPermissionEntry parsePermissionEntry(
// AlgorithmParameterSpec class name.
String algParamSpecClassName = match("quoted string");

Vector<Integer> paramsV = new Vector<>(1);
ArrayList<Integer> paramsV = new ArrayList<>(1);
while (peek(",")) {
match(",");
if (peek("number")) {
paramsV.addElement(match());
paramsV.add(match());
} else {
if (peek("*")) {
match("*");
paramsV.addElement(Integer.MAX_VALUE);
paramsV.add(Integer.MAX_VALUE);
} else {
throw new ParsingException(st.lineno(),
"Expecting an integer");
}
}
}

Integer[] params = new Integer[paramsV.size()];
paramsV.copyInto(params);
Integer[] params = paramsV.toArray(new Integer[0]);

e.checkParam = true;
e.algParamSpec = getInstance(algParamSpecClassName, params);
Expand Down Expand Up @@ -458,7 +458,7 @@ else if (expect.equalsIgnoreCase("permission type")) {
}

CryptoPermission[] getPermissions() {
Vector<CryptoPermission> result = new Vector<>();
ArrayList<CryptoPermission> result = new ArrayList<>();

Enumeration<GrantEntry> grantEnum = grantEntries.elements();
while (grantEnum.hasMoreElements()) {
Expand All @@ -469,16 +469,16 @@ CryptoPermission[] getPermissions() {
CryptoPermissionEntry pe = permEnum.nextElement();
if (pe.cryptoPermission.equals(
"javax.crypto.CryptoAllPermission")) {
result.addElement(CryptoAllPermission.INSTANCE);
result.add(CryptoAllPermission.INSTANCE);
} else {
if (pe.checkParam) {
result.addElement(new CryptoPermission(
result.add(new CryptoPermission(
pe.alg,
pe.maxKeySize,
pe.algParamSpec,
pe.exemptionMechanism));
} else {
result.addElement(new CryptoPermission(
result.add(new CryptoPermission(
pe.alg,
pe.maxKeySize,
pe.exemptionMechanism));
Expand All @@ -487,10 +487,7 @@ CryptoPermission[] getPermissions() {
}
}

CryptoPermission[] ret = new CryptoPermission[result.size()];
result.copyInto(ret);

return ret;
return result.toArray(new CryptoPermission[0]);
}

private boolean isConsistent(String alg, String exemptionMechanism,
Expand Down

1 comment on commit 79cebe2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.