1. /*
  2. * $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/DeferredFileOutputStream.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2003/05/31 22:31:08 $
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowlegement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowlegement may appear in the software itself,
  30. * if and wherever such third-party acknowlegements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Group.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. */
  61. package org.apache.commons.fileupload;
  62. import java.io.ByteArrayOutputStream;
  63. import java.io.File;
  64. import java.io.FileOutputStream;
  65. import java.io.IOException;
  66. import java.io.OutputStream;
  67. /**
  68. * <p>An output stream which will retain data in memory until a specified
  69. * threshold is reached, and only then commit it to disk. If the stream is
  70. * closed before the threshold is reached, the data will not be written to
  71. * disk at all.</p>
  72. *
  73. * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  74. *
  75. * @version $Id: DeferredFileOutputStream.java,v 1.2 2003/05/31 22:31:08 martinc Exp $
  76. */
  77. public class DeferredFileOutputStream
  78. extends ThresholdingOutputStream
  79. {
  80. // ----------------------------------------------------------- Data members
  81. /**
  82. * The output stream to which data will be written prior to the theshold
  83. * being reached.
  84. */
  85. private ByteArrayOutputStream memoryOutputStream;
  86. /**
  87. * The output stream to which data will be written after the theshold is
  88. * reached.
  89. */
  90. private FileOutputStream diskOutputStream;
  91. /**
  92. * The output stream to which data will be written at any given time. This
  93. * will always be one of <code>memoryOutputStream</code> or
  94. * <code>diskOutputStream</code>.
  95. */
  96. private OutputStream currentOutputStream;
  97. /**
  98. * The file to which output will be directed if the threshold is exceeded.
  99. */
  100. private File outputFile;
  101. // ----------------------------------------------------------- Constructors
  102. /**
  103. * Constructs an instance of this class which will trigger an event at the
  104. * specified threshold, and save data to a file beyond that point.
  105. *
  106. * @param threshold The number of bytes at which to trigger an event.
  107. * @param outputFile The file to which data is saved beyond the threshold.
  108. */
  109. public DeferredFileOutputStream(int threshold, File outputFile)
  110. {
  111. super(threshold);
  112. this.outputFile = outputFile;
  113. memoryOutputStream = new ByteArrayOutputStream(threshold);
  114. currentOutputStream = memoryOutputStream;
  115. }
  116. // --------------------------------------- ThresholdingOutputStream methods
  117. /**
  118. * Returns the current output stream. This may be memory based or disk
  119. * based, depending on the current state with respect to the threshold.
  120. *
  121. * @return The underlying output stream.
  122. *
  123. * @exception IOException if an error occurs.
  124. */
  125. protected OutputStream getStream() throws IOException
  126. {
  127. return currentOutputStream;
  128. }
  129. /**
  130. * Switches the underlying output stream from a memory based stream to one
  131. * that is backed by disk. This is the point at which we realise that too
  132. * much data is being written to keep in memory, so we elect to switch to
  133. * disk-based storage.
  134. *
  135. * @exception IOException if an error occurs.
  136. */
  137. protected void thresholdReached() throws IOException
  138. {
  139. byte[] data = memoryOutputStream.toByteArray();
  140. FileOutputStream fos = new FileOutputStream(outputFile);
  141. fos.write(data);
  142. diskOutputStream = fos;
  143. currentOutputStream = fos;
  144. memoryOutputStream = null;
  145. }
  146. // --------------------------------------------------------- Public methods
  147. /**
  148. * Determines whether or not the data for this output stream has been
  149. * retained in memory.
  150. *
  151. * @return <code>true</code> if the data is available in memory;
  152. * <code>false</code> otherwise.
  153. */
  154. public boolean isInMemory()
  155. {
  156. return (!isThresholdExceeded());
  157. }
  158. /**
  159. * Returns the data for this output stream as an array of bytes, assuming
  160. * that the data has been retained in memory. If the data was written to
  161. * disk, this method returns <code>null</code>.
  162. *
  163. * @return The data for this output stream, or <code>null</code> if no such
  164. * data is available.
  165. */
  166. public byte[] getData()
  167. {
  168. if (memoryOutputStream != null)
  169. {
  170. return memoryOutputStream.toByteArray();
  171. }
  172. return null;
  173. }
  174. /**
  175. * Returns the data for this output stream as a <code>File</code>, assuming
  176. * that the data was written to disk. If the data was retained in memory,
  177. * this method returns <code>null</code>.
  178. *
  179. * @return The file for this output stream, or <code>null</code> if no such
  180. * file exists.
  181. */
  182. public File getFile()
  183. {
  184. return outputFile;
  185. }
  186. }