KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > BufferCache


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.smb;
20
21 import jcifs.Config;
22
23 public class BufferCache {
24
25     private static final int MAX_BUFFERS = Config.getInt( "jcifs.smb.maxBuffers", 16 );
26
27     static Object JavaDoc[] cache = new Object JavaDoc[MAX_BUFFERS];
28     private static int numBuffers = 0;
29     private static int freeBuffers = 0;
30
31     private static byte[] getBuffer0() {
32         byte[] buf;
33
34         if (freeBuffers > 0) {
35             for (int i = 0; i < MAX_BUFFERS; i++) {
36                 if( cache[i] != null ) {
37                     buf = (byte[])cache[i];
38                     cache[i] = null;
39                     freeBuffers--;
40                     return buf;
41                 }
42             }
43         }
44
45         buf = new byte[SmbComTransaction.TRANSACTION_BUF_SIZE];
46         numBuffers++;
47
48         return buf;
49     }
50
51     static void getBuffers( SmbComTransaction req, SmbComTransactionResponse rsp ) {
52         synchronized( cache ) {
53             try {
54                 while ((freeBuffers + (MAX_BUFFERS - numBuffers)) < 2) {
55                     cache.wait();
56                 }
57                 req.txn_buf = getBuffer0();
58                 rsp.txn_buf = getBuffer0();
59             } catch( InterruptedException JavaDoc ie ) {
60                 ie.printStackTrace();
61             }
62         }
63     }
64     static public byte[] getBuffer() {
65         synchronized( cache ) {
66             while ((freeBuffers + (MAX_BUFFERS - numBuffers)) < 1) {
67                 try {
68                     cache.wait();
69                 } catch(InterruptedException JavaDoc ie) {
70                 }
71             }
72             return getBuffer0();
73         }
74     }
75     static public void releaseBuffer( byte[] buf ) {
76         synchronized( cache ) {
77             for (int i = 0; i < MAX_BUFFERS; i++) {
78                 if (cache[i] == null) {
79                     cache[i] = buf;
80                     freeBuffers++;
81                     cache.notify();
82                     return;
83                 }
84             }
85         }
86     }
87 }
88
Popular Tags