public final class IoSupport extends Object
| Modifier and Type | Field and Description |
|---|---|
static Function<InputStream,String> |
readToString
Function that reads an input stream into a string using utf-8 encoding.
|
| Modifier and Type | Method and Description |
|---|---|
static Option<File> |
classPathResourceAsFile(String resource)
Get a classpath resource as a file using the class loader of
IoSupport. |
static <A> Function<InputStream,A> |
closeAfterwards(Function<InputStream,? extends A> f)
Wrap function
f to close the input stream after usage. |
static boolean |
closeQuietly(Closeable s)
Closes a
Closable quietly so that no exceptions are thrown. |
static boolean |
closeQuietly(Process process)
Closes the processes input, output and error streams.
|
static boolean |
closeQuietly(StreamHelper s)
Closes a
StreamHelper quietly so that no exceptions are thrown. |
static File |
file(String... pathElems)
Create a file from the list of path elements.
|
static Function0<InputStream> |
fileInputStream(File a)
Create a function that creates a
FileInputStream. |
static String |
getOutput(InputStream is)
Extracts the content from the given input stream.
|
static String |
getSystemTmpDir() |
static Option<String> |
loadFileFromClassPathAsString(String resource)
Load a classpath resource into a string using the class loader of
IoSupport. |
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.
|
static Properties |
loadPropertiesFromClassPath(String resource)
Load a properties file from the classpath using the class loader of
IoSupport. |
static Properties |
loadPropertiesFromClassPath(String resource,
Class<?> clazz)
Load a properties file from the classpath using the class loader of the given class.
|
static Properties |
loadPropertiesFromFile(String path) |
static Properties |
loadPropertiesFromStream(InputStream stream)
Load properties from a stream.
|
static Properties |
loadPropertiesFromUrl(URL url) |
static Option<String> |
loadTxtFromClassPath(String resource,
Class<?> clazz)
Load a text file from the class path using the class loader of the given class.
|
static <A> A |
locked(File file,
Function<File,A> f)
Run function
f having exclusive read/write access to the given file. |
static Option<InputStream> |
openClassPathResource(String resource)
Open a classpath resource using the class loader of
IoSupport. |
static Option<InputStream> |
openClassPathResource(String resource,
Class<?> clazz)
Open a classpath resource using the class loader of the given class.
|
static String |
readFileFromURL(URL url)
Deprecated.
this method doesn't support UTF8 or handle HTTP response codes
|
static String |
readFileFromURL(URL url,
TrustedHttpClient trustedClient)
Deprecated.
this method doesn't support UTF8 or handle HTTP response codes
|
static String |
readToString(URL url,
String encoding)
Like
IOUtils.toString(java.net.URL, String) but without checked exception. |
static <A extends Serializable> |
serializeDeserialize(A a)
Serialize and deserialize an object.
|
static File |
waitForFile(File file)
Returns the given
File back when it's ready for reading |
static boolean |
withFile(File file,
Effect2<OutputStream,File> e)
Handle a stream inside
e and ensure that s gets closed properly. |
static <A> Option<A> |
withFile(File file,
Function2<InputStream,File,A> f)
Handle a stream inside
f and ensure that s gets closed properly. |
static <A,B extends Closeable> |
withResource(B b,
com.entwinemedia.fn.Fn<? super B,? extends A> f)
Handle a closeable resource inside
f and ensure it gets closed properly. |
static <A,B extends Closeable> |
withResource(B b,
Function<B,A> f)
Handle a closeable resource inside
f and ensure it gets closed properly. |
static <A,Err,B extends Closeable> |
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. |
static <A,Err> Either<Err,A> |
withStream(Function0<InputStream> s,
Function<Exception,Err> toErr,
Function<InputStream,A> f)
|
static <A> A |
withStream(InputStream s,
Function<InputStream,A> f)
Deprecated.
|
static <A> A |
withStream(OutputStream s,
Function<OutputStream,A> f)
Deprecated.
|
static <A> A |
withStreams(InputStream[] in,
OutputStream[] out,
Function2<InputStream[],OutputStream[],A> f)
Handle multiple streams inside
f and ensure that they get closed properly. |
static void |
writeUTF8File(File file,
String contents)
Writes the contents variable to the
File. |
static void |
writeUTF8File(String filename,
String contents)
Writes the contents variable to the
File located at the filename. |
static void |
writeUTF8File(URL file,
String contents)
Writes the contents variable to the
URL. |
public static final Function<InputStream,String> readToString
public static String getSystemTmpDir()
public static boolean closeQuietly(Closeable s)
Closable quietly so that no exceptions are thrown.s - maybe nullpublic static boolean closeQuietly(StreamHelper s)
StreamHelper quietly so that no exceptions are thrown.s - maybe nullpublic static boolean closeQuietly(Process process)
process - the processtrue if the streams were closedpublic static String getOutput(InputStream is)
is - the input streampublic static void writeUTF8File(URL file, String contents) throws IOException
URL. Note that the URL must be a local URL.file - The URL of the local file you wish to write to.contents - The contents of the file you wish to create.IOExceptionpublic static void writeUTF8File(File file, String contents) throws IOException
File.file - The File of the local file you wish to write to.contents - The contents of the file you wish to create.IOExceptionpublic static void writeUTF8File(String filename, String contents) throws IOException
File located at the filename.filename - The File of the local file you wish to write to.contents - The contents of the file you wish to create.IOException@Deprecated public static String readFileFromURL(URL url)
url - The URL to read the source data from.@Deprecated public static String readFileFromURL(URL url, TrustedHttpClient trustedClient)
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.public static Properties loadPropertiesFromFile(String path)
public static Properties loadPropertiesFromUrl(URL url)
public static Properties loadPropertiesFromStream(InputStream stream)
public static Properties loadPropertiesFromClassPath(String resource)
IoSupport.public static Properties loadPropertiesFromClassPath(String resource, Class<?> clazz)
public static Option<String> loadTxtFromClassPath(String resource, Class<?> clazz)
@Deprecated public static <A> A withStream(InputStream s, Function<InputStream,A> f)
withResource(java.io.Closeable, org.opencastproject.util.data.Function) insteadf and ensure that s gets closed properly.public static <A,B extends Closeable> A withResource(B b, Function<B,A> f)
f and ensure it gets closed properly.public static <A,B extends Closeable> A withResource(B b, com.entwinemedia.fn.Fn<? super B,? extends A> f)
f and ensure it gets closed properly.public static Option<InputStream> openClassPathResource(String resource, Class<?> clazz)
public static Option<InputStream> openClassPathResource(String resource)
IoSupport.openClassPathResource(String, Class)public static Option<File> classPathResourceAsFile(String resource)
IoSupport.public static Option<String> loadFileFromClassPathAsString(String resource, Class<?> clazz)
public static Option<String> loadFileFromClassPathAsString(String resource)
IoSupport.public static <A> Option<A> withFile(File file, Function2<InputStream,File,A> f)
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.
public static boolean withFile(File file, Effect2<OutputStream,File> e)
e and ensure that s gets closed properly.@Deprecated public static <A,Err> Either<Err,A> withStream(Function0<InputStream> s, Function<Exception,Err> toErr, Function<InputStream,A> f)
withResource(org.opencastproject.util.data.Function0, org.opencastproject.util.data.Function, org.opencastproject.util.data.Function)
insteadf and ensure that s gets closed properly.s - the stream creation functiontoErr - error handler transforming an exception into something elsef - stream handlerpublic static <A,Err,B extends Closeable> Either<Err,A> withResource(Function0<B> r, Function<Exception,Err> toErr, Function<B,A> f)
f and ensure that r gets closed properly.r - resource creation functiontoErr - error handler transforming an exception into something elsef - resource handler@Deprecated public static <A> A withStream(OutputStream s, Function<OutputStream,A> f)
withResource(java.io.Closeable, org.opencastproject.util.data.Function) insteadf and ensure that s gets closed properly.public static <A> A withStreams(InputStream[] in, OutputStream[] out, Function2<InputStream[],OutputStream[],A> f)
f and ensure that they get closed properly.public static String readToString(URL url, String encoding)
IOUtils.toString(java.net.URL, String) but without checked exception.public static <A> Function<InputStream,A> closeAfterwards(Function<InputStream,? extends A> f)
f to close the input stream after usage.public static Function0<InputStream> fileInputStream(File a)
FileInputStream.public static File waitForFile(File file)
File back when it's ready for readingpublic static <A> A locked(File file, Function<File,A> f) throws NotFoundException, IOException
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.
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 limitationspublic static <A extends Serializable> A serializeDeserialize(A a)
Copyright © 2009–2020 Opencast Project. All rights reserved.