1
|
package edu.ucsb.nceas.metacat.util;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileInputStream;
|
5
|
import java.io.FileNotFoundException;
|
6
|
import java.io.IOException;
|
7
|
|
8
|
/**
|
9
|
* Extension of FileInputSteam that deletes the sourcefile when the
|
10
|
* InputStream is closed. Should typically be used with temporary files
|
11
|
* when a more immediate deletion should be performed than is offered
|
12
|
* by the File.deleteOnExit() method.
|
13
|
* @author leinfelder
|
14
|
*
|
15
|
*/
|
16
|
public class DeleteOnCloseFileInputStream extends FileInputStream {
|
17
|
private File file;
|
18
|
|
19
|
public DeleteOnCloseFileInputStream(String name) throws FileNotFoundException {
|
20
|
this(new File(name));
|
21
|
}
|
22
|
|
23
|
public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException {
|
24
|
super(file);
|
25
|
this.file = file;
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Allow access to the underlying file - careful!
|
30
|
* @return
|
31
|
*/
|
32
|
public File getFile() {
|
33
|
return file;
|
34
|
}
|
35
|
|
36
|
|
37
|
/**
|
38
|
* Delete the file when the InputStream is closed
|
39
|
*/
|
40
|
public void close() throws IOException {
|
41
|
try {
|
42
|
super.close();
|
43
|
} finally {
|
44
|
if (file != null) {
|
45
|
file.delete();
|
46
|
file = null;
|
47
|
}
|
48
|
}
|
49
|
}
|
50
|
|
51
|
}
|