Package org.alfresco.repo.web.scripts
Class TempOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.alfresco.repo.web.scripts.TempOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class TempOutputStream extends java.io.OutputStreamAn 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 java.lang.StringTEMP_FILE_PREFIX
-
Constructor Summary
Constructors Constructor Description TempOutputStream(java.io.File tempDir, int memoryThreshold, long maxContentSize, boolean encrypt, boolean deleteTempFileOnClose)Creates a TempOutputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voiddestroy()Closes the stream and removes the backing file (if present).voidflush()java.io.InputStreamgetInputStream()Returns the data as an InputStreamlonggetLength()voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Field Detail
-
TEMP_FILE_PREFIX
public static final java.lang.String TEMP_FILE_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TempOutputStream
public TempOutputStream(java.io.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 Detail
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOExceptionReturns the data as an InputStream- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException
-
destroy
public void destroy() throws java.io.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:
java.io.IOException
-
getLength
public long getLength()
-
-