01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package org.quickserver.util.pool;
16:
17: import java.nio.ByteBuffer;
18: import org.apache.commons.pool.BasePoolableObjectFactory;
19:
20: /**
21: * A factory for creating java.nio.ByteBuffer instances.
22: * @author Akshathkumar Shetty
23: * @since 1.3
24: */
25: public class ByteBufferObjectFactory extends BasePoolableObjectFactory {
26: int bufferSize = -1;
27: boolean useDirectByteBuffer = true;
28:
29: public ByteBufferObjectFactory(int bufferSize,
30: boolean useDirectByteBuffer) {
31: this .bufferSize = bufferSize;
32: this .useDirectByteBuffer = useDirectByteBuffer;
33: }
34:
35: //Creates an instance that can be returned by the pool.
36: public Object makeObject() {
37: if (useDirectByteBuffer)
38: return ByteBuffer.allocateDirect(bufferSize);
39: else
40: return ByteBuffer.allocate(bufferSize);
41: }
42:
43: //Uninitialize an instance to be returned to the pool.
44: public void passivateObject(Object obj) {
45: ByteBuffer ch = (ByteBuffer) obj;
46: ch.clear();
47: }
48:
49: //Reinitialize an instance to be returned by the pool.
50: public void activateObject(Object obj) {
51: }
52:
53: //Destroys an instance no longer needed by the pool.
54: public void destroyObject(Object obj) {
55: if (obj == null)
56: return;
57: passivateObject(obj);
58: obj = null;
59: }
60:
61: //Ensures that the instance is safe to be returned by the pool.
62: public boolean validateObject(Object obj) {
63: if (obj == null)
64: return false;
65: else
66: return true;
67: }
68: }
|