KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > DeferredFileOutputStream


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
17
18 package org.apache.commons.io.output;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 /**
27  * <p>An output stream which will retain data in memory until a specified
28  * threshold is reached, and only then commit it to disk. If the stream is
29  * closed before the threshold is reached, the data will not be written to
30  * disk at all.</p>
31  *
32  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
33  *
34  * @version $Id: DeferredFileOutputStream.java,v 1.2 2004/02/23 04:40:29 bayard Exp $
35  */

36 public class DeferredFileOutputStream
37     extends ThresholdingOutputStream
38 {
39
40     // ----------------------------------------------------------- Data members
41

42
43     /**
44      * The output stream to which data will be written prior to the theshold
45      * being reached.
46      */

47     private ByteArrayOutputStream memoryOutputStream;
48
49
50     /**
51      * The output stream to which data will be written after the theshold is
52      * reached.
53      */

54     private FileOutputStream JavaDoc diskOutputStream;
55
56
57     /**
58      * The output stream to which data will be written at any given time. This
59      * will always be one of <code>memoryOutputStream</code> or
60      * <code>diskOutputStream</code>.
61      */

62     private OutputStream JavaDoc currentOutputStream;
63
64
65     /**
66      * The file to which output will be directed if the threshold is exceeded.
67      */

68     private File JavaDoc outputFile;
69
70
71     // ----------------------------------------------------------- Constructors
72

73
74     /**
75      * Constructs an instance of this class which will trigger an event at the
76      * specified threshold, and save data to a file beyond that point.
77      *
78      * @param threshold The number of bytes at which to trigger an event.
79      * @param outputFile The file to which data is saved beyond the threshold.
80      */

81     public DeferredFileOutputStream(int threshold, File JavaDoc outputFile)
82     {
83         super(threshold);
84         this.outputFile = outputFile;
85
86         memoryOutputStream = new ByteArrayOutputStream(threshold);
87         currentOutputStream = memoryOutputStream;
88     }
89
90
91     // --------------------------------------- ThresholdingOutputStream methods
92

93
94     /**
95      * Returns the current output stream. This may be memory based or disk
96      * based, depending on the current state with respect to the threshold.
97      *
98      * @return The underlying output stream.
99      *
100      * @exception IOException if an error occurs.
101      */

102     protected OutputStream JavaDoc getStream() throws IOException JavaDoc
103     {
104         return currentOutputStream;
105     }
106
107
108     /**
109      * Switches the underlying output stream from a memory based stream to one
110      * that is backed by disk. This is the point at which we realise that too
111      * much data is being written to keep in memory, so we elect to switch to
112      * disk-based storage.
113      *
114      * @exception IOException if an error occurs.
115      */

116     protected void thresholdReached() throws IOException JavaDoc
117     {
118         byte[] data = memoryOutputStream.toByteArray();
119         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(outputFile);
120         fos.write(data);
121         diskOutputStream = fos;
122         currentOutputStream = fos;
123         memoryOutputStream = null;
124     }
125
126
127     // --------------------------------------------------------- Public methods
128

129
130     /**
131      * Determines whether or not the data for this output stream has been
132      * retained in memory.
133      *
134      * @return <code>true</code> if the data is available in memory;
135      * <code>false</code> otherwise.
136      */

137     public boolean isInMemory()
138     {
139         return (!isThresholdExceeded());
140     }
141
142
143     /**
144      * Returns the data for this output stream as an array of bytes, assuming
145      * that the data has been retained in memory. If the data was written to
146      * disk, this method returns <code>null</code>.
147      *
148      * @return The data for this output stream, or <code>null</code> if no such
149      * data is available.
150      */

151     public byte[] getData()
152     {
153         if (memoryOutputStream != null)
154         {
155             return memoryOutputStream.toByteArray();
156         }
157         return null;
158     }
159
160
161     /**
162      * Returns the data for this output stream as a <code>File</code>, assuming
163      * that the data was written to disk. If the data was retained in memory,
164      * this method returns <code>null</code>.
165      *
166      * @return The file for this output stream, or <code>null</code> if no such
167      * file exists.
168      */

169     public File JavaDoc getFile()
170     {
171         return outputFile;
172     }
173 }
174
Popular Tags