KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > uil2 > msgs > DynCircularBuffer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.il.uil2.msgs;
23
24 /** A circular byte[] buffer capable of expanding its capacity
25  *
26  * @author Scott.Stark@jboss.org
27  */

28 public class DynCircularBuffer
29 {
30    byte[] buffer;
31    int pos;
32    int end;
33    boolean isFull;
34
35    /** Creates a new instance of DynCircularBuffer */
36    public DynCircularBuffer()
37    {
38       this(1024);
39    }
40    public DynCircularBuffer(int capacity)
41    {
42       buffer = new byte[capacity];
43       pos = 0;
44       end = 0;
45    }
46
47    public synchronized void clear()
48    {
49       pos = 0;
50       end = 0;
51       isFull = false;
52    }
53
54    public synchronized void fill(byte[] data)
55    {
56       fill(data, data.length);
57    }
58    public synchronized void fill(byte[] data, int length)
59    {
60       int freeSpace = getFreeSpace();
61       int currentLength = getSize();
62       if( freeSpace < length )
63       {
64          int newSize = buffer.length + length - freeSpace;
65          byte[] tmp = new byte[newSize];
66          for(int i = 0; i < currentLength; i ++)
67          {
68             tmp[i] = buffer[pos];
69             pos ++;
70             if( pos == buffer.length )
71                pos = 0;
72          }
73          buffer = tmp;
74          pos = 0;
75          end = currentLength;
76       }
77       // Copy the data in
78
for(int i = 0; i < length; i ++)
79       {
80          buffer[end] = data[i];
81          end ++;
82          if( end == buffer.length )
83             end = 0;
84       }
85       if( end == pos )
86          isFull = true;
87    }
88
89    public synchronized int get()
90    {
91       if( pos == end && isFull == false )
92          return -1;
93
94       int b = buffer[pos] & 0xff;
95       pos ++;
96       if( pos == buffer.length )
97          pos = 0;
98       isFull = false;
99       return b;
100    }
101
102    public synchronized int get(byte b[], int off, int length)
103    {
104       if( pos == end && isFull == false )
105          return -1;
106
107       int getSize = Math.min(getSize(), length);
108       for(int i = 0; i < getSize; i ++)
109       {
110          b[off + i] = buffer[pos];
111          pos ++;
112          if( pos == buffer.length )
113             pos = 0;
114       }
115       isFull = isFull ? getSize == 0 : false;
116       return getSize;
117    }
118
119    /** Get the amount of used space in the buffer
120     */

121    public synchronized int getSize()
122    {
123       int length = buffer.length - getFreeSpace();
124       return length;
125    }
126    /** Get the amount of free space left in the buffer
127     */

128    public synchronized int getFreeSpace()
129    {
130       int available = 0;
131       if( pos == end )
132       {
133          if( isFull == true )
134             available = 0;
135          else
136             available = buffer.length;
137       }
138       else if( pos < end )
139          available = buffer.length - end + pos;
140       else
141          available = pos - end;
142       return available;
143    }
144
145    /** Unit test driver for the buffer
146     */

147    public static void main(String JavaDoc[] args) throws Exception JavaDoc
148    {
149       DynCircularBuffer dcb = new DynCircularBuffer(16);
150       if( dcb.getFreeSpace() != 16 )
151          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 16");
152       if( dcb.getSize() != 0 )
153          throw new IllegalStateException JavaDoc("dcb.getSize() != 0");
154       byte[] tst = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
155       dcb.fill(tst);
156       if( dcb.getFreeSpace() != 1 )
157          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 1, "+dcb.getFreeSpace());
158       if( dcb.getSize() != 15 )
159          throw new IllegalStateException JavaDoc("dcb.getSize() != 15");
160
161       byte[] tst2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
162       dcb.clear();
163       dcb.fill(tst2);
164       if( dcb.getFreeSpace() != 0 )
165          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 0, "+dcb.getFreeSpace());
166       if( dcb.getSize() != 16 )
167          throw new IllegalStateException JavaDoc("dcb.getSize() != 16");
168
169       dcb.clear();
170       dcb.fill(tst);
171       dcb.fill(tst2);
172       if( dcb.getFreeSpace() != 0 )
173          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 0, "+dcb.getFreeSpace());
174       if( dcb.getSize() != 31 )
175          throw new IllegalStateException JavaDoc("dcb.getSize() != 31, "+dcb.getSize());
176
177       for(int i = 0; i < tst.length; i ++)
178       {
179          int b = dcb.get();
180          if( b != tst[i] )
181             throw new IllegalStateException JavaDoc("tst["+i+","+tst[i]+"] != "+b);
182       }
183       for(int i = 0; i < tst2.length; i ++)
184       {
185          int b = dcb.get();
186          if( b != tst2[i] )
187             throw new IllegalStateException JavaDoc("tst2["+i+","+tst2[i]+"] != "+b);
188       }
189       if( dcb.get() != -1 )
190          throw new IllegalStateException JavaDoc("dcb.get() != -1");
191
192       dcb.fill(tst);
193       byte[] out = new byte[128];
194       int bytes = dcb.get(out, 0, 128);
195       if( bytes != 15 )
196          throw new IllegalStateException JavaDoc("dcb.get(out, 0, 128) != 15, "+bytes);
197       if( dcb.getFreeSpace() != 31 )
198          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 31, "+dcb.getFreeSpace());
199       if( dcb.getSize() != 0 )
200          throw new IllegalStateException JavaDoc("dcb.getSize() != 0, "+dcb.getSize());
201
202       dcb.clear();
203       for(int n = 0; n < 32; n ++)
204       {
205          dcb.fill(tst);
206          if( dcb.getFreeSpace() != 16 )
207             throw new IllegalStateException JavaDoc(n+", dcb.getFreeSpace() != 16, "+dcb.getFreeSpace());
208          if( dcb.getSize() != 15 )
209             throw new IllegalStateException JavaDoc(n+"dcb.getSize() != 15");
210          for(int i = 0; i < tst.length; i ++)
211          {
212             int b = dcb.get();
213             if( b != tst[i] )
214                throw new IllegalStateException JavaDoc(n+"tst["+i+","+tst[i]+"] != "+b);
215          }
216          if( dcb.get() != -1 )
217             throw new IllegalStateException JavaDoc(n+"dcb.get() != -1");
218          if( dcb.getFreeSpace() != 31 )
219             throw new IllegalStateException JavaDoc(n+", dcb.getFreeSpace() != 31, "+dcb.getFreeSpace());
220       }
221
222       byte[] tst3 = new byte[tst.length + tst2.length];
223       System.arraycopy(tst, 0, tst3, 0, tst.length);
224       System.arraycopy(tst2, 0, tst3, tst.length, tst2.length);
225       dcb.clear();
226       dcb.fill(tst3);
227       if( dcb.getFreeSpace() != 0 )
228          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 0, "+dcb.getFreeSpace());
229       if( dcb.getSize() != 31 )
230          throw new IllegalStateException JavaDoc("dcb.getSize() != 31, "+dcb.getSize());
231       dcb.fill(tst);
232       if( dcb.getFreeSpace() != 0 )
233          throw new IllegalStateException JavaDoc("dcb.getFreeSpace() != 0, "+dcb.getFreeSpace());
234       if( dcb.getSize() != 46 )
235          throw new IllegalStateException JavaDoc("dcb.getSize() != 46, "+dcb.getSize());
236       for(int i = 0; i < tst.length; i ++)
237       {
238          int b = dcb.get();
239          if( b != tst[i] )
240             throw new IllegalStateException JavaDoc("tst["+i+","+tst[i]+"] != "+b);
241       }
242       for(int i = 0; i < tst2.length; i ++)
243       {
244          int b = dcb.get();
245          if( b != tst2[i] )
246             throw new IllegalStateException JavaDoc("tst2["+i+","+tst2[i]+"] != "+b);
247       }
248       for(int i = 0; i < tst.length; i ++)
249       {
250          int b = dcb.get();
251          if( b != tst[i] )
252             throw new IllegalStateException JavaDoc("tst["+i+","+tst[i]+"] != "+b);
253       }
254
255       System.out.println("Done");
256    }
257 }
258
Popular Tags