@Immutable public class IoUtil extends Object
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Closeable closeable)
Closes the closable silently.
|
static InputStream |
getResourceAsStream(String resourcePath,
ClassLoader classLoader,
Class<?> clazz)
Get the
input stream to the resource given by the supplied path. |
static boolean |
isSame(InputStream input1,
InputStream input2)
Write the entire contents of the supplied string to the given stream.
|
static String |
read(File file)
Read and return the entire contents of the supplied
File . |
static String |
read(InputStream stream)
Read and return the entire contents of the supplied
InputStream . |
static String |
read(InputStream stream,
String charset)
Read and return the entire contents of the supplied
InputStream . |
static String |
read(Reader reader)
Read and return the entire contents of the supplied
Reader . |
static byte[] |
readBytes(File file)
Read and return the entire contents of the supplied
file . |
static byte[] |
readBytes(InputStream stream)
Read and return the entire contents of the supplied
stream . |
static void |
write(InputStream input,
OutputStream stream)
Write the entire contents of the supplied string to the given stream.
|
static void |
write(InputStream input,
OutputStream stream,
int bufferSize)
Write the entire contents of the supplied string to the given stream.
|
static void |
write(Reader input,
Writer writer)
Write the entire contents of the supplied string to the given writer.
|
static void |
write(String content,
File file)
Write the entire contents of the supplied string to the given file.
|
static void |
write(String content,
OutputStream stream)
Write the entire contents of the supplied string to the given stream.
|
static void |
write(String content,
Writer writer)
Write the entire contents of the supplied string to the given writer.
|
public static byte[] readBytes(InputStream stream) throws IOException
stream
. This method always closes the stream when
finished reading.stream
- the stream to the contents; may be nullIOException
- if there is an error reading the contentpublic static byte[] readBytes(File file) throws IOException
file
.file
- the file containing the contents; may be nullIOException
- if there is an error reading the contentpublic static String read(Reader reader) throws IOException
Reader
. This method always closes the reader when finished
reading.reader
- the reader of the contents; may be nullIOException
- if there is an error reading the contentpublic static String read(InputStream stream) throws IOException
InputStream
. This method always closes the stream when finished
reading.stream
- the streamed contents; may be nullIOException
- if there is an error reading the contentpublic static String read(InputStream stream, String charset) throws IOException
InputStream
. This method always closes the stream when finished
reading.stream
- the streamed contents; may be nullcharset
- charset of the stream data; may not be nullIOException
- if there is an error reading the contentpublic static String read(File file) throws IOException
File
.file
- the file containing the information to be read; may be nullIOException
- if there is an error reading the contentpublic static void write(String content, File file) throws IOException
content
- the content to write to the stream; may be nullfile
- the file to which the content is to be writtenIOException
IllegalArgumentException
- if the stream is nullpublic static void write(String content, OutputStream stream) throws IOException
content
- the content to write to the stream; may be nullstream
- the stream to which the content is to be writtenIOException
IllegalArgumentException
- if the stream is nullpublic static void write(String content, Writer writer) throws IOException
content
- the content to write to the writer; may be nullwriter
- the writer to which the content is to be writtenIOException
IllegalArgumentException
- if the writer is nullpublic static void write(InputStream input, OutputStream stream) throws IOException
input
- the content to write to the stream; may be nullstream
- the stream to which the content is to be writtenIOException
IllegalArgumentException
- if the stream is nullpublic static void write(InputStream input, OutputStream stream, int bufferSize) throws IOException
input
- the content to write to the stream; may be nullstream
- the stream to which the content is to be writtenbufferSize
- the size of the buffer; must be positiveIOException
IllegalArgumentException
- if the stream is nullpublic static void write(Reader input, Writer writer) throws IOException
input
- the content to write to the writer; may be nullwriter
- the writer to which the content is to be writtenIOException
IllegalArgumentException
- if the writer is nullpublic static boolean isSame(InputStream input1, InputStream input2) throws IOException
input1
- the first streaminput2
- the second streamIOException
IllegalArgumentException
- if the stream is nullpublic static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz)
input stream
to the resource given by the supplied path. If a class loader is supplied, the
method attempts to resolve the resource using the ClassLoader.getResourceAsStream(String)
method; if the result is
non-null, it is returned. Otherwise, if a class is supplied, this method attempts to resolve the resource using the
Class.getResourceAsStream(String)
method; if the result is non-null, it is returned. Otherwise, this method then
uses the Class' ClassLoader to load the resource; if non-null, it is returned . Otherwise, this method looks for an
existing and readable file
at the path; if found, a buffered stream to that file is returned. Otherwise, this
method attempts to parse the resource path into a valid URL
; if this succeeds, the method attempts to open a stream
to that URL. If all of these fail, this method returns null.resourcePath
- the logical path to the classpath, file, or URL resourceclazz
- the class that should be used to load the resource as a stream; may be nullclassLoader
- the classloader that should be used to load the resource as a stream; may be nullIllegalArgumentException
- if the resource path is null or emptypublic static void closeQuietly(Closeable closeable)
closeable
- the closeable instance; may be nullCopyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.