类 ByteArrayPool

java.lang.Object
com.android.volley.toolbox.ByteArrayPool

public class ByteArrayPool extends Object
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.

  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    protected static final Comparator<byte[]>
    Compares buffers by size
  • 构造器概要

    构造器
    构造器
    说明
    ByteArrayPool(int sizeLimit)
     
  • 方法概要

    修饰符和类型
    方法
    说明
    byte[]
    getBuf(int len)
    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.
    void
    returnBuf(byte[] buf)
    Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted size.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详细资料

    • BUF_COMPARATOR

      protected static final Comparator<byte[]> BUF_COMPARATOR
      Compares buffers by size
  • 构造器详细资料

    • ByteArrayPool

      public ByteArrayPool(int sizeLimit)
      参数:
      sizeLimit - the maximum size of the pool, in bytes
  • 方法详细资料

    • getBuf

      public byte[] getBuf(int len)
      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.
      参数:
      len - the minimum size, in bytes, of the requested buffer. The returned buffer may be larger.
      返回:
      a byte[] buffer is always returned.
    • returnBuf

      public void returnBuf(byte[] buf)
      Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted size.
      参数:
      buf - the buffer to return to the pool.