Package org.opencastproject.util
Class IoSupport
java.lang.Object
org.opencastproject.util.IoSupport
Contains operations concerning IO.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Function<InputStream, String> Function that reads an input stream into a string using utf-8 encoding. -
Method Summary
Modifier and TypeMethodDescriptionclassPathResourceAsFile(String resource) Get a classpath resource as a file using the class loader ofIoSupport.static booleanCloses aClosablequietly so that no exceptions are thrown.static booleancloseQuietly(Process process) Closes the processes input, output and error streams.static FileCreate a file from the list of path elements.static Function0<InputStream> Create a function that creates aFileInputStream.static StringloadFileFromClassPathAsString(String resource) Load a classpath resource into a string using the class loader ofIoSupport.loadFileFromClassPathAsString(String resource, Class<?> clazz) Load a classpath resource into a string using UTF-8 encoding and the class loader of the given class.static PropertiesloadPropertiesFromStream(InputStream stream) Load properties from a stream.static PropertiesloadPropertiesFromUrl(URL url) static <A> ARun functionfhaving exclusive read/write access to the given file.static Option<InputStream> openClassPathResource(String resource) Open a classpath resource using the class loader ofIoSupport.static Option<InputStream> openClassPathResource(String resource, Class<?> clazz) Open a classpath resource using the class loader of the given class.static StringreadFileFromURL(URL url) Deprecated.this method doesn't support UTF8 or handle HTTP response codesstatic StringreadFileFromURL(URL url, TrustedHttpClient trustedClient) Deprecated.this method doesn't support UTF8 or handle HTTP response codesstatic <A extends Serializable>
AserializeDeserialize(A a) Serialize and deserialize an object.static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) Handle a stream insidefand ensure thatsgets closed properly.static <A,B extends Closeable>
AwithResource(B b, com.entwinemedia.fn.Fn<? super B, ? extends A> f) Handle a closeable resource insidefand ensure it gets closed properly.static <A,B extends Closeable>
AwithResource(B b, Function<B, A> f) Handle a closeable resource insidefand ensure it gets closed properly.withResource(Function0<B> r, Function<Exception, Err> toErr, Function<B, A> f) Handle a closeable resource insidefand ensure thatrgets closed properly.static <A> AwithStream(OutputStream s, Function<OutputStream, A> f) Deprecated.static <A,Err> Either <Err, A> withStream(Function0<InputStream> s, Function<Exception, Err> toErr, Function<InputStream, A> f) static voidwriteUTF8File(File file, String contents) Writes the contents variable to theFile.static voidwriteUTF8File(String filename, String contents) Writes the contents variable to theFilelocated at the filename.static voidwriteUTF8File(URL file, String contents) Writes the contents variable to theURL.
-
Field Details
-
readToString
Function that reads an input stream into a string using utf-8 encoding. Stream does not get closed.
-
-
Method Details
-
getSystemTmpDir
-
closeQuietly
Closes aClosablequietly so that no exceptions are thrown.- Parameters:
s- maybe null
-
closeQuietly
Closes the processes input, output and error streams.- Parameters:
process- the process- Returns:
trueif the streams were closed
-
writeUTF8File
Writes the contents variable to theURL. Note that the URL must be a localURL.- Parameters:
file- TheURLof the local file you wish to write to.contents- The contents of the file you wish to create.- Throws:
IOException
-
writeUTF8File
Writes the contents variable to theFile.- Parameters:
file- TheFileof the local file you wish to write to.contents- The contents of the file you wish to create.- Throws:
IOException
-
writeUTF8File
Writes the contents variable to theFilelocated at the filename.- Parameters:
filename- TheFileof the local file you wish to write to.contents- The contents of the file you wish to create.- Throws:
IOException
-
readFileFromURL
Deprecated.this method doesn't support UTF8 or handle HTTP response codesConvenience method to read in a file from a local source.- Parameters:
url- TheURLto read the source data from.- Returns:
- A String containing the source data or null in the case of an error.
-
readFileFromURL
Deprecated.this method doesn't support UTF8 or handle HTTP response codesConvenience method to read in a file from either a remote or local source.- Parameters:
url- TheURLto read the source data from.trustedClient- TheTrustedHttpClientwhich 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
-
loadPropertiesFromStream
Load properties from a stream. Close the stream after reading. -
withResource
Handle a closeable resource insidefand 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 insidefand ensure it gets closed properly. -
openClassPathResource
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
Open a classpath resource using the class loader ofIoSupport.- See Also:
-
classPathResourceAsFile
Get a classpath resource as a file using the class loader ofIoSupport. -
loadFileFromClassPathAsString
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
Load a classpath resource into a string using the class loader ofIoSupport.- See Also:
-
withFile
Handle a stream insidefand ensure thatsgets closed properly.Please note: The outcome of
fis wrapped into a some. Thereforefis not allowed to returnnull. Use anOptioninstead 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) Deprecated.Handle a stream insidefand ensure thatsgets closed properly.- Parameters:
s- the stream creation functiontoErr- error handler transforming an exception into something elsef- stream handler
-
withResource
public static <A,Err, Either<Err,B extends Closeable> A> withResource(Function0<B> r, Function<Exception, Err> toErr, Function<B, A> f) Handle a closeable resource insidefand ensure thatrgets closed properly.- Parameters:
r- resource creation functiontoErr- error handler transforming an exception into something elsef- resource handler
-
withStream
Deprecated.Handle a stream insidefand ensure thatsgets closed properly. -
fileInputStream
Create a function that creates aFileInputStream. -
file
Create a file from the list of path elements. -
locked
Run functionfhaving exclusive read/write access to the given file.Please note that the implementation uses Java NIO
FileLockwhich 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 existIOException- if the file lock can not be created due to access limitations
-
serializeDeserialize
Serialize and deserialize an object. To test serializability.
-