public boolean store(String filetype, InputStream stream, long length, Date lastModified) throws IOException,
ExecutionFileStorageException {
File storeFile = getDestinationFile(filetype);
File tempFile = getDestinationTempFile(filetype);
if (!storeFile.getParentFile().isDirectory() && !storeFile.getParentFile().mkdirs()) {
throw new ExecutionFileStorageException(String.format("Failed creating dirs %s", storeFile.getParentFile()));
}
if (!tempFile.getParentFile().isDirectory() && !tempFile.getParentFile().mkdirs()) {
throw new ExecutionFileStorageException(String.format("Failed creating dirs %s", storeFile.getParentFile()));
}
tempFile.deleteOnExit();
OutputStream os = new FileOutputStream(tempFile);
boolean finished = false;
try {
Streams.copyStream(stream, os);
finished = true;
} finally {
os.close();
if (!finished) {
tempFile.delete();
}
}
finished = tempFile.renameTo(storeFile);
if (!finished) {
tempFile.delete();
throw new ExecutionFileStorageException(String.format("Failed to rename output to file %s ", storeFile));
}
return finished;
}