Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
20   93   11   3.33
8   51   0.55   6
6     1.83  
1    
 
 
  DeferredSpoolFileOutputStream       Line # 10 20 11 70.6% 0.7058824
 
  (2)
 
1    package com.atlassian.core.spool;
2   
3    import java.io.*;
4   
5    /**
6    * This specialisation of DeferredFileOutputStream may be configured with a FileFactory so that files are only created
7    * once the deferred threshold is reached. getInputStream() returns a SpoolFileInputStream to read the result,
8    * which means that the deferred file will be deleted once the input stream is closed.
9    */
 
10    public class DeferredSpoolFileOutputStream extends DeferredFileOutputStream
11    {
12    private FileFactory fileFactory = DefaultSpoolFileFactory.getInstance();
13    private boolean unspooling = false;
14   
15    /**
16    * Create a new DeferredSpoolFileOutputStream with the specified threshold and deferred file
17    * @see #DeferredFileOutputStream(int, File)
18    */
 
19  0 toggle public DeferredSpoolFileOutputStream(int threshold, File outputFile)
20    {
21  0 super(threshold, outputFile);
22    }
23   
24    /**
25    * Create a new DeferredSpoolFileOutputStream with the specified threshold and file factory. The file factory
26    * will only be used when the threshold is reached, so you can defer the creation of files until they are necessary
27    * @param threshold
28    * @param fileFactory Factory to use when the deferred file must be created
29    */
 
30  2 toggle public DeferredSpoolFileOutputStream(int threshold, FileFactory fileFactory)
31    {
32  2 super(threshold, null);
33  2 this.fileFactory = fileFactory;
34    }
35   
36    /**
37    * @return True if the stream has been closed
38    */
 
39  1 toggle public boolean isClosed()
40    {
41  1 return closed;
42    }
43   
44    /**
45    *
46    * @return True if getInputStream() has been called already
47    */
 
48  1 toggle public boolean isUnspooling()
49    {
50  1 return unspooling;
51    }
52   
 
53  1 toggle protected void thresholdReached() throws IOException
54    {
55  1 if (outputFile == null)
56  1 outputFile = fileFactory.createNewFile();
57  1 super.thresholdReached();
58    }
59   
60    /**
61    * Return an input stream of the written data. This method may only be called once and only once the output stream
62    * has been closed.
63    *
64    * @return A InputStream - If the deferred stream has been written to disk, a SpoolFileInputStream will be returned
65    * and the deferred file will be deleted when this stream is closed.
66    * @throws IOException If the output stream is not closed or an input stream has already been returned
67    */
 
68  1 toggle public InputStream getInputStream() throws IOException
69    {
70  1 if (!isClosed())
71  0 throw new IOException("Output stream not closed");
72   
73  1 if (isUnspooling())
74  0 throw new IOException("Stream is already being unspooled");
75   
76  1 InputStream spoolStream;
77   
78  1 if (isInMemory())
79  0 spoolStream = new ByteArrayInputStream(getData());
80    else
81  1 try
82    {
83  1 spoolStream = new SpoolFileInputStream(getFile());
84    }
85    catch (FileNotFoundException ex)
86    {
87  0 throw new IOException("Deferred file does not exist");
88    }
89   
90  1 unspooling = true;
91  1 return spoolStream;
92    }
93    }