KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > ByteArrayPool


1 // ========================================================================
2
// $Id: ByteArrayPool.java,v 1.9 2004/05/09 20:32:49 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18
19 /* ------------------------------------------------------------ */
20 /** Byte Array Pool
21  * Simple pool for recycling byte arrays of a fixed size.
22  *
23  * @version $Id: ByteArrayPool.java,v 1.9 2004/05/09 20:32:49 gregwilkins Exp $
24  * @author Greg Wilkins (gregw)
25  */

26 public class ByteArrayPool
27 {
28     public static final int __POOL_SIZE=
29         Integer.getInteger("org.mortbay.util.ByteArrayPool.pool_size",8).intValue();
30     
31     public static final ThreadLocal JavaDoc __pools=new BAThreadLocal();
32     public static int __slot;
33     
34     /* ------------------------------------------------------------ */
35     /** Get a byte array from the pool of known size.
36      * @param size Size of the byte array.
37      * @return Byte array of known size.
38      */

39     public static byte[] getByteArray(int size)
40     {
41         byte[][] pool = (byte[][])__pools.get();
42         boolean full=true;
43         for (int i=pool.length;i-->0;)
44         {
45             if (pool[i]!=null && pool[i].length==size)
46             {
47                 byte[]b = pool[i];
48                 pool[i]=null;
49                 return b;
50             }
51             else
52                 full=false;
53         }
54
55         if (full)
56             for (int i=pool.length;i-->0;)
57                 pool[i]=null;
58         
59         return new byte[size];
60     }
61
62     /* ------------------------------------------------------------ */
63     public static byte[] getByteArrayAtLeast(int minSize)
64     {
65         byte[][] pool = (byte[][])__pools.get();
66         for (int i=pool.length;i-->0;)
67         {
68             if (pool[i]!=null && pool[i].length>=minSize)
69             {
70                 byte[]b = pool[i];
71                 pool[i]=null;
72                 return b;
73             }
74         }
75         
76         return new byte[minSize];
77     }
78
79
80     /* ------------------------------------------------------------ */
81     public static void returnByteArray(final byte[] b)
82     {
83         if (b==null)
84             return;
85         
86         byte[][] pool = (byte[][])__pools.get();
87         for (int i=pool.length;i-->0;)
88         {
89             if (pool[i]==null)
90             {
91                 pool[i]=b;
92                 return;
93             }
94         }
95
96         // slot.
97
int s = __slot++;
98         if (s<0)s=-s;
99         pool[s%pool.length]=b;
100     }
101
102     
103     /* ------------------------------------------------------------ */
104     /* ------------------------------------------------------------ */
105     private static final class BAThreadLocal extends ThreadLocal JavaDoc
106     {
107         protected Object JavaDoc initialValue()
108             {
109                 return new byte[__POOL_SIZE][];
110             }
111     }
112 }
113
Popular Tags