KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > byteable > UnboundedByteBufferPool


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on 16/06/2004
48  *
49  * Manta LTD
50  */

51 package org.mr.core.util.byteable;
52
53 import java.nio.ByteBuffer JavaDoc;
54
55 import org.mr.core.util.SynchronizedQueue;
56
57 /**
58  * UnboundedByteBufferPool is a pool of bytr buffer that when empty allocates new buffers
59  * @author Amir Shevat
60  *
61  */

62 public class UnboundedByteBufferPool implements ByteBufferFactory{
63     
64     
65     
66     private SynchronizedQueue small = new SynchronizedQueue();
67     private SynchronizedQueue medium = new SynchronizedQueue();
68     private SynchronizedQueue large = new SynchronizedQueue();
69     
70     private ByteBufferPoolParameters params;
71
72     /**
73      * Constructor
74      */

75     public UnboundedByteBufferPool(ByteBufferPoolParameters params) {
76         this.params = params;
77         
78         for (int i = 0; i < params.getNumInSmallPool(); i++) {
79             small.enqueue(ByteBuffer.allocate( params.getSmallBufferSize()));
80         }//for
81

82         for (int i = 0; i < params.getNumInMediumPool(); i++) {
83             medium.enqueue(ByteBuffer.allocate( params.getMediumBufferSize()));
84         }//for
85

86         for (int i = 0; i < params.getNumInLargePool(); i++) {
87             large.enqueue(ByteBuffer.allocate( params.getLargeBufferSize()));
88         }//for
89
}//ByteBufferPool
90

91     static int count = 0;
92     public final ByteBuffer JavaDoc getBuffer(int size){
93         if(size > 2000){
94             int y =1;
95             //System.out.print(small.size()+"|");
96
}
97         
98         ByteBuffer JavaDoc result = null;
99         if(size <= params.getSmallBufferSize()){
100             result= (ByteBuffer JavaDoc)small.dequeueNoBlock();//dequeue(); //dequeueNoBlock(); //
101
}else if(size <= params.getMediumBufferSize()){
102             result= (ByteBuffer JavaDoc)medium.dequeueNoBlock();//dequeue(); //dequeueNoBlock();//
103

104         }else if(size <= params.getLargeBufferSize()){
105             result= (ByteBuffer JavaDoc)large.dequeueNoBlock();//dequeue(); //dequeueNoBlock();//
106
}
107         if(result == null){
108             //System.out.println((count++)+"-"+size);
109
result= ByteBuffer.allocate(size);
110         }
111
112         result.limit(size);
113         return result;
114         
115     }//getBuffer
116

117     
118     public final void release(ByteBuffer JavaDoc buffer){
119
120         // we do not need bad buffers here in our pool
121
if(buffer==null)
122             return;
123         
124         buffer.position(0);
125         buffer.limit(buffer.capacity());
126         buffer.mark();
127         int size = buffer.capacity();
128         
129         if(size == params.getSmallBufferSize() && small.size() < params.getNumInSmallPool())
130             small.enqueue(buffer);
131         else if(size == params.getMediumBufferSize() && medium.size() < params.getNumInMediumPool()){
132             medium.enqueue(buffer);
133             
134         }else if(size == params.getLargeBufferSize() && large.size() < params.getNumInLargePool())
135             large.enqueue(buffer);
136     }//release
137

138
139     /* (non-Javadoc)
140      * @see org.mr.core.util.byteable.ByteBufferFactory#getSmallBufferSize()
141      */

142     public int getSmallBufferSize() {
143         
144         return params.getSmallBufferSize();
145     }
146 }
147
148
Popular Tags