01: /*
02: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.security.jca;
27:
28: import java.lang.ref.*;
29:
30: import java.security.*;
31:
32: /**
33: * Collection of static utility methods used by the security framework.
34: *
35: * @version 1.9, 05/05/07
36: * @author Andreas Sterbenz
37: * @since 1.5
38: */
39: public final class JCAUtil {
40:
41: private JCAUtil() {
42: // no instantiation
43: }
44:
45: // lock to use for synchronization
46: private static final Object LOCK = JCAUtil.class;
47:
48: // cached SecureRandom instance
49: private static volatile SecureRandom secureRandom;
50:
51: // size of the temporary arrays we use. Should fit into the CPU's 1st
52: // level cache and could be adjusted based on the platform
53: private final static int ARRAY_SIZE = 4096;
54:
55: /**
56: * Get the size of a temporary buffer array to use in order to be
57: * cache efficient. totalSize indicates the total amount of data to
58: * be buffered. Used by the engineUpdate(ByteBuffer) methods.
59: */
60: public static int getTempArraySize(int totalSize) {
61: return Math.min(ARRAY_SIZE, totalSize);
62: }
63:
64: /**
65: * Get a SecureRandom instance. This method should me used by JDK
66: * internal code in favor of calling "new SecureRandom()". That needs to
67: * iterate through the provider table to find the default SecureRandom
68: * implementation, which is fairly inefficient.
69: */
70: public static SecureRandom getSecureRandom() {
71: // we use double checked locking to minimize synchronization
72: // works because we use a volatile reference
73: SecureRandom r = secureRandom;
74: if (r == null) {
75: synchronized (LOCK) {
76: r = secureRandom;
77: if (r == null) {
78: r = new SecureRandom();
79: secureRandom = r;
80: }
81: }
82: }
83: return r;
84: }
85:
86: }
|