1 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 [] cache = new Object [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 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 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 |