A new flavor of createFile. Slightly slower. Significantly safer.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3277 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-07-03 16:36:31 +00:00
parent 92a7348a43
commit 37843668a4
9 changed files with 163 additions and 6 deletions

View File

@@ -18,6 +18,8 @@
package org.alfresco.repo.avm;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
@@ -356,6 +358,58 @@ public class AVMServiceImpl implements AVMService
return doit.out;
}
/**
* Create a file with content specified by the InputStream.
* Guaranteed to be created atomically.
* @param path The path to the containing directory.
* @param name The name to give the file.
* @param in An InputStream containing data for file.
*/
public void createFile(final String path, final String name, InputStream in)
{
if (path == null || name == null || in == null)
{
throw new AVMBadArgumentException("Illegal null argument.");
}
// Save the contents to temp space.
File dir = new File(fStorage);
final File temp;
try
{
temp = File.createTempFile("alf", "tmp", dir);
OutputStream out = new FileOutputStream(temp);
byte [] buff = new byte[8192];
int read;
while ((read = in.read(buff)) != -1)
{
out.write(buff, 0, read);
}
out.close();
in.close();
}
catch (IOException ie)
{
throw new AVMException("I/O Error.");
}
class HTxnCallback implements HibernateTxnCallback
{
public void perform(Session session)
{
fSuperRepository.setSession(session);
fSuperRepository.createFile(path, name, temp);
}
}
HTxnCallback doit = new HTxnCallback();
try
{
fTransaction.perform(doit, true);
}
finally
{
temp.delete();
}
}
/* (non-Javadoc)
* @see org.alfresco.repo.avm.AVMService#createFolder(java.lang.String, java.lang.String)
*/