KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > ByteArrayPool


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 //
22
// Modified for use in SSL-Explorer by 3SP. These portions licensed as above,
23
// everything else licensed as below.
24
//
25
//
26

27 // ========================================================================
28
// $Id$
29
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
30
// ------------------------------------------------------------------------
31
// Licensed under the Apache License, Version 2.0 (the "License");
32
// you may not use this file except in compliance with the License.
33
// You may obtain a copy of the License at
34
// http://www.apache.org/licenses/LICENSE-2.0
35
// Unless required by applicable law or agreed to in writing, software
36
// distributed under the License is distributed on an "AS IS" BASIS,
37
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38
// See the License for the specific language governing permissions and
39
// limitations under the License.
40
// ========================================================================
41

42 package com.sslexplorer.core;
43
44
45 /* ------------------------------------------------------------ */
46 /** Byte Array Pool
47  * Simple pool for recycling byte arrays of a fixed size.
48  *
49  * @author Greg Wilkins (gregw)
50  */

51 public class ByteArrayPool
52 {
53     public static final int __POOL_SIZE=
54         Integer.getInteger("org.mortbay.util.ByteArrayPool.pool_size",8).intValue();
55     
56     public static final ThreadLocal JavaDoc __pools=new BAThreadLocal();
57     public static int __slot;
58     
59     /* ------------------------------------------------------------ */
60     /** Get a byte array from the pool of known size.
61      * @param size Size of the byte array.
62      * @return Byte array of known size.
63      */

64     public static byte[] getByteArray(int size)
65     {
66         byte[][] pool = (byte[][])__pools.get();
67         boolean full=true;
68         for (int i=pool.length;i-->0;)
69         {
70             if (pool[i]!=null && pool[i].length==size)
71             {
72                 byte[]b = pool[i];
73                 pool[i]=null;
74                 return b;
75             }
76             else
77                 full=false;
78         }
79
80         if (full)
81             for (int i=pool.length;i-->0;)
82                 pool[i]=null;
83         
84         return new byte[size];
85     }
86
87     /* ------------------------------------------------------------ */
88     public static byte[] getByteArrayAtLeast(int minSize)
89     {
90         byte[][] pool = (byte[][])__pools.get();
91         for (int i=pool.length;i-->0;)
92         {
93             if (pool[i]!=null && pool[i].length>=minSize)
94             {
95                 byte[]b = pool[i];
96                 pool[i]=null;
97                 return b;
98             }
99         }
100         
101         return new byte[minSize];
102     }
103
104
105     /* ------------------------------------------------------------ */
106     public static void returnByteArray(final byte[] b)
107     {
108         if (b==null)
109             return;
110         
111         byte[][] pool = (byte[][])__pools.get();
112         for (int i=pool.length;i-->0;)
113         {
114             if (pool[i]==null)
115             {
116                 pool[i]=b;
117                 return;
118             }
119         }
120
121         // slot.
122
int s = __slot++;
123         if (s<0)s=-s;
124         pool[s%pool.length]=b;
125     }
126
127     
128     /* ------------------------------------------------------------ */
129     /* ------------------------------------------------------------ */
130     private static final class BAThreadLocal extends ThreadLocal JavaDoc
131     {
132         protected Object JavaDoc initialValue()
133             {
134                 return new byte[__POOL_SIZE][];
135             }
136     }
137 }
138
Popular Tags