Mercurial Hosting > luan
annotate 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 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.util.Queue; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.util.concurrent.ConcurrentLinkedQueue; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.util.concurrent.atomic.AtomicInteger; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
25 public final class PooledBuffers extends AbstractBuffers |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 { |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
27 private final Queue<Buffer> _headers; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
28 private final Queue<Buffer> _buffers; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
29 private final Queue<Buffer> _others; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
30 private final AtomicInteger _size = new AtomicInteger(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
31 private final int _maxSize; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
32 private final boolean _otherHeaders; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
34 public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize,int maxSize) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
35 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
36 super(headerType,headerSize,bufferType,bufferSize); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
37 _headers=new ConcurrentLinkedQueue<Buffer>(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
38 _buffers=new ConcurrentLinkedQueue<Buffer>(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
39 _others=new ConcurrentLinkedQueue<Buffer>(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
40 _otherHeaders=headerType==bufferType; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
41 _maxSize=maxSize; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
42 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
44 public Buffer getHeader() |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
45 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
46 Buffer buffer = _headers.poll(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
47 if (buffer==null) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
48 buffer=newHeader(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
49 else |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
50 _size.decrementAndGet(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
51 return buffer; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
52 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
54 public Buffer getBuffer() |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
55 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
56 Buffer buffer = _buffers.poll(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
57 if (buffer==null) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
58 buffer=newBuffer(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
59 else |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
60 _size.decrementAndGet(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
61 return buffer; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
62 } |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
63 |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
64 /* ------------------------------------------------------------ */ |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
65 public Buffer getBuffer(int size) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
66 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
67 if (_otherHeaders && size==getHeaderSize()) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
68 return getHeader(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
69 if (size==getBufferSize()) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
70 return getBuffer(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
72 // Look for an other buffer |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
73 Buffer buffer = _others.poll(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
75 // consume all other buffers until one of the right size is found |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
76 while (buffer!=null && buffer.capacity()!=size) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
77 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
78 _size.decrementAndGet(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
79 buffer = _others.poll(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
80 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
82 if (buffer==null) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
83 buffer=newBuffer(size); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
84 else |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
85 _size.decrementAndGet(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
86 return buffer; |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
87 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
89 /* ------------------------------------------------------------ */ |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
90 public void returnBuffer(Buffer buffer) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
91 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
92 buffer.clear(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
93 if (buffer.isVolatile() || buffer.isImmutable()) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
94 return; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
95 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
96 if (_size.incrementAndGet() > _maxSize) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
97 _size.decrementAndGet(); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
98 else |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
99 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
100 if (isHeader(buffer)) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
101 _headers.add(buffer); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
102 else if (isBuffer(buffer)) |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
103 _buffers.add(buffer); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
104 else |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
105 _others.add(buffer); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
106 } |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
107 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
108 |
1006
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
109 public String toString() |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
110 { |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
111 return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
112 getClass().getSimpleName(), |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
113 _headers.size(),_maxSize,_headerSize, |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
114 _buffers.size(),_maxSize,_bufferSize, |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
115 _others.size(),_maxSize); |
58a9c4a42292
simplify Buffers code
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
116 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
117 } |