Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
29   108   16   4.14
14   78   0.55   7
7     2.29  
1    
 
 
  SpoolFileInputStream       Line # 13 29 16 40% 0.4
 
  (4)
 
1    package com.atlassian.core.spool;
2   
3    import org.apache.log4j.Category;
4   
5    import java.io.*;
6   
7    /**
8    * A FileInputStream that deletes its input file when closed. Useful for transient file spooling.
9    *
10    * Because we don't know when a database will have finished using the stream, or if it will close it itself, this class
11    * closes the stream when all the data has been read from it.
12    */
 
13    public class SpoolFileInputStream extends FileInputStream {
14   
15    private static final Category log = Category.getInstance(SpoolFileInputStream.class);
16   
17    private File fileToDelete;
18   
19    private boolean closed = false;
20   
21    /**
22    * @param file
23    * @throws FileNotFoundException
24    */
 
25  3 toggle public SpoolFileInputStream(File file) throws FileNotFoundException
26    {
27  3 super(file);
28  3 init(file);
29    }
30   
31    /**
32    * @param name
33    * @throws FileNotFoundException
34    */
 
35  0 toggle public SpoolFileInputStream(String name) throws FileNotFoundException
36    {
37  0 super(name);
38  0 init(new File(name));
39    }
40   
 
41  3 toggle private void init(File file)
42    {
43  3 this.fileToDelete = file;
44    }
45   
46    /* (non-Javadoc)
47    * @see java.io.FileInputStream#close()
48    */
 
49  6 toggle public void close() throws IOException
50    {
51  6 try {
52  6 closed = true;
53  6 super.close();
54    }
55    catch (IOException ex)
56    {
57  0 log.error("Error closing spool stream", ex);
58    }
59    finally
60    {
61  6 if (fileToDelete.exists() && !fileToDelete.delete())
62    {
63  0 log.warn("Could not delete spool file " + fileToDelete);
64    }
65    }
66    }
67   
 
68  0 toggle public int read() throws IOException
69    {
70  0 if (closed)
71    {
72  0 return -1;
73    }
74  0 int n = super.read();
75  0 if (n == -1)
76    {
77  0 close();
78    }
79  0 return n;
80    }
81   
 
82  0 toggle public int read(byte b[]) throws IOException
83    {
84  0 if (closed)
85    {
86  0 return -1;
87    }
88  0 int n = super.read(b);
89  0 if (n == -1)
90    {
91  0 close();
92    }
93  0 return n; }
94   
 
95  8 toggle public int read(byte b[], int off, int len) throws IOException
96    {
97  8 if (closed)
98    {
99  0 return -1;
100    }
101  8 int n = super.read(b, off, len);
102  8 if (n == -1)
103    {
104  2 close();
105    }
106  8 return n;
107    }
108    }