Class IoSupport

java.lang.Object
org.opencastproject.util.IoSupport

public final class IoSupport extends Object
Contains operations concerning IO.
  • Field Details

    • readToString

      public static final Function<InputStream,String> readToString
      Function that reads an input stream into a string using utf-8 encoding. Stream does not get closed.
  • Method Details

    • getSystemTmpDir

      public static String getSystemTmpDir()
    • closeQuietly

      public static boolean closeQuietly(Closeable s)
      Closes a Closable quietly so that no exceptions are thrown.
      Parameters:
      s - maybe null
    • closeQuietly

      public static boolean closeQuietly(Process process)
      Closes the processes input, output and error streams.
      Parameters:
      process - the process
      Returns:
      true if the streams were closed
    • writeUTF8File

      public static void writeUTF8File(URL file, String contents) throws IOException
      Writes the contents variable to the URL. Note that the URL must be a local URL.
      Parameters:
      file - The URL of the local file you wish to write to.
      contents - The contents of the file you wish to create.
      Throws:
      IOException
    • writeUTF8File

      public static void writeUTF8File(File file, String contents) throws IOException
      Writes the contents variable to the File.
      Parameters:
      file - The File of the local file you wish to write to.
      contents - The contents of the file you wish to create.
      Throws:
      IOException
    • writeUTF8File

      public static void writeUTF8File(String filename, String contents) throws IOException
      Writes the contents variable to the File located at the filename.
      Parameters:
      filename - The File of the local file you wish to write to.
      contents - The contents of the file you wish to create.
      Throws:
      IOException
    • readFileFromURL

      @Deprecated public static String readFileFromURL(URL url)
      Deprecated.
      this method doesn't support UTF8 or handle HTTP response codes
      Convenience method to read in a file from a local source.
      Parameters:
      url - The URL to read the source data from.
      Returns:
      A String containing the source data or null in the case of an error.
    • readFileFromURL

      @Deprecated public static String readFileFromURL(URL url, TrustedHttpClient trustedClient)
      Deprecated.
      this method doesn't support UTF8 or handle HTTP response codes
      Convenience method to read in a file from either a remote or local source.
      Parameters:
      url - The URL to read the source data from.
      trustedClient - The TrustedHttpClient which should be used to communicate with the remote server. This can be null for local file reads.
      Returns:
      A String containing the source data or null in the case of an error.
    • loadPropertiesFromUrl

      public static Properties loadPropertiesFromUrl(URL url)
    • loadPropertiesFromStream

      public static Properties loadPropertiesFromStream(InputStream stream)
      Load properties from a stream. Close the stream after reading.
    • withResource

      public static <A, B extends Closeable> A withResource(B b, Function<B,A> f)
      Handle a closeable resource inside f and ensure it gets closed properly.
    • withResource

      public static <A, B extends Closeable> A withResource(B b, com.entwinemedia.fn.Fn<? super B,? extends A> f)
      Handle a closeable resource inside f and ensure it gets closed properly.
    • openClassPathResource

      public static Option<InputStream> openClassPathResource(String resource, Class<?> clazz)
      Open a classpath resource using the class loader of the given class.
      Returns:
      an input stream to the resource wrapped in a Some or none if the resource cannot be found
    • openClassPathResource

      public static Option<InputStream> openClassPathResource(String resource)
      Open a classpath resource using the class loader of IoSupport.
      See Also:
    • classPathResourceAsFile

      public static Option<File> classPathResourceAsFile(String resource)
      Get a classpath resource as a file using the class loader of IoSupport.
    • loadFileFromClassPathAsString

      public static Option<String> loadFileFromClassPathAsString(String resource, Class<?> clazz)
      Load a classpath resource into a string using UTF-8 encoding and the class loader of the given class.
      Returns:
      the content of the resource wrapped in a Some or none in case of any error
    • loadFileFromClassPathAsString

      public static Option<String> loadFileFromClassPathAsString(String resource)
      Load a classpath resource into a string using the class loader of IoSupport.
      See Also:
    • withFile

      public static <A> Option<A> withFile(File file, Function2<InputStream,File,A> f)
      Handle a stream inside f and ensure that s gets closed properly.

      Please note: The outcome of f is wrapped into a some. Therefore f is not allowed to return null. Use an Option instead and flatten the overall result.

      Returns:
      none, if the file does not exist
    • withStream

      @Deprecated public static <A, Err> Either<Err,A> withStream(Function0<InputStream> s, Function<Exception,Err> toErr, Function<InputStream,A> f)
      Handle a stream inside f and ensure that s gets closed properly.
      Parameters:
      s - the stream creation function
      toErr - error handler transforming an exception into something else
      f - stream handler
    • withResource

      public static <A, Err, B extends Closeable> Either<Err,A> withResource(Function0<B> r, Function<Exception,Err> toErr, Function<B,A> f)
      Handle a closeable resource inside f and ensure that r gets closed properly.
      Parameters:
      r - resource creation function
      toErr - error handler transforming an exception into something else
      f - resource handler
    • withStream

      @Deprecated public static <A> A withStream(OutputStream s, Function<OutputStream,A> f)
      Handle a stream inside f and ensure that s gets closed properly.
    • fileInputStream

      public static Function0<InputStream> fileInputStream(File a)
      Create a function that creates a FileInputStream.
    • file

      public static File file(String... pathElems)
      Create a file from the list of path elements.
    • locked

      public static <A> A locked(File file, Function<File,A> f) throws NotFoundException, IOException
      Run function f having exclusive read/write access to the given file.

      Please note that the implementation uses Java NIO FileLock which only guarantees that two Java processes cannot interfere with each other.

      The implementation blocks until a lock can be acquired.

      Throws:
      NotFoundException - if the path to the file, to create a lock for, does not exist
      IOException - if the file lock can not be created due to access limitations
    • serializeDeserialize

      public static <A extends Serializable> A serializeDeserialize(A a)
      Serialize and deserialize an object. To test serializability.