1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.io;
20
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.eclipse.jetty.util.BufferUtil;
26
27
28
29
30
31
32
33 public interface ByteBufferPool
34 {
35
36
37
38
39
40
41
42
43
44
45 public ByteBuffer acquire(int size, boolean direct);
46
47
48
49
50
51
52
53
54 public void release(ByteBuffer buffer);
55
56 public static class Lease
57 {
58 private final ByteBufferPool byteBufferPool;
59 private final List<ByteBuffer> buffers;
60 private final List<Boolean> recycles;
61
62 public Lease(ByteBufferPool byteBufferPool)
63 {
64 this.byteBufferPool = byteBufferPool;
65 this.buffers = new ArrayList<>();
66 this.recycles = new ArrayList<>();
67 }
68
69 public ByteBuffer acquire(int capacity, boolean direct)
70 {
71 ByteBuffer buffer = byteBufferPool.acquire(capacity, direct);
72 BufferUtil.clearToFill(buffer);
73 return buffer;
74 }
75
76 public void append(ByteBuffer buffer, boolean recycle)
77 {
78 buffers.add(buffer);
79 recycles.add(recycle);
80 }
81
82 public void insert(int index, ByteBuffer buffer, boolean recycle)
83 {
84 buffers.add(index, buffer);
85 recycles.add(index, recycle);
86 }
87
88 public List<ByteBuffer> getByteBuffers()
89 {
90 return buffers;
91 }
92
93 public long getTotalLength()
94 {
95 long length = 0;
96 for (int i = 0; i < buffers.size(); ++i)
97 length += buffers.get(i).remaining();
98 return length;
99 }
100
101 public int getSize()
102 {
103 return buffers.size();
104 }
105
106 public void recycle()
107 {
108 for (int i = 0; i < buffers.size(); ++i)
109 {
110 ByteBuffer buffer = buffers.get(i);
111 if (recycles.get(i))
112 byteBufferPool.release(buffer);
113 }
114 buffers.clear();
115 recycles.clear();
116 }
117 }
118 }