- java.lang.Object
-
- com.aoapps.lang.util.BufferManager
-
public final class BufferManager extends Object
BufferManagermanages a reusable pool ofbyte[]andchar[]buffers. This avoids the repetitive allocation of memory for an operation that only needs a temporary buffer. The buffers are stored asThreadLocalto maximize cache locality.Do not use if intra-thread security is more important than performance.
The buffers are not necessarily cleared between invocations so the results of previous operations may be available to additional callers. On the scale of security versus performance, this is biased toward performance. However, being thread local there remains some control over the visibility of the data.
Buffers should not be passed between threads. Giving a thread a buffer you didn't get from it could result in a memory or information leak. Soft references are used to avoid full-on memory leaks, but keeping buffers to a single thread is optimal.
Under no circumstances should a buffer be released more than once. This may result in the buffer being allocated twice at the same time, with resulting data corruption.
The Java virtual machine has improved greatly over the years. However, we still believe this buffer management to be valuable to reduce garbage collection pressure. If this ever proves to not be the case, the implementation here can be simply changed to create new arrays on each use.
- Author:
- AO Industries, Inc.
-
-
Field Summary
Fields Modifier and Type Field Description static intBUFFER_SIZEThe size of buffers that are returned.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static longgetByteBufferCreates()Gets the number ofbyte[]buffers instantiated.static longgetByteBuffersCollected()Gets the number ofbyte[]buffers detected to have been garbage collected.static longgetByteBufferUses()Gets the number of timebyte[]buffers have been used.static longgetByteBufferZeroFills()Gets the number of timebyte[]buffers have been zero-filled on release.static byte[]getBytes()Gets abyte[]of lengthBUFFER_SIZEthat may be temporarily used for any purpose.static longgetCharBufferCreates()Gets the number ofchar[]buffers instantiated.static longgetCharBuffersCollected()Gets the number ofchar[]buffers detected to have been garbage collected.static longgetCharBufferUses()Gets the number of timechar[]buffers have been used.static longgetCharBufferZeroFills()Gets the number of timechar[]buffers have been zero-filled on release.static char[]getChars()Gets achar[]of lengthBUFFER_SIZEthat may be temporarily used for any purpose.static voidrelease(byte[] buffer)Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data.static voidrelease(byte[] buffer, boolean zeroFill)Releases abyte[]that was obtained by a call togetBytes.static voidrelease(char[] buffer)Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data.static voidrelease(char[] buffer, boolean zeroFill)Releases achar[]that was obtained by a call togetChars.
-
-
-
Field Detail
-
BUFFER_SIZE
public static final int BUFFER_SIZE
The size of buffers that are returned.- See Also:
- Constant Field Values
-
-
Method Detail
-
getBytes
public static byte[] getBytes()
Gets abyte[]of lengthBUFFER_SIZEthat may be temporarily used for any purpose. Once done with the buffer,releaseshould be called, this is best accomplished in afinallyblock. The buffer is not necessarily zero-filled and may contain data from a previous use.
-
getChars
public static char[] getChars()
Gets achar[]of lengthBUFFER_SIZEthat may be temporarily used for any purpose. Once done with the buffer,releaseshould be called, this is best accomplished in afinallyblock. The buffer is not necessarily zero-filled and may contain data from a previous use.
-
release
@Deprecated public static void release(byte[] buffer)
Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data.
-
release
public static void release(byte[] buffer, boolean zeroFill)Releases abyte[]that was obtained by a call togetBytes. A buffer must not be released more than once.- Parameters:
buffer- thebyte[]to releasezeroFill- if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release.
-
release
@Deprecated public static void release(char[] buffer)
Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data.
-
release
public static void release(char[] buffer, boolean zeroFill)Releases achar[]that was obtained by a call togetChars. A buffer must not be released more than once.- Parameters:
buffer- thechar[]to releasezeroFill- if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release.
-
getByteBufferCreates
public static long getByteBufferCreates()
Gets the number ofbyte[]buffers instantiated.
-
getByteBufferUses
public static long getByteBufferUses()
Gets the number of timebyte[]buffers have been used.
-
getByteBufferZeroFills
public static long getByteBufferZeroFills()
Gets the number of timebyte[]buffers have been zero-filled on release.
-
getByteBuffersCollected
public static long getByteBuffersCollected()
Gets the number ofbyte[]buffers detected to have been garbage collected.
-
getCharBufferCreates
public static long getCharBufferCreates()
Gets the number ofchar[]buffers instantiated.
-
getCharBufferUses
public static long getCharBufferUses()
Gets the number of timechar[]buffers have been used.
-
getCharBufferZeroFills
public static long getCharBufferZeroFills()
Gets the number of timechar[]buffers have been zero-filled on release.
-
getCharBuffersCollected
public static long getCharBuffersCollected()
Gets the number ofchar[]buffers detected to have been garbage collected.
-
-