4040public class DeferredFileOutputStream
4141 extends ThresholdingOutputStream
4242{
43- // ----------------------------------------------------------- Data members
44-
4543
4644 /**
4745 * The output stream to which data will be written prior to the threshold
4846 * being reached.
4947 */
5048 private ByteArrayOutputStream memoryOutputStream ;
5149
52-
5350 /**
5451 * The output stream to which data will be written at any given time. This
5552 * will always be one of <code>memoryOutputStream</code> or
5653 * <code>diskOutputStream</code>.
5754 */
5855 private OutputStream currentOutputStream ;
5956
60-
6157 /**
6258 * The file to which output will be directed if the threshold is exceeded.
6359 */
@@ -78,15 +74,11 @@ public class DeferredFileOutputStream
7874 */
7975 private final File directory ;
8076
81-
8277 /**
8378 * True when close() has been called successfully.
8479 */
8580 private boolean closed = false ;
8681
87- // ----------------------------------------------------------- Constructors
88-
89-
9082 /**
9183 * Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a
9284 * file beyond that point. The initial buffer size will default to
@@ -185,10 +177,6 @@ private DeferredFileOutputStream(final int threshold, final File outputFile, fin
185177 currentOutputStream = memoryOutputStream ;
186178 }
187179
188-
189- // --------------------------------------- ThresholdingOutputStream methods
190-
191-
192180 /**
193181 * Returns the current output stream. This may be memory based or disk
194182 * based, depending on the current state with respect to the threshold.
@@ -203,7 +191,6 @@ protected OutputStream getStream() throws IOException
203191 return currentOutputStream ;
204192 }
205193
206-
207194 /**
208195 * Switches the underlying output stream from a memory based stream to one
209196 * that is backed by disk. This is the point at which we realize that too
@@ -230,10 +217,6 @@ protected void thresholdReached() throws IOException
230217 memoryOutputStream = null ;
231218 }
232219
233-
234- // --------------------------------------------------------- Public methods
235-
236-
237220 /**
238221 * Determines whether or not the data for this output stream has been
239222 * retained in memory.
@@ -246,7 +229,6 @@ public boolean isInMemory()
246229 return !isThresholdExceeded ();
247230 }
248231
249-
250232 /**
251233 * Returns the data for this output stream as an array of bytes, assuming
252234 * that the data has been retained in memory. If the data was written to
@@ -257,14 +239,9 @@ public boolean isInMemory()
257239 */
258240 public byte [] getData ()
259241 {
260- if (memoryOutputStream != null )
261- {
262- return memoryOutputStream .toByteArray ();
263- }
264- return null ;
242+ return memoryOutputStream != null ? memoryOutputStream .toByteArray () : null ;
265243 }
266244
267-
268245 /**
269246 * Returns either the output file specified in the constructor or
270247 * the temporary file created or null.
@@ -284,7 +261,6 @@ public File getFile()
284261 return outputFile ;
285262 }
286263
287-
288264 /**
289265 * Closes underlying output stream, and mark this as closed
290266 *
@@ -297,15 +273,15 @@ public void close() throws IOException
297273 closed = true ;
298274 }
299275
300-
301276 /**
302277 * Writes the data from this output stream to the specified output stream,
303278 * after it has been closed.
304279 *
305- * @param out output stream to write to.
280+ * @param outputStream output stream to write to.
281+ * @throws NullPointerException if the OutputStream is {@code null}.
306282 * @throws IOException if this stream is not yet closed or an error occurs.
307283 */
308- public void writeTo (final OutputStream out ) throws IOException
284+ public void writeTo (final OutputStream outputStream ) throws IOException
309285 {
310286 // we may only need to check if this is closed if we are working with a file
311287 // but we should force the habit of closing whether we are working with
@@ -315,10 +291,10 @@ public void writeTo(final OutputStream out) throws IOException
315291 }
316292
317293 if (isInMemory ()) {
318- memoryOutputStream .writeTo (out );
294+ memoryOutputStream .writeTo (outputStream );
319295 } else {
320296 try (FileInputStream fis = new FileInputStream (outputFile )) {
321- IOUtils .copy (fis , out );
297+ IOUtils .copy (fis , outputStream );
322298 }
323299 }
324300 }
0 commit comments