Class AS2ResourceHelper

java.lang.Object
com.helger.as2lib.util.AS2ResourceHelper
All Implemented Interfaces:
Closeable, AutoCloseable

public class AS2ResourceHelper extends Object implements Closeable
A resource manager that keeps track of temporary files and other closables that will be closed when this manager is closed. When calling createTempFile() a new filename is created and added to the list. When using addCloseable(Closeable) the Closable is added for postponed closing.
Since:
4.5.3
Author:
Philip Helger
  • Constructor Details

    • AS2ResourceHelper

      public AS2ResourceHelper()
  • Method Details

    • getTempDir

      @Nullable public static File getTempDir()
      Returns:
      The temp file directory to use, or null for the system default.
    • setTempDir

      public static void setTempDir(@Nullable File aTempDir)
      Set a temporary directory to use.
      Parameters:
      aTempDir - The directory to use. It must be an existing directory. May be null to use the system default.
      Throws:
      IllegalArgumentException - If the directory does not exist
    • createTempFile

      @Nonnull public File createTempFile() throws IOException
      Returns:
      A new temporary File that will be deleted when close() is called.
      Throws:
      IOException - When temp file creation fails.
      IllegalStateException - If close() was already called before
    • getAllTempFiles

      @Nonnull @ReturnsMutableCopy public com.helger.commons.collection.impl.ICommonsList<File> getAllTempFiles()
      Returns:
      A list of all known temp files. Never null but maybe empty.
    • addCloseable

      public void addCloseable(@Nonnull Closeable aCloseable)
      Add a new closable for later closing.
      Parameters:
      aCloseable - The closable to be closed later. May not be null.
      Throws:
      IllegalStateException - If close() was already called before
    • getAllCloseables

      @Nonnull @ReturnsMutableCopy public com.helger.commons.collection.impl.ICommonsList<Closeable> getAllCloseables()
      Returns:
      A list of all known closables. Never null but maybe empty.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • createRepeatableHttpEntity

      @Nonnull public org.apache.hc.core5.http.HttpEntity createRepeatableHttpEntity(@Nonnull org.apache.hc.core5.http.HttpEntity aSrcEntity) throws IOException
      Ensure the provided HttpEntity can be read more than once. If the provided entity is not repeatable a temporary file is created and a new file-based Http Entity is created.
      Parameters:
      aSrcEntity - The source Http entity. May not be null.
      Returns:
      A non-null Http entity that can be read more than once.
      Throws:
      IOException - on IO error