KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > 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 com.knowgate.jcifs.smb;
20
21 import com.knowgate.jcifs.Config;
22
23 class BufferCache {
24
25     private static final int MAX_BUFFERS = Config.getInt( "jcifs.smb.maxBuffers", 16 );
26
27     private static Object JavaDoc[] cache = new Object JavaDoc[MAX_BUFFERS];
28     private static int numBuffers = 0;
29     private static int freeBuffers = 0;
30
31     static byte[] getBuffer() {
32         byte[] buf;
33
34         synchronized( cache ) {
35             while( freeBuffers == 0 && numBuffers == MAX_BUFFERS ) {
36                 try {
37                     cache.wait();
38                 } catch( InterruptedException JavaDoc ie ) {
39                     return null;
40                 }
41             }
42
43             if( freeBuffers > 0 ) {
44                 for( int i = 0; i < MAX_BUFFERS; i++ ) {
45                     if( cache[i] != null ) {
46                         buf = (byte[])cache[i];
47                         cache[i] = null;
48                         freeBuffers--;
49                         return buf;
50                     }
51                 }
52             }
53
54             buf = new byte[SmbComTransaction.TRANSACTION_BUF_SIZE];
55             numBuffers++;
56         }
57
58         return buf;
59     }
60     static void releaseBuffer( byte[] buf ) {
61         synchronized( cache ) {
62             for( int i = 0; i < MAX_BUFFERS; i++ ) {
63                 if( cache[i] == null ) {
64                     cache[i] = buf;
65                     freeBuffers++;
66                     cache.notify();
67                     return;
68                 }
69             }
70         }
71     }
72 }
73
Popular Tags