Mercurial Hosting > luan
comparison src/org/eclipse/jetty/io/PooledBuffers.java @ 1006:58a9c4a42292
simplify Buffers code
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 23 Oct 2016 17:21:20 -0600 |
parents | 3428c60d7cfc |
children | c3a04bded909 |
comparison
equal
deleted
inserted
replaced
1005:0e96ce3db20a | 1006:58a9c4a42292 |
---|---|
20 | 20 |
21 import java.util.Queue; | 21 import java.util.Queue; |
22 import java.util.concurrent.ConcurrentLinkedQueue; | 22 import java.util.concurrent.ConcurrentLinkedQueue; |
23 import java.util.concurrent.atomic.AtomicInteger; | 23 import java.util.concurrent.atomic.AtomicInteger; |
24 | 24 |
25 public class PooledBuffers extends AbstractBuffers | 25 public final class PooledBuffers extends AbstractBuffers |
26 { | 26 { |
27 private final Queue<Buffer> _headers; | 27 private final Queue<Buffer> _headers; |
28 private final Queue<Buffer> _buffers; | 28 private final Queue<Buffer> _buffers; |
29 private final Queue<Buffer> _others; | 29 private final Queue<Buffer> _others; |
30 private final AtomicInteger _size = new AtomicInteger(); | 30 private final AtomicInteger _size = new AtomicInteger(); |
31 private final int _maxSize; | 31 private final int _maxSize; |
32 private final boolean _otherHeaders; | 32 private final boolean _otherHeaders; |
33 private final boolean _otherBuffers; | |
34 | 33 |
35 /* ------------------------------------------------------------ */ | 34 public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize,int maxSize) |
36 public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType,int maxSize) | 35 { |
37 { | 36 super(headerType,headerSize,bufferType,bufferSize); |
38 super(headerType,headerSize,bufferType,bufferSize,otherType); | 37 _headers=new ConcurrentLinkedQueue<Buffer>(); |
39 _headers=new ConcurrentLinkedQueue<Buffer>(); | 38 _buffers=new ConcurrentLinkedQueue<Buffer>(); |
40 _buffers=new ConcurrentLinkedQueue<Buffer>(); | 39 _others=new ConcurrentLinkedQueue<Buffer>(); |
41 _others=new ConcurrentLinkedQueue<Buffer>(); | 40 _otherHeaders=headerType==bufferType; |
42 _otherHeaders=headerType==otherType; | 41 _maxSize=maxSize; |
43 _otherBuffers=bufferType==otherType; | 42 } |
44 _maxSize=maxSize; | |
45 } | |
46 | 43 |
47 /* ------------------------------------------------------------ */ | 44 public Buffer getHeader() |
48 public Buffer getHeader() | 45 { |
49 { | 46 Buffer buffer = _headers.poll(); |
50 Buffer buffer = _headers.poll(); | 47 if (buffer==null) |
51 if (buffer==null) | 48 buffer=newHeader(); |
52 buffer=newHeader(); | 49 else |
53 else | 50 _size.decrementAndGet(); |
54 _size.decrementAndGet(); | 51 return buffer; |
55 return buffer; | 52 } |
56 } | |
57 | 53 |
58 /* ------------------------------------------------------------ */ | 54 public Buffer getBuffer() |
59 public Buffer getBuffer() | 55 { |
60 { | 56 Buffer buffer = _buffers.poll(); |
61 Buffer buffer = _buffers.poll(); | 57 if (buffer==null) |
62 if (buffer==null) | 58 buffer=newBuffer(); |
63 buffer=newBuffer(); | 59 else |
64 else | 60 _size.decrementAndGet(); |
65 _size.decrementAndGet(); | 61 return buffer; |
66 return buffer; | 62 } |
67 } | |
68 | 63 |
69 /* ------------------------------------------------------------ */ | 64 /* ------------------------------------------------------------ */ |
70 public Buffer getBuffer(int size) | 65 public Buffer getBuffer(int size) |
71 { | 66 { |
72 if (_otherHeaders && size==getHeaderSize()) | 67 if (_otherHeaders && size==getHeaderSize()) |
73 return getHeader(); | 68 return getHeader(); |
74 if (_otherBuffers && size==getBufferSize()) | 69 if (size==getBufferSize()) |
75 return getBuffer(); | 70 return getBuffer(); |
76 | 71 |
77 // Look for an other buffer | 72 // Look for an other buffer |
78 Buffer buffer = _others.poll(); | 73 Buffer buffer = _others.poll(); |
79 | 74 |
80 // consume all other buffers until one of the right size is found | 75 // consume all other buffers until one of the right size is found |
81 while (buffer!=null && buffer.capacity()!=size) | 76 while (buffer!=null && buffer.capacity()!=size) |
82 { | 77 { |
83 _size.decrementAndGet(); | 78 _size.decrementAndGet(); |
84 buffer = _others.poll(); | 79 buffer = _others.poll(); |
85 } | 80 } |
86 | 81 |
87 if (buffer==null) | 82 if (buffer==null) |
88 buffer=newBuffer(size); | 83 buffer=newBuffer(size); |
89 else | 84 else |
90 _size.decrementAndGet(); | 85 _size.decrementAndGet(); |
91 return buffer; | 86 return buffer; |
92 } | 87 } |
93 | 88 |
94 /* ------------------------------------------------------------ */ | 89 /* ------------------------------------------------------------ */ |
95 public void returnBuffer(Buffer buffer) | 90 public void returnBuffer(Buffer buffer) |
96 { | 91 { |
97 buffer.clear(); | 92 buffer.clear(); |
98 if (buffer.isVolatile() || buffer.isImmutable()) | 93 if (buffer.isVolatile() || buffer.isImmutable()) |
99 return; | 94 return; |
100 | 95 |
101 if (_size.incrementAndGet() > _maxSize) | 96 if (_size.incrementAndGet() > _maxSize) |
102 _size.decrementAndGet(); | 97 _size.decrementAndGet(); |
103 else | 98 else |
104 { | 99 { |
105 if (isHeader(buffer)) | 100 if (isHeader(buffer)) |
106 _headers.add(buffer); | 101 _headers.add(buffer); |
107 else if (isBuffer(buffer)) | 102 else if (isBuffer(buffer)) |
108 _buffers.add(buffer); | 103 _buffers.add(buffer); |
109 else | 104 else |
110 _others.add(buffer); | 105 _others.add(buffer); |
111 } | 106 } |
112 } | 107 } |
113 | 108 |
114 public String toString() | 109 public String toString() |
115 { | 110 { |
116 return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", | 111 return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", |
117 getClass().getSimpleName(), | 112 getClass().getSimpleName(), |
118 _headers.size(),_maxSize,_headerSize, | 113 _headers.size(),_maxSize,_headerSize, |
119 _buffers.size(),_maxSize,_bufferSize, | 114 _buffers.size(),_maxSize,_bufferSize, |
120 _others.size(),_maxSize); | 115 _others.size(),_maxSize); |
121 } | 116 } |
122 } | 117 } |