01: /* jcifs smb client library in Java
02: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * 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: class BufferCache {
24:
25: private static final int MAX_BUFFERS = Config.getInt(
26: "jcifs.smb.maxBuffers", 16);
27:
28: private static Object[] cache = new Object[MAX_BUFFERS];
29: private static int numBuffers = 0;
30: private static int freeBuffers = 0;
31:
32: static byte[] getBuffer() {
33: byte[] buf;
34:
35: synchronized (cache) {
36: while (freeBuffers == 0 && numBuffers == MAX_BUFFERS) {
37: try {
38: cache.wait();
39: } catch (InterruptedException ie) {
40: return null;
41: }
42: }
43:
44: if (freeBuffers > 0) {
45: for (int i = 0; i < MAX_BUFFERS; i++) {
46: if (cache[i] != null) {
47: buf = (byte[]) cache[i];
48: cache[i] = null;
49: freeBuffers--;
50: return buf;
51: }
52: }
53: }
54:
55: buf = new byte[SmbComTransaction.TRANSACTION_BUF_SIZE];
56: numBuffers++;
57: }
58:
59: return buf;
60: }
61:
62: static void releaseBuffer(byte[] buf) {
63: synchronized (cache) {
64: for (int i = 0; i < MAX_BUFFERS; i++) {
65: if (cache[i] == null) {
66: cache[i] = buf;
67: freeBuffers++;
68: cache.notify();
69: return;
70: }
71: }
72: }
73: }
74: }
|