J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * 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 | * @author Andreas Sterbenz |
| 36 | * @since 1.5 |
| 37 | */ |
| 38 | public final class JCAUtil { |
| 39 | |
| 40 | private JCAUtil() { |
| 41 | // no instantiation |
| 42 | } |
| 43 | |
| 44 | // lock to use for synchronization |
| 45 | private static final Object LOCK = JCAUtil.class; |
| 46 | |
| 47 | // cached SecureRandom instance |
| 48 | private static volatile SecureRandom secureRandom; |
| 49 | |
| 50 | // size of the temporary arrays we use. Should fit into the CPU's 1st |
| 51 | // level cache and could be adjusted based on the platform |
| 52 | private final static int ARRAY_SIZE = 4096; |
| 53 | |
| 54 | /** |
| 55 | * Get the size of a temporary buffer array to use in order to be |
| 56 | * cache efficient. totalSize indicates the total amount of data to |
| 57 | * be buffered. Used by the engineUpdate(ByteBuffer) methods. |
| 58 | */ |
| 59 | public static int getTempArraySize(int totalSize) { |
| 60 | return Math.min(ARRAY_SIZE, totalSize); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get a SecureRandom instance. This method should me used by JDK |
| 65 | * internal code in favor of calling "new SecureRandom()". That needs to |
| 66 | * iterate through the provider table to find the default SecureRandom |
| 67 | * implementation, which is fairly inefficient. |
| 68 | */ |
| 69 | public static SecureRandom getSecureRandom() { |
| 70 | // we use double checked locking to minimize synchronization |
| 71 | // works because we use a volatile reference |
| 72 | SecureRandom r = secureRandom; |
| 73 | if (r == null) { |
| 74 | synchronized (LOCK) { |
| 75 | r = secureRandom; |
| 76 | if (r == null) { |
| 77 | r = new SecureRandom(); |
| 78 | secureRandom = r; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return r; |
| 83 | } |
| 84 | |
| 85 | } |