KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > pool > ByteBufferObjectFactory


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
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 JavaDoc;
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, boolean useDirectByteBuffer) {
30         this.bufferSize = bufferSize;
31         this.useDirectByteBuffer = useDirectByteBuffer;
32     }
33
34     //Creates an instance that can be returned by the pool.
35
public Object JavaDoc makeObject() {
36         if(useDirectByteBuffer)
37             return ByteBuffer.allocateDirect(bufferSize);
38         else
39             return ByteBuffer.allocate(bufferSize);
40     }
41
42     //Uninitialize an instance to be returned to the pool.
43
public void passivateObject(Object JavaDoc obj) {
44         ByteBuffer JavaDoc ch = (ByteBuffer JavaDoc)obj;
45         ch.clear();
46     }
47
48     //Reinitialize an instance to be returned by the pool.
49
public void activateObject(Object JavaDoc obj) {
50     }
51     
52     //Destroys an instance no longer needed by the pool.
53
public void destroyObject(Object JavaDoc obj) {
54         if(obj==null) return;
55         passivateObject(obj);
56         obj = null;
57     }
58
59     //Ensures that the instance is safe to be returned by the pool.
60
public boolean validateObject(Object JavaDoc obj) {
61         if(obj==null)
62             return false;
63         else
64             return true;
65     }
66 }
67
Popular Tags