001: package org.mortbay.jetty;
002:
003: import java.util.ArrayList;
004:
005: import org.mortbay.component.AbstractLifeCycle;
006: import org.mortbay.io.Buffer;
007: import org.mortbay.io.Buffers;
008:
009: /* ------------------------------------------------------------ */
010: /** Abstract Buffer pool.
011: * simple unbounded pool of buffers.
012: * @author gregw
013: *
014: */
015: public abstract class AbstractBuffers extends AbstractLifeCycle
016: implements Buffers {
017: private int _headerBufferSize = 8 * 1024;
018: private int _requestBufferSize = 32 * 1024;
019: private int _responseBufferSize = 64 * 1024;
020:
021: private transient ArrayList _headerBuffers;
022: private transient ArrayList _requestBuffers;
023: private transient ArrayList _responseBuffers;
024:
025: /* ------------------------------------------------------------ */
026: /**
027: * @return Returns the headerBufferSize.
028: */
029: public int getHeaderBufferSize() {
030: return _headerBufferSize;
031: }
032:
033: /* ------------------------------------------------------------ */
034: /**
035: * @param headerBufferSize The headerBufferSize to set.
036: */
037: public void setHeaderBufferSize(int headerBufferSize) {
038: _headerBufferSize = headerBufferSize;
039: }
040:
041: /* ------------------------------------------------------------ */
042: /**
043: * @return Returns the requestBufferSize.
044: */
045: public int getRequestBufferSize() {
046: return _requestBufferSize;
047: }
048:
049: /* ------------------------------------------------------------ */
050: /**
051: * @param requestBufferSize The requestBufferSize to set.
052: */
053: public void setRequestBufferSize(int requestBufferSize) {
054: _requestBufferSize = requestBufferSize;
055: }
056:
057: /* ------------------------------------------------------------ */
058: /**
059: * @return Returns the responseBufferSize.
060: */
061: public int getResponseBufferSize() {
062: return _responseBufferSize;
063: }
064:
065: /* ------------------------------------------------------------ */
066: /**
067: * @param responseBufferSize The responseBufferSize to set.
068: */
069: public void setResponseBufferSize(int responseBufferSize) {
070: _responseBufferSize = responseBufferSize;
071: }
072:
073: /* ------------------------------------------------------------ */
074: protected abstract Buffer newBuffer(int size);
075:
076: /* ------------------------------------------------------------ */
077: public Buffer getBuffer(int size) {
078: if (size == _headerBufferSize) {
079: synchronized (_headerBuffers) {
080: if (_headerBuffers.size() == 0)
081: return newBuffer(size);
082: return (Buffer) _headerBuffers.remove(_headerBuffers
083: .size() - 1);
084: }
085: } else if (size == _responseBufferSize) {
086: synchronized (_responseBuffers) {
087: if (_responseBuffers.size() == 0)
088: return newBuffer(size);
089: return (Buffer) _responseBuffers
090: .remove(_responseBuffers.size() - 1);
091: }
092: } else if (size == _requestBufferSize) {
093: synchronized (_requestBuffers) {
094: if (_requestBuffers.size() == 0)
095: return newBuffer(size);
096: return (Buffer) _requestBuffers.remove(_requestBuffers
097: .size() - 1);
098: }
099: }
100:
101: return newBuffer(size);
102: }
103:
104: /* ------------------------------------------------------------ */
105: public void returnBuffer(Buffer buffer) {
106: buffer.clear();
107: if (!buffer.isVolatile() && !buffer.isImmutable()) {
108: int c = buffer.capacity();
109: if (c == _headerBufferSize) {
110: synchronized (_headerBuffers) {
111: _headerBuffers.add(buffer);
112: }
113: } else if (c == _responseBufferSize) {
114: synchronized (_responseBuffers) {
115: _responseBuffers.add(buffer);
116: }
117: } else if (c == _requestBufferSize) {
118: synchronized (_requestBuffers) {
119: _requestBuffers.add(buffer);
120: }
121: }
122: }
123: }
124:
125: /* ------------------------------------------------------------ */
126: protected void doStart() throws Exception {
127: super .doStart();
128:
129: if (_headerBuffers != null)
130: _headerBuffers.clear();
131: else
132: _headerBuffers = new ArrayList();
133: if (_requestBuffers != null)
134: _requestBuffers.clear();
135: else
136: _requestBuffers = new ArrayList();
137: if (_responseBuffers != null)
138: _responseBuffers.clear();
139: else
140: _responseBuffers = new ArrayList();
141: }
142:
143: }
|