byte[] spoolBuffer = new byte[ValueFileIOHelper.IOBUFFER_SIZE];
int read;
int len = 0;
OutputStream out = null;
SwapFile swapFile = null;
try
{
// stream from database
if (content != null)
{
while ((read = content.read(spoolBuffer)) >= 0)
{
if (out != null)
{
// spool to temp file
out.write(spoolBuffer, 0, read);
len += read;
}
else if (len + read > maxBufferSize)
{
// threshold for keeping data in memory exceeded;
// create temp file and spool buffer contents
swapFile = SwapFile.get(swapDirectory, cid + orderNumber + "." + version,swapCleaner);
if (swapFile.isSpooled())
{
// break, value already spooled
buffer = null;
break;
}
out = PrivilegedFileHelper.fileOutputStream(swapFile);
out.write(buffer, 0, len);
out.write(spoolBuffer, 0, read);
buffer = null;
len += read;
}
else
{
// reallocate new buffer and spool old buffer contents
byte[] newBuffer = new byte[len + read];
System.arraycopy(buffer, 0, newBuffer, 0, len);
System.arraycopy(spoolBuffer, 0, newBuffer, len, read);
buffer = newBuffer;
len += read;
}
}
}
}
finally
{
if (out != null)
{
out.close();
swapFile.spoolDone();
}
}
if (buffer == null)
{