KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > ByteArray


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.utils;
17
18 import org.apache.axis.AxisEngine;
19 import org.apache.axis.AxisProperties;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 /**
33  * Class ByteArray
34  */

35 public class ByteArray extends OutputStream JavaDoc {
36
37     protected static double DEFAULT_CACHE_INCREMENT = 2.5;
38     protected static int DEFAULT_RESIDENT_SIZE = 512 * 1024 * 1024; // 512 MB
39
protected static boolean DEFAULT_ENABLE_BACKING_STORE = true;
40     protected static int WORKING_BUFFER_SIZE = 8192;
41
42     protected org.apache.axis.utils.ByteArrayOutputStream cache = null;
43
44     protected int max_size = 0;
45     protected File JavaDoc bs_handle = null;
46     protected OutputStream JavaDoc bs_stream = null;
47     protected long count = 0;
48     protected boolean enableBackingStore = DEFAULT_ENABLE_BACKING_STORE;
49
50     public boolean isEnableBackingStore() {
51         return enableBackingStore;
52     }
53
54     public void setEnableBackingStore(boolean enableBackingStore) {
55         this.enableBackingStore = enableBackingStore;
56     }
57
58     public static boolean isDEFAULT_ENABLE_BACKING_STORE() {
59         return DEFAULT_ENABLE_BACKING_STORE;
60     }
61
62     public static void setDEFAULT_ENABLE_BACKING_STORE(
63             boolean DEFAULT_ENABLE_BACKING_STORE) {
64         ByteArray.DEFAULT_ENABLE_BACKING_STORE = DEFAULT_ENABLE_BACKING_STORE;
65     }
66
67     public static int getDEFAULT_RESIDENT_SIZE() {
68         return DEFAULT_RESIDENT_SIZE;
69     }
70
71     public static void setDEFAULT_RESIDENT_SIZE(int DEFAULT_RESIDENT_SIZE) {
72         ByteArray.DEFAULT_RESIDENT_SIZE = DEFAULT_RESIDENT_SIZE;
73     }
74
75     public static double getDEFAULT_CACHE_INCREMENT() {
76         return DEFAULT_CACHE_INCREMENT;
77     }
78
79     public static void setDEFAULT_CACHE_INCREMENT(
80             double DEFAULT_CACHE_INCREMENT) {
81         ByteArray.DEFAULT_CACHE_INCREMENT = DEFAULT_CACHE_INCREMENT;
82     }
83
84     static {
85         String JavaDoc value;
86         value = AxisProperties.getProperty(
87                 AxisEngine.PROP_BYTE_BUFFER_CACHE_INCREMENT,
88                 "" + DEFAULT_CACHE_INCREMENT);
89         DEFAULT_CACHE_INCREMENT = Double.parseDouble(value);
90         value = AxisProperties.getProperty(
91                 AxisEngine.PROP_BYTE_BUFFER_RESIDENT_MAX_SIZE,
92                 "" + DEFAULT_RESIDENT_SIZE);
93         DEFAULT_RESIDENT_SIZE = Integer.parseInt(value);
94         value = AxisProperties.getProperty(
95                 AxisEngine.PROP_BYTE_BUFFER_WORK_BUFFER_SIZE,
96                 "" + WORKING_BUFFER_SIZE);
97         WORKING_BUFFER_SIZE = Integer.parseInt(value);
98         value =
99                 AxisProperties.getProperty(AxisEngine.PROP_BYTE_BUFFER_BACKING,
100                         "" + DEFAULT_ENABLE_BACKING_STORE);
101         if (value.equalsIgnoreCase("true") ||
102                 value.equals("1") ||
103                 value.equalsIgnoreCase("yes")) {
104             DEFAULT_ENABLE_BACKING_STORE = true;
105         } else {
106             DEFAULT_ENABLE_BACKING_STORE = false;
107         }
108     }
109
110     /**
111      * Constructor ByteArray
112      */

113     public ByteArray() {
114         this(DEFAULT_RESIDENT_SIZE);
115     }
116
117     /**
118      * Constructor ByteArray
119      *
120      * @param max_resident_size
121      */

122     public ByteArray(int max_resident_size) {
123         this(0, max_resident_size);
124     }
125
126     /**
127      * Constructor ByteArray
128      *
129      * @param probable_size
130      * @param max_resident_size
131      */

132     public ByteArray(int probable_size, int max_resident_size) {
133         if (probable_size > max_resident_size) {
134             probable_size = 0;
135         }
136         if (probable_size < WORKING_BUFFER_SIZE) {
137             probable_size = WORKING_BUFFER_SIZE;
138         }
139         cache = new org.apache.axis.utils.ByteArrayOutputStream(probable_size);
140         max_size = max_resident_size;
141     }
142
143     /**
144      * Method write
145      *
146      * @param bytes
147      * @throws IOException
148      */

149     public void write(byte bytes[]) throws IOException JavaDoc {
150         write(bytes, 0, bytes.length);
151     }
152
153     /**
154      * Method write
155      *
156      * @param bytes
157      * @param start
158      * @param length
159      * @throws IOException
160      */

161     public void write(byte bytes[], int start, int length) throws IOException JavaDoc {
162         count += length;
163         if (cache != null) {
164             increaseCapacity(length);
165         }
166         if (cache != null) {
167             cache.write(bytes, start, length);
168         } else if (bs_stream != null) {
169             bs_stream.write(bytes, start, length);
170         } else {
171             throw new IOException JavaDoc("ByteArray does not have a backing store!");
172         }
173     }
174
175     /**
176      * Method write
177      *
178      * @param b
179      * @throws IOException
180      */

181     public void write(int b) throws IOException JavaDoc {
182         count += 1;
183         if (cache != null) {
184             increaseCapacity(1);
185         }
186         if (cache != null) {
187             cache.write(b);
188         } else if (bs_stream != null) {
189             bs_stream.write(b);
190         } else {
191             throw new IOException JavaDoc("ByteArray does not have a backing store!");
192         }
193     }
194
195     /**
196      * Method close
197      *
198      * @throws IOException
199      */

200     public void close() throws IOException JavaDoc {
201         if (bs_stream != null) {
202             bs_stream.close();
203             bs_stream = null;
204         }
205     }
206
207     /**
208      * Method size
209      *
210      * @return
211      */

212     public long size() {
213         return count;
214     }
215
216     /**
217      * Method flush
218      *
219      * @throws IOException
220      */

221     public void flush() throws IOException JavaDoc {
222         if (bs_stream != null) {
223             bs_stream.flush();
224         }
225     }
226
227     /**
228      * Method increaseCapacity
229      *
230      * @param count
231      * @throws IOException
232      */

233     protected void increaseCapacity(int count) throws IOException JavaDoc {
234         if (cache == null) {
235             return;
236         }
237         if (count + cache.size() <= max_size) {
238             return;
239         } else if (enableBackingStore) {
240             switchToBackingStore();
241         } else {
242             throw new IOException JavaDoc("ByteArray can not increase capacity by " +
243                     count +
244                     " due to max size limit of " + max_size);
245         }
246     }
247
248     /**
249      * Method discardBuffer
250      */

251     public synchronized void discardBuffer() {
252         cache = null;
253         if (bs_stream != null) {
254             try {
255                 bs_stream.close();
256             } catch (IOException JavaDoc e) {
257                 // just ignore it...
258
}
259             bs_stream = null;
260         }
261         discardBackingStore();
262     }
263
264     /**
265      * Method makeInputStream
266      *
267      * @return
268      * @throws IOException
269      * @throws FileNotFoundException
270      */

271     protected InputStream JavaDoc makeInputStream()
272             throws IOException JavaDoc, FileNotFoundException JavaDoc {
273         close();
274         if (cache != null) {
275             return new ByteArrayInputStream JavaDoc(cache.toByteArray());
276         } else if (bs_handle != null) {
277             return createBackingStoreInputStream();
278         } else {
279             return null;
280         }
281     }
282
283     /**
284      * Method finalize
285      */

286     protected void finalize() {
287         discardBuffer();
288     }
289
290     /**
291      * Method switchToBackingStore
292      *
293      * @throws IOException
294      */

295     protected void switchToBackingStore() throws IOException JavaDoc {
296         bs_handle = File.createTempFile("Axis", ".msg");
297         bs_handle.createNewFile();
298         bs_handle.deleteOnExit();
299         bs_stream = new FileOutputStream JavaDoc(bs_handle);
300         bs_stream.write(cache.toByteArray());
301         cache = null;
302     }
303
304     /**
305      * Method getBackingStoreFileName
306      *
307      * @throws IOException
308      */

309     public String JavaDoc getBackingStoreFileName() throws IOException JavaDoc {
310         String JavaDoc fileName = null;
311         if (bs_handle != null) {
312             fileName = bs_handle.getCanonicalPath();
313         }
314         return fileName;
315     }
316
317     /**
318      * Method discardBackingStore
319      */

320     protected void discardBackingStore() {
321         if (bs_handle != null) {
322             bs_handle.delete();
323             bs_handle = null;
324         }
325     }
326
327     /**
328      * Method createBackingStoreInputStream
329      *
330      * @return
331      * @throws FileNotFoundException
332      */

333     protected InputStream JavaDoc createBackingStoreInputStream()
334             throws FileNotFoundException JavaDoc {
335         try {
336             return new BufferedInputStream JavaDoc(
337                     new FileInputStream JavaDoc(bs_handle.getCanonicalPath()));
338         } catch (IOException JavaDoc e) {
339             throw new FileNotFoundException JavaDoc(bs_handle.getAbsolutePath());
340         }
341     }
342
343     /**
344      * Method toByteArray
345      *
346      * @return
347      * @throws IOException
348      */

349     public byte[] toByteArray() throws IOException JavaDoc {
350         InputStream JavaDoc inp = this.makeInputStream();
351         byte[] buf = null;
352         ByteArrayOutputStream baos = new ByteArrayOutputStream();
353         buf = new byte[WORKING_BUFFER_SIZE];
354         int len;
355         while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) {
356             baos.write(buf, 0, len);
357         }
358         inp.close();
359         discardBackingStore();
360         return baos.toByteArray();
361     }
362
363     /**
364      * Method writeTo
365      *
366      * @param os
367      * @throws IOException
368      */

369     public void writeTo(OutputStream JavaDoc os) throws IOException JavaDoc {
370         InputStream JavaDoc inp = this.makeInputStream();
371         byte[] buf = null;
372         buf = new byte[WORKING_BUFFER_SIZE];
373         int len;
374         while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) {
375             os.write(buf, 0, len);
376         }
377         inp.close();
378         discardBackingStore();
379     }
380 }
381
Popular Tags