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
public class TempOutputStream extends OutputStream
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 toTempOutputStream.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 Modifier and Type Field Description static StringTEMP_FILE_PREFIX
-
Constructor Summary
Constructors Constructor Description TempOutputStream(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt)Creates a TempOutputStream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voiddestroy()Closes the stream and removes the backing file (if present).static Supplier<TempOutputStream>factory(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt)Creates aTempOutputStreamfactory/supplier.voidflush()longgetLength()InputStreamtoNewInputStream()Returns the data as an InputStreamvoidwrite(byte[] b, int off, int len)voidwrite(int b)-
Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
-
-
-
Field Detail
-
TEMP_FILE_PREFIX
public static final String TEMP_FILE_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TempOutputStream
public TempOutputStream(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt)
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 encrypted
-
-
Method Detail
-
toNewInputStream
public InputStream toNewInputStream() throws IOException
Returns the data as an InputStream- Throws:
IOException
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
destroy
public void destroy() throws IOExceptionCloses 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()
-
factory
public static Supplier<TempOutputStream> factory(File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt)
Creates aTempOutputStreamfactory/supplier.- 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 encrypted
-
-