public final class IOUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
closeIfNotNull(Closeable... o)
Closes all of the provided
Closeables and swallows any exceptions. |
static void |
closeIfNotNull(Closeable closeable)
Closes the passed in resource if not null.
|
static long |
copyStream(InputStream in,
OutputStream out)
Copies the whole input stream to the given output stream.
|
static <T> T |
deserialise(byte[] bytes,
boolean compressed)
Deserialises an object from a byte array to an object of a specified type.
|
static <T> T |
deserialise(byte[] bytes,
boolean compressed,
ClassLoader classLoader)
Deserialises an object from a byte array to an object of a specified type.
|
static String |
getFileContent(String path)
Reads the file content at the given path, and returns its UTF-8 content
in String format.
|
static String |
getFileContent(String path,
String encoding)
Reads the file content at the given path, and returns its content in
String format using the given encoding.
|
static String |
getFileContentFromClassPath(Class classInSameModule,
String path)
Reads a files content from the classpath using UTF-8 encoding.
|
static String |
getFileContentFromClassPath(Class classInSameModule,
String path,
String encoding)
Reads a files content from the classpath using the given encoding.
|
static String |
readStream(InputStream is)
Reads the InputStream and tries to interpret its content as String using
UTF-8 encoding.
|
static String |
readStream(InputStream is,
String encoding)
Reads the InputStream and tries to interpret its content as String using
the given encoding.
|
static void |
writeToFile(String fileName,
String content)
Writes the provided content to a file with the provided name.
|
public static String getFileContent(String path) throws IOException
path - The path to the fileIOException - if an error occurs while reading the fileIllegalArgumentException - if the file does not exists at the given
pathpublic static String getFileContent(String path, String encoding) throws IOException
path - The path to the fileencoding - encoding of the fileIOException - if an error occurs while reading the fileIllegalArgumentException - if the file does not exists at the given
pathpublic static String getFileContentFromClassPath(Class classInSameModule, String path) throws IOException
classInSameModule - A class that is in the same module than the resource.path - the path to the file on the classpathIOException - if an error occured while readingIllegalArgumentException - if the file does not existspublic static String getFileContentFromClassPath(Class classInSameModule, String path, String encoding) throws IOException
classInSameModule - A class that is in the same module than the resource.path - the path to the file on the classpathencoding - encoding of the fileIOException - if an error occured while readingIllegalArgumentException - if the file does not existspublic static String readStream(InputStream is) throws IOException
is - the inputstream to readIOException - if there was an error while reading the streampublic static String readStream(InputStream is, String encoding) throws IOException
is - the inputstream to readencoding - the encoding to be usedIOException - if there was an error while reading the streampublic static void writeToFile(String fileName, String content) throws IOException
fileName - The name of the file to write to.content - The contents to write.IOException - If the file could be written to.public static void closeIfNotNull(Closeable closeable)
closeable - The resource that needs to be closed.public static void closeIfNotNull(Closeable... o)
Closeables and swallows any exceptions.
Also performs a null check on the Closeable.
o - The Closeables.public static long copyStream(InputStream in, OutputStream out) throws IOException
in - the stream to copy from.out - the stream to copy to.IOException - if an error occurs while copying the data.public static <T> T deserialise(byte[] bytes,
boolean compressed)
throws IOException,
ClassNotFoundException
T - The returned object type.bytes - The bytes that represent the Object to be deserialized. The classes to be loaded must be from the
set specified in the whitelistcompressed - If true, expect that the bytes are compressed.IOException - If there was a problem with the ObjectInputStream process.ClassNotFoundException - If there was problem loading a class that makes up the bytes to be deserialized.public static <T> T deserialise(byte[] bytes,
boolean compressed,
ClassLoader classLoader)
throws IOException,
ClassNotFoundException
T - The returned object type.bytes - The bytes that represent the Object to be deserialized. The classes to be loaded must be from the
set specified in the whitelist maintained in the WhitelistObjectInputStreamcompressed - If true, expect that the bytes are compressed.classLoader - Used in place of the default ClassLoader, default will be used if null.IOException - If there was a problem with the ObjectInputStream process.ClassNotFoundException - If there was problem loading a class that makes up the bytes to be deserialized.Copyright © 2010–2023 Open Identity Platform Community. All rights reserved.