KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > network > ByteBuffer4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation.network;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.foundation.*;
27
28 /**
29  * Transport buffer for C/S mode to simulate a
30  * socket connection in memory.
31  */

32 class ByteBuffer4 {
33
34     private final int DISCARD_BUFFER_SIZE = 500;
35     protected byte[] i_cache;
36     private boolean i_closed = false;
37     protected int i_readOffset;
38     protected int i_timeout;
39     protected int i_writeOffset;
40     protected final Lock4 i_lock = new Lock4();
41
42     public ByteBuffer4(int timeout) {
43         i_timeout = timeout;
44     }
45
46     protected int available() {
47         return i_writeOffset - i_readOffset;
48     }
49
50     protected void checkDiscardCache() {
51         if (i_readOffset == i_writeOffset && i_cache.length > DISCARD_BUFFER_SIZE) {
52             i_cache = null;
53             i_readOffset = 0;
54             i_writeOffset = 0;
55         }
56     }
57
58     void close() {
59         i_closed = true;
60     }
61
62     protected void makefit(int length) {
63         if (i_cache == null) {
64             i_cache = new byte[length];
65         } else {
66             // doesn't fit
67
if (i_writeOffset + length > i_cache.length) {
68                 // move, if possible
69
if (i_writeOffset + length - i_readOffset <= i_cache.length) {
70                     byte[] temp = new byte[i_cache.length];
71                     System.arraycopy(i_cache, i_readOffset, temp, 0, i_cache.length - i_readOffset);
72                     i_cache = temp;
73                     i_writeOffset -= i_readOffset;
74                     i_readOffset = 0;
75
76                     // else append
77
} else {
78                     byte[] temp = new byte[i_writeOffset + length];
79                     System.arraycopy(i_cache, 0, temp, 0, i_cache.length);
80                     i_cache = temp;
81                 }
82             }
83         }
84     }
85
86     public int read() throws IOException {
87         try{
88             Integer JavaDoc ret = (Integer JavaDoc)i_lock.run(new Closure4() {
89                 public Object JavaDoc run() throws Exception JavaDoc {
90                     waitForAvailable();
91                     int retVal = i_cache[i_readOffset++];
92                     checkDiscardCache();
93                     return new Integer JavaDoc(retVal);
94                 }
95             
96             });
97             return ret.intValue();
98         }catch(IOException iex){
99             throw iex;
100         }catch(Exception JavaDoc bex){
101             // TODO: lots is caught here, be more exact
102
}
103         return -1;
104     }
105
106     public int read(final byte[] a_bytes, final int a_offset, final int a_length) throws IOException {
107         try{
108             Integer JavaDoc ret = (Integer JavaDoc)i_lock.run(new Closure4() {
109                 public Object JavaDoc run() throws Exception JavaDoc {
110                     waitForAvailable();
111                     int avail = available();
112                     int length = a_length;
113                     if (avail < a_length) {
114                         length = avail;
115                     }
116                     System.arraycopy(i_cache, i_readOffset, a_bytes, a_offset, length);
117                     i_readOffset += length;
118                     checkDiscardCache();
119                     return new Integer JavaDoc(avail);
120                 }
121             
122             });
123             return ret.intValue();
124         }catch(IOException iex){
125             throw iex;
126         }catch(Exception JavaDoc bex){
127             // TODO: lots is caught here, be more exact
128
}
129         return -1;
130     }
131
132     public void setTimeout(int timeout) {
133         i_timeout = timeout;
134     }
135
136     protected void waitForAvailable() throws IOException {
137         while (available() == 0) {
138             try {
139                 i_lock.snooze(i_timeout);
140             } catch (Exception JavaDoc e) {
141                 throw new IOException(Messages.get(55));
142             }
143         }
144         if (i_closed) {
145             throw new IOException(Messages.get(35));
146         }
147
148     }
149
150     public void write(byte[] bytes) {
151         write(bytes, 0, bytes.length);
152     }
153     
154     public void write(final byte[] bytes, final int off, final int len) {
155         try {
156             i_lock.run(new Closure4() {
157                 public Object JavaDoc run() throws Exception JavaDoc {
158                     makefit(len);
159                     System.arraycopy(bytes, off, i_cache, i_writeOffset, len);
160                     i_writeOffset += len;
161                     i_lock.awake();
162                     return null;
163                 }
164             });
165         } catch (Exception JavaDoc e) {
166             
167             // TODO: delegate up
168

169         }
170     }
171
172     public void write(final int i) {
173         try {
174             i_lock.run(new Closure4() {
175                 public Object JavaDoc run() throws Exception JavaDoc {
176                     makefit(1);
177                     i_cache[i_writeOffset++] = (byte) i;
178                     i_lock.awake();
179                     return null;
180                 }
181             });
182         } catch (Exception JavaDoc e) {
183             
184             // TODO: delegate up
185

186         }
187     }
188 }
189
Popular Tags