0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.jca;
0N/A
0N/Aimport java.lang.ref.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/A/**
0N/A * Collection of static utility methods used by the security framework.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.5
0N/A */
0N/Apublic final class JCAUtil {
0N/A
0N/A private JCAUtil() {
0N/A // no instantiation
0N/A }
0N/A
0N/A // lock to use for synchronization
0N/A private static final Object LOCK = JCAUtil.class;
0N/A
0N/A // cached SecureRandom instance
0N/A private static volatile SecureRandom secureRandom;
0N/A
0N/A // size of the temporary arrays we use. Should fit into the CPU's 1st
0N/A // level cache and could be adjusted based on the platform
0N/A private final static int ARRAY_SIZE = 4096;
0N/A
0N/A /**
0N/A * Get the size of a temporary buffer array to use in order to be
0N/A * cache efficient. totalSize indicates the total amount of data to
0N/A * be buffered. Used by the engineUpdate(ByteBuffer) methods.
0N/A */
0N/A public static int getTempArraySize(int totalSize) {
0N/A return Math.min(ARRAY_SIZE, totalSize);
0N/A }
0N/A
0N/A /**
0N/A * Get a SecureRandom instance. This method should me used by JDK
0N/A * internal code in favor of calling "new SecureRandom()". That needs to
0N/A * iterate through the provider table to find the default SecureRandom
0N/A * implementation, which is fairly inefficient.
0N/A */
0N/A public static SecureRandom getSecureRandom() {
0N/A // we use double checked locking to minimize synchronization
0N/A // works because we use a volatile reference
0N/A SecureRandom r = secureRandom;
0N/A if (r == null) {
0N/A synchronized (LOCK) {
0N/A r = secureRandom;
0N/A if (r == null) {
0N/A r = new SecureRandom();
0N/A secureRandom = r;
0N/A }
0N/A }
0N/A }
0N/A return r;
0N/A }
0N/A
0N/A}