KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > helpers > DelayedOutputStream


1 /*
2  * Copyright 1999-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.cocoon.bean.helpers;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 /**
24  * A output stream writing to a ByteArrayOutputStream, until an OutputStream target is defined.
25  *
26  * @author huber@apache.org
27  * @author uv@upaya.co.uk
28  * @version CVS $Id: DelayedOutputStream.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class DelayedOutputStream extends OutputStream JavaDoc {
31     /**
32      * write to baos as long as fos member is still null,
33      * create a ByteArrayOutputStream only
34      */

35     private ByteArrayOutputStream JavaDoc baos;
36     /**
37      * If fos is defined write into fos, dump content of
38      * baos to fos first
39      */

40     private OutputStream JavaDoc fos;
41
42
43     /**
44      * Constructor for the DelayedFileOutputStream object,
45      * create a ByteArrayOutputStream only
46      */

47     public DelayedOutputStream() {
48         baos = new ByteArrayOutputStream JavaDoc();
49         fos = null;
50     }
51
52     /**
53      * Creates a file output stream to write to the file represented by the specified File object.
54      *
55      * @param outputStream The new fileOutputStream value
56      * @exception FileNotFoundException thrown if creating of FileOutputStream fails
57      */

58     public void setFileOutputStream(OutputStream JavaDoc outputStream) throws FileNotFoundException JavaDoc {
59         if (fos == null) {
60             fos = outputStream;
61         }
62     }
63
64     /**
65      * Write into ByteArrayOutputStrem, or FileOutputStream, depending on inner
66      * state of this stream
67      *
68      * @param b Description of Parameter
69      * @exception IOException thrown iff implicitly flush of baos to fos fails, or writing
70      * of baos, or fos fails
71      */

72     public void write(int b) throws IOException JavaDoc {
73         OutputStream JavaDoc os = getTargetOutputStream();
74         os.write(b);
75     }
76
77
78     /**
79      * Write into ByteArrayOutputStrem, or FileOutputStream, depending on inner
80      * state of this stream
81      *
82      * @param b Description of Parameter
83      * @exception IOException thrown iff implicitly flush of baos to fos fails, or writing
84      * of baos, or fos fails
85      */

86     public void write(byte b[]) throws IOException JavaDoc {
87         OutputStream JavaDoc os = getTargetOutputStream();
88         os.write(b);
89     }
90
91
92     /**
93      * Write into ByteArrayOutputStrem, or FileOutputStream, depending on inner
94      * state of this stream
95      *
96      * @param b Description of Parameter
97      * @param off Description of Parameter
98      * @param len Description of Parameter
99      * @exception IOException thrown iff implicitly flush of baos to fos fails, or writing
100      * of baos, or fos fails
101      */

102     public void write(byte b[], int off, int len) throws IOException JavaDoc {
103         OutputStream JavaDoc os = getTargetOutputStream();
104         os.write(b, off, len);
105     }
106
107
108     /**
109      * Close ByteArrayOutputStrem, and FileOutputStream, depending on inner
110      * state of this stream
111      *
112      * @exception IOException thrown iff implicitly flush of baos to fos fails, or closing
113      * of baos, or fos fails
114      */

115     public void close() throws IOException JavaDoc {
116         IOException JavaDoc ioexception = null;
117
118         getTargetOutputStream();
119
120         // close baos
121
try {
122             if (baos != null) {
123                 baos.close();
124             }
125         } catch (IOException JavaDoc ioe) {
126             ioexception = ioe;
127         } finally {
128             baos = null;
129         }
130
131         // close fos
132
try {
133             if (fos != null) {
134                 fos.close();
135             }
136         } catch (IOException JavaDoc ioe) {
137             if (ioexception == null) {
138                 ioexception = ioe;
139             }
140         } finally {
141             fos = null;
142         }
143
144         if (ioexception != null) {
145             throw ioexception;
146         }
147     }
148
149
150     /**
151      * Flush ByteArrayOutputStrem, writing content to FileOutputStream,
152      * flush FileOutputStream
153      *
154      * @exception IOException thrown iff implicitly flush of baos to fos fails, or flushing
155      * of baos, or fos fails
156      */

157     public void flush() throws IOException JavaDoc {
158         IOException JavaDoc ioexception = null;
159
160         // flush baos, writing to fos, if neccessary
161
getTargetOutputStream();
162
163         // flush baos
164
try {
165             if (baos != null) {
166                 baos.flush();
167             }
168         } catch (IOException JavaDoc ioe) {
169             ioexception = ioe;
170         }
171
172         // flush fos
173
try {
174             if (fos != null) {
175                 fos.flush();
176             }
177         } catch (IOException JavaDoc ioe) {
178             if (ioexception == null) {
179                 ioexception = ioe;
180             }
181         }
182         if (ioexception != null) {
183             throw ioexception;
184         }
185     }
186
187
188     /**
189      * Gets the targetOutputStream attribute of the DelayedFileOutputStream object
190      *
191      * @return The targetOutputStream value
192      * @exception IOException thrown iff implicitly flush of baos to fos fails
193      */

194     private OutputStream JavaDoc getTargetOutputStream() throws IOException JavaDoc {
195         if (baos != null && fos == null) {
196
197             // no fos is defined, just write to baos in the mean time
198
return baos;
199         } else if (baos != null && fos != null) {
200             // fos is defined, flush boas to fos, and destroy baos
201
try {
202                 baos.flush();
203                 baos.writeTo(fos);
204                 baos.close();
205             } finally {
206                 baos = null;
207             }
208
209             return fos;
210         } else if (baos == null && fos != null) {
211             // no more temporary baos writing, write directly to fos
212
return fos;
213         } else {
214             // neither baos, nor fos are valid
215
throw new IOException JavaDoc("No outputstream available!");
216         }
217     }
218
219     /**
220      * Gets the size of the content of the current output stream
221      */

222     public int size() {
223         if (baos != null) {
224             return baos.size();
225         }
226         return 0;
227     }
228
229     /**
230      * Return the contents of the stream as a byte array
231      */

232     public byte[] getContent() {
233         if (baos != null) {
234             return baos.toByteArray();
235         } else {
236             return null;
237         }
238     }
239 }
240
Popular Tags