byte[] buffer = new byte[0];
byte[] tmpBuff = new byte[2048];
int read = 0;
int len = 0;
SpoolFile sf = null;
OutputStream sfout = null;
try
{
while ((read = tmpStream.read(tmpBuff)) >= 0)
{
if (sfout != null)
{
// spool to temp file
sfout.write(tmpBuff, 0, read);
len += read;
}
else if (len + read > maxBufferSize && fileCleaner != null)
{
// threshold for keeping data in memory exceeded,
// if have a fileCleaner create temp file and spool buffer contents.
sf = SpoolFile.createTempFile("jcrvd", null, tempDirectory);
sf.acquire(this);
sfout = PrivilegedFileHelper.fileOutputStream(sf);
sfout.write(buffer, 0, len);
sfout.write(tmpBuff, 0, read);