Skip to content

Commit

Permalink
Stylistic changes
Browse files Browse the repository at this point in the history
- Moved constructor up
- Javadoc added
- inner class is now static
- Added logger attribute
  • Loading branch information
zanella committed Nov 23, 2016
1 parent dc4fa90 commit 3b6ed17
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions core/src/main/java/com/turn/ttorrent/common/ByteBufferPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;

public class ByteBufferPool {

private final GenericObjectPool<ByteBuffer> pool;

public ByteBuffer borrowObject() throws Exception {
return pool.borrowObject();
}
private static final Logger logger =
LoggerFactory.getLogger(ByteBufferPool.class);

public void returnObject(ByteBuffer buffer) {
pool.returnObject(buffer);
}
private final GenericObjectPool<ByteBuffer> pool;

/**
* A limited pool of buffers, so that:
*
* - We don't thrash the memory with a lot of short-lived objects
* - We don't use a lot of memory when we're ingesting a huge amount of data
*
* The ByteBuffers are array backed, so the APIs they get sent to have no need to instantiate one
*
* @param amount The exact amount of buffers this pool will hold
* @param length The legth of each buffer in this pool
*/
public ByteBufferPool(int amount, int length) {
final GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
gopc.setMinIdle(amount);
Expand All @@ -28,7 +36,15 @@ public ByteBufferPool(int amount, int length) {
pool = new GenericObjectPool<ByteBuffer>(new ByteBufferFactory(length), gopc);
}

private class ByteBufferFactory extends BasePooledObjectFactory<ByteBuffer> {
public ByteBuffer borrowObject() throws Exception {
return pool.borrowObject();
}

public void returnObject(ByteBuffer buffer) {
pool.returnObject(buffer);
}

private static class ByteBufferFactory extends BasePooledObjectFactory<ByteBuffer> {
private final int length;

private ByteBufferFactory(int length) {
Expand Down

0 comments on commit 3b6ed17

Please sign in to comment.