KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > io > MemBuffer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.io;
37
38 import org.columba.ristretto.concurrency.Mutex;
39
40 /**
41  * A flexible memory buffer.
42  *
43  * @author tstich
44  */

45 public class MemBuffer {
46     private final static int INCREMENT = 1024;
47     
48     private byte[] buffer;
49     
50     private int len;
51     
52     private Mutex mutex;
53     
54     /**
55      * Constructs the memory buffer.
56      */

57     public MemBuffer() {
58         this(INCREMENT);
59     }
60     
61     /**
62      * Constructs the memory buffer with
63      * the given initial size.
64      * @param size the initial size
65      */

66     public MemBuffer(int size) {
67         buffer = new byte[size];
68         
69         len = 0;
70         
71         mutex = new Mutex();
72     }
73     
74     /**
75      * Append the value to the buffer.
76      *
77      * @param value
78      */

79     public void append(int value) {
80         mutex.lock();
81         if( len == buffer.length) growBuffer(buffer.length + INCREMENT);
82         
83         buffer[len++] = (byte) value;
84         mutex.release();
85     }
86     
87     /**
88      * Append the byte array to the end of the buffer.
89      *
90      * @param array
91      * @param offset
92      * @param length
93      */

94     public void append(byte[] array, int offset, int length) {
95         mutex.lock();
96         int available = buffer.length - len;
97         if( length > available) growBuffer(buffer.length + ( length - available > INCREMENT ? length : INCREMENT));
98         System.arraycopy(array, offset, buffer, len, length);
99         len += length;
100         mutex.release();
101     }
102     
103     /**
104      * Append the byte array to the end of the buffer.
105      *
106      * @param array
107      */

108     public void append(byte[] array) {
109         append(array, 0, array.length);
110     }
111     
112     /**
113      * Get the value at the given position.
114      *
115      * @param pos
116      * @return the value at the position
117      */

118     public int get(int pos) {
119         if( pos > len ) throw new ArrayIndexOutOfBoundsException JavaDoc(pos);
120         
121         return buffer[pos];
122     }
123     
124     /**
125      * Fill the array with data from the buffer starting
126      * at position pos.
127      *
128      * @param pos
129      * @param array
130      * @return the number of bytes copied to the array.
131      */

132     public int get(int pos, byte[] array) {
133         return get(pos, array, 0, array.length);
134     }
135     
136     /**
137      * Fill the array with data from the buffer starting
138      * at position pos.
139      *
140      * @param pos
141      * @param array
142      * @param offset
143      * @param length
144      * @return the number of bytes copied to the array.
145      */

146     public int get(int pos, byte[] array, int offset, int length) {
147         mutex.lock();
148         int result = length > (len-pos) ? (len-pos) : length;
149         if( result < 0 ) {
150             mutex.release();
151             throw new ArrayIndexOutOfBoundsException JavaDoc(pos);
152         }
153         
154         System.arraycopy(buffer, pos, array, offset, result);
155         mutex.release();
156         
157         return result;
158     }
159
160     /**
161      *
162      *
163      * @return returns the size of the buffer.
164      */

165     public int size() {
166         mutex.lock();
167         int result = len;
168         mutex.release();
169         return result;
170     }
171     
172        /**
173         * Grows the buffer to fit the given size.
174         *
175      * @param newSize
176      */

177     private void growBuffer(int newSize) {
178         byte[] newBuffer = new byte[newSize];
179         System.arraycopy(buffer,0,newBuffer,0,buffer.length);
180         buffer = newBuffer;
181     }
182     
183
184 }
185
Popular Tags