@Override public void finalize() { mPool.returnBuf(buf); }
@Override public void finalize() { mPool.returnBuf(buf); }
@Override public void finalize() { mPool.returnBuf(buf); }
@Override public void close() throws IOException { mPool.returnBuf(buf); buf = null; super.close(); }
@Override public void close() throws IOException { mPool.returnBuf(buf); buf = null; super.close(); }
@Override public void close() throws IOException { mPool.returnBuf(buf); buf = null; super.close(); }
/** * 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; }
/** * 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; }
/** * 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; }
/** 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(); } }
/** 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(); } }
/** 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(); } }
@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); }
@Test public void obeysSizeLimit() { ByteArrayPool pool = new ByteArrayPool(32); byte[] buf1 = pool.getBuf(16); byte[] buf2 = pool.getBuf(16); byte[] buf3 = pool.getBuf(16); pool.returnBuf(buf1); pool.returnBuf(buf2); pool.returnBuf(buf3); byte[] buf4 = pool.getBuf(16); byte[] buf5 = pool.getBuf(16); byte[] buf6 = pool.getBuf(16); assertTrue(buf4 == buf2 || buf4 == buf3); assertTrue(buf5 == buf2 || buf5 == buf3); assertTrue(buf4 != buf5); assertTrue(buf6 != buf1 && buf6 != buf2 && buf6 != buf3); }
@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); }
@Test public void obeysSizeLimit() { ByteArrayPool pool = new ByteArrayPool(32); byte[] buf1 = pool.getBuf(16); byte[] buf2 = pool.getBuf(16); byte[] buf3 = pool.getBuf(16); pool.returnBuf(buf1); pool.returnBuf(buf2); pool.returnBuf(buf3); byte[] buf4 = pool.getBuf(16); byte[] buf5 = pool.getBuf(16); byte[] buf6 = pool.getBuf(16); assertTrue(buf4 == buf2 || buf4 == buf3); assertTrue(buf5 == buf2 || buf5 == buf3); assertTrue(buf4 != buf5); assertTrue(buf6 != buf1 && buf6 != buf2 && buf6 != buf3); }
@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); } }
@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); } }
@Override public void close() throws IOException { mPool.returnBuf(buf); buf = null; super.close(); }
@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); } }