Path: blob/master/src/java.base/share/classes/sun/security/jca/JCAUtil.java
41159 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.jca;2627import java.lang.ref.*;28import java.security.*;2930/**31* Collection of static utility methods used by the security framework.32*33* @author Andreas Sterbenz34* @since 1.535*/36public final class JCAUtil {3738private JCAUtil() {39// no instantiation40}4142// size of the temporary arrays we use. Should fit into the CPU's 1st43// level cache and could be adjusted based on the platform44private static final int ARRAY_SIZE = 4096;4546/**47* Get the size of a temporary buffer array to use in order to be48* cache efficient. totalSize indicates the total amount of data to49* be buffered. Used by the engineUpdate(ByteBuffer) methods.50*/51public static int getTempArraySize(int totalSize) {52return Math.min(ARRAY_SIZE, totalSize);53}5455// cached SecureRandom instance56private static class CachedSecureRandomHolder {57public static SecureRandom instance = new SecureRandom();58}5960private static volatile SecureRandom def = null;6162/**63* Get a SecureRandom instance. This method should be used by JDK64* internal code in favor of calling "new SecureRandom()". That needs to65* iterate through the provider table to find the default SecureRandom66* implementation, which is fairly inefficient.67*/68public static SecureRandom getSecureRandom() {69return CachedSecureRandomHolder.instance;70}7172// called by sun.security.jca.Providers class when provider list is changed73static void clearDefSecureRandom() {74def = null;75}7677/**78* Get the default SecureRandom instance. This method is the79* optimized version of "new SecureRandom()" which re-uses the default80* SecureRandom impl if the provider table is the same.81*/82public static SecureRandom getDefSecureRandom() {83SecureRandom result = def;84if (result == null) {85synchronized (JCAUtil.class) {86result = def;87if (result == null) {88def = result = new SecureRandom();89}90}91}92return result;9394}95}969798