Class 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 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();
       }
       
     
    • 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 streams
        memoryThreshold - the memory threshold in B
        maxContentSize - the max content size in B
        encrypt - true if temp files should be encrypted
    • Method Detail

      • destroy

        public void destroy()
                     throws IOException
        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()
      • factory

        public static Supplier<TempOutputStream> factory​(File tempDir,
                                                         int memoryThreshold,
                                                         long maxContentSize,
                                                         boolean encrypt)
        Creates a TempOutputStream factory/supplier.
        Parameters:
        tempDir - the temporary directory, i.e. isDir == true, that will be used as * parent directory for creating temp file backed streams
        memoryThreshold - the memory threshold in B
        maxContentSize - the max content size in B
        encrypt - true if temp files should be encrypted