-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBufferPool.java
More file actions
64 lines (51 loc) · 1.64 KB
/
BufferPool.java
File metadata and controls
64 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.timgroup.statsd;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BufferPool {
private final BlockingQueue<ByteBuffer> pool;
private final int size;
private final int bufferSize;
private final boolean direct;
BufferPool(final int poolSize, int bufferSize, final boolean direct) throws InterruptedException {
size = poolSize;
this.bufferSize = bufferSize;
this.direct = direct;
pool = new ArrayBlockingQueue<ByteBuffer>(poolSize);
for (int i = 0; i < size ; i++) {
if (direct) {
pool.put(ByteBuffer.allocateDirect(bufferSize));
} else {
pool.put(ByteBuffer.allocate(bufferSize));
}
}
}
BufferPool(final BufferPool pool) throws InterruptedException {
this.size = pool.size;
this.bufferSize = pool.bufferSize;
this.direct = pool.direct;
this.pool = new ArrayBlockingQueue<ByteBuffer>(pool.size);
for (int i = 0; i < size ; i++) {
if (direct) {
this.pool.put(ByteBuffer.allocateDirect(bufferSize));
} else {
this.pool.put(ByteBuffer.allocate(bufferSize));
}
}
}
ByteBuffer borrow() throws InterruptedException {
return pool.take();
}
void put(ByteBuffer buffer) throws InterruptedException {
pool.put(buffer);
}
int getSize() {
return size;
}
int getBufferSize() {
return bufferSize;
}
int available() {
return pool.size();
}
}