KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > SaveRestoreStream


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.FileNotFoundException JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.OutputStream JavaDoc;
50
51 /**
52  * This class implements a simple persistent object which can be written
53  * as an OutputStream and read as an InputStream. Small objects can be
54  * stored in memory, larger objects can be stored in temp files on disk.
55  *
56  * @author Stan Bailes
57  */

58 public class SaveRestoreStream {
59     ByteArrayOutputStream JavaDoc bos = null;
60     FileOutputStream JavaDoc fos = null;
61     File JavaDoc file = null;
62     int len = 0;
63     int max = 4096;
64
65     /**
66      * Construct a new SaveRestoreStream with the default max buffered
67      */

68     public SaveRestoreStream() {
69     }
70
71     /**
72      * Construct a new SaveRestoreStream with the specified max buffered
73      * size.
74      *
75      * @param max the maximum amount of data to be buffered in memory.
76      */

77     public SaveRestoreStream(int max) {
78         this.max = max;
79     }
80
81     /**
82      * Construct a new SaveRestoreStream using the data in the specified
83      * InputStream.
84      *
85      * @param in the InputStream supplying the data for this stream
86      */

87     public SaveRestoreStream(InputStream JavaDoc in) throws IOException JavaDoc {
88         OutputStream JavaDoc out = getOutputStream();
89         try {
90             IO.copyStream(in, out);
91         } finally {
92             out.close();
93         }
94     }
95
96     /**
97      * Return an output stream that the caller can use to save his object
98      *
99      * @return the output stream
100      */

101     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
102     if (file == null) {
103         bos = new ByteArrayOutputStream JavaDoc();
104         if (fos != null) fos.close();
105         fos = null;
106     } else {
107         bos = null;
108         fos = new FileOutputStream JavaDoc(file);
109     }
110     
111         return new OutputStream JavaDoc() {
112             public void write(int c) throws IOException JavaDoc {
113                 doWrite(c);
114             }
115             public void flush() throws IOException JavaDoc {
116                 if (bos != null) bos.flush();
117                 else fos.flush();
118             }
119         public void close() throws IOException JavaDoc {
120         if (bos != null) {
121             len = bos.size();
122         } else if (fos != null) {
123             fos.close();
124             fos = null;
125             len = (int)file.length();
126         }
127         }
128         };
129     }
130
131     public int length() {
132     return len;
133     }
134
135     static int tmpFileCount = 0;
136
137     static synchronized File JavaDoc tmpFile() throws IOException JavaDoc {
138     return File.createTempFile("" + System.currentTimeMillis() + "." +
139                    (tmpFileCount++), ".tmp");
140     }
141
142     /**
143      * Write a byte to the stream.
144      *
145      * @param c the byte
146      * @exception IOException may be thrown
147      */

148     public void doWrite(int c) throws IOException JavaDoc {
149         if (bos != null) {
150             if (bos.size() < max) {
151                 bos.write(c);
152             } else {
153                 file = tmpFile();
154                 fos = new FileOutputStream JavaDoc(file);
155                 bos.writeTo(fos);
156                 fos.write(c);
157         bos.close();
158                 bos = null;
159             }
160         } else {
161             fos.write(c);
162         }
163     }
164                     
165     /**
166      * Return an input stream that the user can use to get his data back
167      *
168      * @return the input stream
169      */

170     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
171         return getInputStream(true);
172     }
173     
174     public InputStream JavaDoc getInputStream(final boolean del) throws IOException JavaDoc {
175         if (bos != null) return new ByteArrayInputStream JavaDoc(bos.toByteArray());
176         if (fos != null) {
177         fos.close();
178         fos = null;
179     }
180         if (file != null) {
181         final FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
182         final File JavaDoc f = file;
183         if (del) file = null;
184         return new InputStream JavaDoc() {
185         public int read() throws IOException JavaDoc {
186             return fis.read();
187         }
188         public int read(byte[] buf) throws IOException JavaDoc {
189             return fis.read(buf);
190         }
191         public int read(byte[] buf, int off, int cnt) throws IOException JavaDoc {
192             return fis.read(buf, off, cnt);
193         }
194         public void close() throws IOException JavaDoc {
195             fis.close();
196             if (del) f.delete();
197         }
198         public void finalize() {
199             try {
200             close();
201             } catch (Throwable JavaDoc t) {}
202         }
203         };
204         }
205         throw new IOException JavaDoc("no data");
206     }
207
208     /**
209      * Return the data as a string
210      */

211     public String JavaDoc toString() {
212     if (bos != null) return bos.toString();
213     if (fos != null) {
214         throw new RuntimeException JavaDoc("too large");
215     }
216     return null;
217     }
218
219     public void close() {
220     bos = null;
221     if (fos != null) {
222         try {
223         fos.close();
224         } catch (Exception JavaDoc e) {
225         }
226     }
227     fos = null;
228     if (file != null) {
229         try {
230         file.delete();
231         } catch (Exception JavaDoc e) {
232         }
233     }
234     file = null;
235     len = 0;
236     }
237
238     public void reset() {
239     close();
240     }
241
242     public void finalize() {
243         close();
244     }
245 }
246
Popular Tags