Package org.alfresco.repo.web.scripts
Class TempOutputStream
java.lang.Object
java.io.OutputStream
org.alfresco.repo.web.scripts.TempOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
An output stream implementation that keeps the data in memory if is less then
the specified memoryThreshold otherwise it writes it to a temp file.
Close the stream before any call to
TempOutputStream
.getInputStream().
If deleteTempFileOnClose is false then use proper try-finally patterns
to ensure that the temp file is destroyed after it is no longer needed.
try
{
StreamUtils.copy(new BufferedInputStream(new FileInputStream(file)), tempOutputStream);
tempOutputStream.close();
}
finally
{
tempOutputStream.destroy();
}
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTempOutputStream
(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt, boolean deleteTempFileOnClose) Creates a TempOutputStream. -
Method Summary
Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
Field Details
-
TEMP_FILE_PREFIX
- See Also:
-
-
Constructor Details
-
TempOutputStream
public TempOutputStream(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt, boolean deleteTempFileOnClose) Creates a TempOutputStream.- Parameters:
tempDir
- the temporary directory, i.e.isDir == true
, that will be used as * parent directory for creating temp file backed streamsmemoryThreshold
- the memory threshold in BmaxContentSize
- the max content size in Bencrypt
- true if temp files should be encrypteddeleteTempFileOnClose
- true if temp files should be deleted on output stream close (useful if we need to cache the content for further reads). If this is false then we need to make sure we callTempOutputStream
.destroy to clean up properly.
-
-
Method Details
-
getInputStream
Returns the data as an InputStream- Throws:
IOException
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
destroy
Closes the stream and removes the backing file (if present). If deleteTempFileOnClose is false then use proper try-finally patterns to ensure that the temp file is destroyed after it is no longer needed.try { StreamUtils.copy(new BufferedInputStream(new FileInputStream(file)), tempOutputStream); tempOutputStream.close(); } finally { tempOutputStream.destroy(); }
- Throws:
IOException
-
getLength
public long getLength()
-