AndBase开发框架  1.6
 全部  命名空间 文件 函数 变量 枚举值 
Public 成员函数 | 静态 Protected 属性 | Private 成员函数 | Private 属性 | 所有成员列表
com.ab.network.toolbox.ByteArrayPool类 参考

Public 成员函数

 ByteArrayPool (int sizeLimit)
 
synchronized byte[] getBuf (int len)
 
synchronized void returnBuf (byte[] buf)
 

静态 Protected 属性

static final Comparator< byte[]> BUF_COMPARATOR
 

Private 成员函数

synchronized void trim ()
 

Private 属性

List< byte[]> mBuffersByLastUse = new LinkedList<byte[]>()
 
List< byte[]> mBuffersBySize = new ArrayList<byte[]>(64)
 
int mCurrentSize = 0
 
final int mSizeLimit
 

详细描述

ByteArrayPool is a source and repository of byte[] objects. Its purpose is to supply those buffers to consumers who need to use them for a short period of time and then dispose of them. Simply creating and disposing such buffers in the conventional manner can considerable heap churn and garbage collection delays on Android, which lacks good management of short-lived heap objects. It may be advantageous to trade off some memory in the form of a permanently allocated pool of buffers in order to gain heap performance improvements; that is what this class does.

A good candidate user for this class is something like an I/O system that uses large temporary byte[] buffers to copy data around. In these use cases, often the consumer wants the buffer to be a certain minimum size to ensure good performance (e.g. when copying data chunks off of a stream), but doesn't mind if the buffer is larger than the minimum. Taking this into account and also to maximize the odds of being able to reuse a recycled buffer, this class is free to return buffers larger than the requested size. The caller needs to be able to gracefully deal with getting buffers any size over the minimum.

If there is not a suitably-sized buffer in its recycling pool when a buffer is requested, this class will allocate a new buffer and return it.

This class has no special ownership of buffers it creates; the caller is free to take a buffer it receives from this pool, use it permanently, and never return it to the pool; additionally, it is not harmful to return to this pool a buffer that was allocated elsewhere, provided there are no other lingering references to it.

This class ensures that the total size of the buffers in its recycling pool never exceeds a certain byte limit. When a buffer is returned that would cause the pool to exceed the limit, least-recently-used buffers are disposed.

构造及析构函数说明

com.ab.network.toolbox.ByteArrayPool.ByteArrayPool ( int  sizeLimit)
inline
参数
sizeLimitthe maximum size of the pool, in bytes

成员函数说明

synchronized byte [] com.ab.network.toolbox.ByteArrayPool.getBuf ( int  len)
inline

Returns a buffer from the pool if one is available in the requested size, or allocates a new one if a pooled one is not available.

参数
lenthe minimum size, in bytes, of the requested buffer. The returned buffer may be larger.
返回
a byte[] buffer is always returned.
synchronized void com.ab.network.toolbox.ByteArrayPool.returnBuf ( byte[]  buf)
inline

Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted size.

参数
bufthe buffer to return to the pool.
synchronized void com.ab.network.toolbox.ByteArrayPool.trim ( )
inlineprivate

Removes buffers from the pool until it is under its size limit.

类成员变量说明

final Comparator<byte[]> com.ab.network.toolbox.ByteArrayPool.BUF_COMPARATOR
staticprotected
初始值:
= new Comparator<byte[]>() {
@Override
public int compare(byte[] lhs, byte[] rhs) {
return lhs.length - rhs.length;
}
}

Compares buffers by size

List<byte[]> com.ab.network.toolbox.ByteArrayPool.mBuffersByLastUse = new LinkedList<byte[]>()
private

The buffer pool, arranged both by last use and by buffer size

List<byte[]> com.ab.network.toolbox.ByteArrayPool.mBuffersBySize = new ArrayList<byte[]>(64)
private
int com.ab.network.toolbox.ByteArrayPool.mCurrentSize = 0
private

The total size of the buffers in the pool

final int com.ab.network.toolbox.ByteArrayPool.mSizeLimit
private

The maximum aggregate size of the buffers in the pool. Old buffers are discarded to stay under this limit.


该类的文档由以下文件生成: