congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
ByteArrayPool
Code IndexAdd Tabnine to your IDE (free)

How to use
ByteArrayPool
in
com.android.volley.toolbox

Best Java code snippets using com.android.volley.toolbox.ByteArrayPool (Showing top 20 results out of 315)

origin: mcxiaoke/android-volley

@Test public void reusesBuffer() {
  ByteArrayPool pool = new ByteArrayPool(32);
  byte[] buf1 = pool.getBuf(16);
  byte[] buf2 = pool.getBuf(16);
  pool.returnBuf(buf1);
  pool.returnBuf(buf2);
  byte[] buf3 = pool.getBuf(16);
  byte[] buf4 = pool.getBuf(16);
  assertTrue(buf3 == buf1 || buf3 == buf2);
  assertTrue(buf4 == buf1 || buf4 == buf2);
  assertTrue(buf3 != buf4);
}
origin: chentao0707/SimplifyReader

/**
 * Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted
 * size.
 *
 * @param buf the buffer to return to the pool.
 */
public synchronized void returnBuf(byte[] buf) {
  if (buf == null || buf.length > mSizeLimit) {
    return;
  }
  mBuffersByLastUse.add(buf);
  int pos = Collections.binarySearch(mBuffersBySize, buf, BUF_COMPARATOR);
  if (pos < 0) {
    pos = -pos - 1;
  }
  mBuffersBySize.add(pos, buf);
  mCurrentSize += buf.length;
  trim();
}
origin: chentao0707/SimplifyReader

/**
 * Ensures there is enough space in the buffer for the given number of additional bytes.
 */
private void expand(int i) {
  /* Can the buffer handle @i more bytes, if not expand it */
  if (count + i <= buf.length) {
    return;
  }
  byte[] newbuf = mPool.getBuf((count + i) * 2);
  System.arraycopy(buf, 0, newbuf, 0, count);
  mPool.returnBuf(buf);
  buf = newbuf;
}
origin: chentao0707/SimplifyReader

/**
 * @param httpStack HTTP stack to be used
 */
public BasicNetwork(HttpStack httpStack) {
  // If a pool isn't passed in, then build a small default pool that will give us a lot of
  // benefit and not use too much memory.
  this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
}
origin: mcxiaoke/android-volley

@Override
public void finalize() {
  mPool.returnBuf(buf);
}
origin: chentao0707/SimplifyReader

/**
 * Constructs a new {@code ByteArrayOutputStream} with a default size of {@code size} bytes. If
 * more than {@code size} bytes are written to this instance, the underlying byte array will
 * expand.
 *
 * @param size initial size for the underlying byte array. The value will be pinned to a default
 *        minimum size.
 */
public PoolingByteArrayOutputStream(ByteArrayPool pool, int size) {
  mPool = pool;
  buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
}
origin: mcxiaoke/android-volley

/**
 * Ensures there is enough space in the buffer for the given number of additional bytes.
 */
private void expand(int i) {
  /* Can the buffer handle @i more bytes, if not expand it */
  if (count + i <= buf.length) {
    return;
  }
  byte[] newbuf = mPool.getBuf((count + i) * 2);
  System.arraycopy(buf, 0, newbuf, 0, count);
  mPool.returnBuf(buf);
  buf = newbuf;
}
origin: mcxiaoke/android-volley

/**
 * @param httpStack HTTP stack to be used
 */
public BasicNetwork(HttpStack httpStack) {
  // If a pool isn't passed in, then build a small default pool that will give us a lot of
  // benefit and not use too much memory.
  this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
}
origin: chentao0707/SimplifyReader

@Override
public void finalize() {
  mPool.returnBuf(buf);
}
origin: mcxiaoke/android-volley

/**
 * Constructs a new {@code ByteArrayOutputStream} with a default size of {@code size} bytes. If
 * more than {@code size} bytes are written to this instance, the underlying byte array will
 * expand.
 *
 * @param size initial size for the underlying byte array. The value will be pinned to a default
 *        minimum size.
 */
public PoolingByteArrayOutputStream(ByteArrayPool pool, int size) {
  mPool = pool;
  buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
}
origin: mcxiaoke/android-volley

  @Test public void returnsBufferWithRightSize() {
    ByteArrayPool pool = new ByteArrayPool(32);

    byte[] buf1 = pool.getBuf(16);
    pool.returnBuf(buf1);

    byte[] buf2 = pool.getBuf(17);
    assertNotSame(buf2, buf1);

    byte[] buf3 = pool.getBuf(15);
    assertSame(buf3, buf1);
  }
}
origin: jiangqqlmj/FastDev4Android

/**
 * Ensures there is enough space in the buffer for the given number of additional bytes.
 */
private void expand(int i) {
  /* Can the buffer handle @i more bytes, if not expand it */
  if (count + i <= buf.length) {
    return;
  }
  byte[] newbuf = mPool.getBuf((count + i) * 2);
  System.arraycopy(buf, 0, newbuf, 0, count);
  mPool.returnBuf(buf);
  buf = newbuf;
}
origin: jiangqqlmj/FastDev4Android

/**
 * @param httpStack HTTP stack to be used
 */
public BasicNetwork(HttpStack httpStack) {
  // If a pool isn't passed in, then build a small default pool that will give us a lot of
  // benefit and not use too much memory.
  this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
}
origin: jiangqqlmj/FastDev4Android

@Override
public void finalize() {
  mPool.returnBuf(buf);
}
origin: jiangqqlmj/FastDev4Android

/**
 * Constructs a new {@code ByteArrayOutputStream} with a default size of {@code size} bytes. If
 * more than {@code size} bytes are written to this instance, the underlying byte array will
 * expand.
 *
 * @param size initial size for the underlying byte array. The value will be pinned to a default
 *        minimum size.
 */
public PoolingByteArrayOutputStream(ByteArrayPool pool, int size) {
  mPool = pool;
  buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
}
origin: mcxiaoke/android-volley

/**
 * Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted
 * size.
 *
 * @param buf the buffer to return to the pool.
 */
public synchronized void returnBuf(byte[] buf) {
  if (buf == null || buf.length > mSizeLimit) {
    return;
  }
  mBuffersByLastUse.add(buf);
  int pos = Collections.binarySearch(mBuffersBySize, buf, BUF_COMPARATOR);
  if (pos < 0) {
    pos = -pos - 1;
  }
  mBuffersBySize.add(pos, buf);
  mCurrentSize += buf.length;
  trim();
}
origin: jiangqqlmj/FastDev4Android

@Test public void reusesBuffer() {
  ByteArrayPool pool = new ByteArrayPool(32);
  byte[] buf1 = pool.getBuf(16);
  byte[] buf2 = pool.getBuf(16);
  pool.returnBuf(buf1);
  pool.returnBuf(buf2);
  byte[] buf3 = pool.getBuf(16);
  byte[] buf4 = pool.getBuf(16);
  assertTrue(buf3 == buf1 || buf3 == buf2);
  assertTrue(buf4 == buf1 || buf4 == buf2);
  assertTrue(buf3 != buf4);
}
origin: chentao0707/SimplifyReader

/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
  PoolingByteArrayOutputStream bytes =
      new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
  byte[] buffer = null;
  try {
    InputStream in = entity.getContent();
    if (in == null) {
      throw new ServerError();
    }
    buffer = mPool.getBuf(1024);
    int count;
    while ((count = in.read(buffer)) != -1) {
      bytes.write(buffer, 0, count);
    }
    return bytes.toByteArray();
  } finally {
    try {
      // Close the InputStream and release the resources by "consuming the content".
      entity.consumeContent();
    } catch (IOException e) {
      // This can happen if there was an exception above that left the entity in
      // an invalid state.
      VolleyLog.v("Error occured when calling consumingContent");
    }
    mPool.returnBuf(buffer);
    bytes.close();
  }
}
origin: mcxiaoke/android-volley

@Test public void pooledIndividualWrites() throws IOException {
  ByteArrayPool pool = new ByteArrayPool(32768);
  writeBytesIndividually(pool);
  writeBytesIndividually(pool);
  writeBytesIndividually(pool);
}
origin: chentao0707/SimplifyReader

@Override
public void close() throws IOException {
  mPool.returnBuf(buf);
  buf = null;
  super.close();
}
com.android.volley.toolboxByteArrayPool

Javadoc

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.

Most used methods

  • <init>
  • getBuf
    Returns a buffer from the pool if one is available in the requested size, or allocates a new one if
  • returnBuf
    Returns a buffer to the pool, throwing away old buffers if the pool would exceed its allotted size.
  • trim
    Removes buffers from the pool until it is under its size limit.

Popular in Java

  • Reading from database using SQL prepared statement
  • startActivity (Activity)
  • notifyDataSetChanged (ArrayAdapter)
  • scheduleAtFixedRate (Timer)
  • Point (java.awt)
    A point representing a location in (x,y) coordinate space, specified in integer precision.
  • BigDecimal (java.math)
    An immutable arbitrary-precision signed decimal.A value is represented by an arbitrary-precision "un
  • Proxy (java.net)
    This class represents proxy server settings. A created instance of Proxy stores a type and an addres
  • ArrayList (java.util)
    ArrayList is an implementation of List, backed by an array. All optional operations including adding
  • Set (java.util)
    A Set is a data structure which does not allow duplicate elements.
  • TreeSet (java.util)
    TreeSet is an implementation of SortedSet. All optional operations (adding and removing) are support
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now