/** * Releases a {@code byte[]} that was obtained by a call to * {@code getBytes}. A buffer must not be released more than once. * * @param buffer the {@code byte[]} to release * @param zeroFill if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release. */ public static void release(byte[] buffer, boolean zeroFill) { Deque<SoftReference<byte[]>> myBytes = bytes.get(); if(buffer.length != BUFFER_SIZE) throw new IllegalArgumentException(); assert !inQueue(myBytes, buffer); // Error if already in the buffer list if(zeroFill) { bytesZeroFills.getAndIncrement(); Arrays.fill(buffer, 0, BUFFER_SIZE, (byte)0); } myBytes.add(new SoftReference<byte[]>(buffer)); } private static boolean inQueue(Iterable<SoftReference<byte[]>> myBytes, byte[] buffer) {
/** * Releases a {@code char[]} that was obtained by a call to * {@code getChars}. A buffer must not be released more than once. * * @param buffer the {@code char[]} to release * @param zeroFill if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release. */ public static void release(char[] buffer, boolean zeroFill) { Deque<SoftReference<char[]>> myChars = chars.get(); if(buffer.length != BUFFER_SIZE) throw new IllegalArgumentException(); assert !inQueue(myChars, buffer); // Error if already in the buffer list if(zeroFill) { charsZeroFills.getAndIncrement(); Arrays.fill(buffer, 0, BUFFER_SIZE, (char)0); } myChars.add(new SoftReference<char[]>(buffer)); } private static boolean inQueue(Iterable<SoftReference<char[]>> myChars, char[] buffer) {