01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.catalina.tribes.io;
18:
19: import java.util.LinkedList;
20:
21: /**
22: *
23: * @author Filip Hanik
24: * @version 1.0
25: */
26: class BufferPool14Impl implements BufferPool.BufferPoolAPI {
27: protected int maxSize;
28: protected int size = 0;
29: protected LinkedList queue = new LinkedList();
30:
31: public void setMaxSize(int bytes) {
32: this .maxSize = bytes;
33: }
34:
35: public synchronized int addAndGet(int val) {
36: size = size + (val);
37: return size;
38: }
39:
40: public synchronized XByteBuffer getBuffer(int minSize,
41: boolean discard) {
42: XByteBuffer buffer = (XByteBuffer) (queue.size() > 0 ? queue
43: .remove(0) : null);
44: if (buffer != null)
45: addAndGet(-buffer.getCapacity());
46: if (buffer == null)
47: buffer = new XByteBuffer(minSize, discard);
48: else if (buffer.getCapacity() <= minSize)
49: buffer.expand(minSize);
50: buffer.setDiscard(discard);
51: buffer.reset();
52: return buffer;
53: }
54:
55: public synchronized void returnBuffer(XByteBuffer buffer) {
56: if ((size + buffer.getCapacity()) <= maxSize) {
57: addAndGet(buffer.getCapacity());
58: queue.add(buffer);
59: }
60: }
61:
62: public synchronized void clear() {
63: queue.clear();
64: size = 0;
65: }
66:
67: public int getMaxSize() {
68: return maxSize;
69: }
70:
71: }
|