KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > fileupload > DeferredFileOutputStream


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

17
18
19 package org.apache.tomcat.util.http.fileupload;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 /**
28  * <p>An output stream which will retain data in memory until a specified
29  * threshold is reached, and only then commit it to disk. If the stream is
30  * closed before the threshold is reached, the data will not be written to
31  * disk at all.</p>
32  *
33  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
34  *
35  * @version $Id: DeferredFileOutputStream.java 467222 2006-10-24 03:17:11Z markt $
36  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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