Class IOUtils
java.lang.Object
org.apache.juneau.common.internal.IOUtils
Various I/O related utility methods.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final InputStream
Reusable empty input stream.static final Reader
Reusable empty reader.static final Charset
UTF-8 charset -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Close all specified input streams, output streams, readers, and writers.static void
Close input stream and ignore any exceptions.static void
Close output stream and ignore any exceptions.static void
Close reader and ignore any exceptions.static void
Close writer and ignore any exceptions.static void
closeQuietly
(Object... o) Quietly close all specified input streams, output streams, readers, and writers.static long
count
(InputStream is) Counts the number of bytes in the input stream and then closes the stream.static long
Counts the number of characters in the reader and then closes the reader.static void
Flushes multiple output streams and writers in a single call.static String
loadSystemResourceAsString
(String name, String... paths) Loads a text file from either the file system or classpath.static long
pipe
(byte[] in, OutputStream out, int maxBytes) Pipes the specified byte array to the specified output stream.static long
pipe
(InputStream in, OutputStream out) Pipes the specified input stream to the specified output stream.static long
pipe
(InputStream in, OutputStream out, long maxBytes) Pipes the specified input stream to the specified output stream.static long
pipe
(InputStream in, OutputStream out, Consumer<IOException> onException) Pipes the specified input stream to the specified output stream.static long
pipe
(InputStream in, Writer out) Pipes the contents of the specified input stream to the writer.static long
pipe
(InputStream in, Writer out, Consumer<IOException> onException) Pipes the contents of the specified input stream to the writer.static long
Pipes the contents of the specifiedReader to the specified file.static long
pipe
(Reader in, OutputStream out) Pipes the specified reader to the specified output stream.static long
pipe
(Reader in, OutputStream out, Consumer<IOException> onException) Pipes the specified reader to the specified output stream.static long
Pipes the contents of the specifiedReader to the specifiedWriter .static long
pipe
(Reader in, Writer out, Consumer<IOException> onException) Pipes the contents of the specifiedReader to the specifiedWriter .static long
Pipes the contents of the specifiedReader to the specifiedWriter a line at a time.static String
read
(byte[] in) Reads the specified byte array containing UTF-8 into a string.static String
Reads the specified byte array into a string.static String
Reads the contents of a file into a string.static String
read
(InputStream in) Reads the contents of an input stream into a string.static String
read
(InputStream in, Charset cs) Reads the contents of an input stream into a string using the specified charset.static String
read
(InputStream in, Charset cs, Consumer<IOException> onException) Reads the contents of an input stream into a string using the specified charset.static String
read
(InputStream in, Consumer<IOException> onException) Reads the contents of an input stream into a string.static String
Reads the contents of a reader into a string.static String
Reads the specified input into aString
until the end of the input is reached.static String
read
(Reader in, Consumer<IOException> onException) Reads the contents of a reader into a string.static String
Pipes the specified object to the specified output stream.static byte[]
Read the specified file into a byte array.static byte[]
Read the specified file into a byte array.static byte[]
readBytes
(InputStream in) Reads the specified input stream into the specified byte array.static byte[]
readBytes
(InputStream in, int maxBytes) Reads the specified input stream into the specified byte array.static byte[]
Reads the specified input stream into the specified byte array.static Reader
Wraps the specified reader in a buffered reader.
-
Field Details
-
UTF8
UTF-8 charset -
EMPTY_INPUT_STREAM
Reusable empty input stream. -
EMPTY_READER
Reusable empty reader.
-
-
Constructor Details
-
IOUtils
public IOUtils()
-
-
Method Details
-
pipe
Pipes the contents of the specifiedReader to the specified file.- Parameters:
in
- The reader to pipe from.
Can benull .
Reader is automatically closed.out
- The file to write the output to.
Can benull .- Returns:
- The number of characters piped.
- Throws:
IOException
- Thrown by underlying stream.
-
pipe
Pipes the contents of the specifiedReader to the specifiedWriter .- Parameters:
in
- The reader to pipe from.
Can benull .
Reader is automatically closed.out
- The file to write the output to.
Can benull .
Writer is flushed but not automatically closed.- Returns:
- The number of characters piped.
- Throws:
IOException
- Thrown by underlying stream.
-
pipe
Pipes the contents of the specifiedReader to the specifiedWriter .- Parameters:
in
- The reader to pipe from.
Can benull .
Reader is automatically closed.out
- The file to write the output to.
Can benull .
Writer is flushed but not automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The number of characters piped.
-
pipeLines
Pipes the contents of the specifiedReader to the specifiedWriter a line at a time.Writer is flushed after every line. Typically useful when writing to consoles.
- Parameters:
in
- The reader to pipe from.
Can benull .
Reader is automatically closed.out
- The file to write the output to.
Can benull .
Writer is flushed but not automatically closed.- Returns:
- The number of characters piped.
- Throws:
IOException
- Thrown by underlying stream.
-
pipe
Pipes the contents of the specified input stream to the writer.- Parameters:
in
- The stream to pipe from.
Can benull .
Streams is automatically closed.out
- The writer to pipe to.
Can benull .
Stream is not automatically closed.- Returns:
- The number of bytes written.
- Throws:
IOException
- Thrown by underlying stream.
-
pipe
Pipes the contents of the specified input stream to the writer.- Parameters:
in
- The stream to pipe from.
Can benull .
Streams is automatically closed.out
- The writer to pipe to.
Can benull .
Stream is not automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The number of bytes written.
-
pipe
Pipes the specified input stream to the specified output stream.Either stream is not automatically closed.
- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.out
- The output stream.
Can benull .
Stream is not automatically closed.- Returns:
- The number of bytes written.
- Throws:
IOException
- If thrown from either stream.
-
pipe
Pipes the specified input stream to the specified output stream.Either stream is not automatically closed.
- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.out
- The output stream.
Can benull .
Stream is not automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The number of bytes written.
-
pipe
Pipes the specified input stream to the specified output stream.Either stream is not automatically closed.
- Parameters:
in
- The input stream.
Can benull .
Stream is not automatically closed.out
- The output stream.
Can benull .
Stream is not automatically closed.maxBytes
- The maximum number of bytes or-1 to read the entire input stream.- Returns:
- The number of bytes written.
- Throws:
IOException
- If thrown from either stream.
-
pipe
Pipes the specified reader to the specified output stream.- Parameters:
in
- The input reader.
Can benull .
Stream is automatically closed.out
- The output stream.
Can benull .
Stream is not automatically closed.- Returns:
- The number of bytes written.
- Throws:
IOException
- If thrown from output stream.
-
pipe
Pipes the specified reader to the specified output stream.- Parameters:
in
- The input reader.
Can benull .
Stream is automatically closed.out
- The output stream.
Can benull .
Stream is not automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The number of bytes written.
-
pipe
Pipes the specified byte array to the specified output stream.- Parameters:
in
- The input byte array.
Can benull .out
- The output stream.
Can benull .
Stream is not automatically closed.maxBytes
- The maximum number of bytes or-1 to read the entire byte array.- Returns:
- The number of bytes written.
- Throws:
IOException
- If thrown from output stream.
-
read
Pipes the specified object to the specified output stream.- Parameters:
in
- The input byte array.
Can benull or any of the following types:Reader
InputStream
File
- byte array.
- Returns:
- The input converted to a string.
- Throws:
IOException
- If thrown from output stream.
-
read
Reads the specified byte array containing UTF-8 into a string.- Parameters:
in
- The input.
Can benull .- Returns:
- The new string, or
null if the input was null.
-
read
Reads the specified byte array into a string.- Parameters:
in
- The input.
Can benull .charset
- The character set to use for decoding.- Returns:
- The new string, or
null if the input was null.
-
read
Reads the contents of a file into a string.Assumes default character encoding.
- Parameters:
in
- The file to read.
Can benull .- Returns:
- The contents of the reader as a string, or
null if file does not exist. - Throws:
IOException
- If a problem occurred trying to read from the reader.
-
read
Reads the contents of a reader into a string.- Parameters:
in
- The input reader.
Can benull .
Stream is automatically closed.- Returns:
- The contents of the reader as a string, or
null if the reader wasnull . - Throws:
IOException
- If a problem occurred trying to read from the reader.
-
read
Reads the contents of a reader into a string.- Parameters:
in
- The input reader.
Can benull .
Stream is automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The contents of the reader as a string, or
null if the reader wasnull .
-
read
Reads the specified input into aString
until the end of the input is reached.- Parameters:
in
- The input reader.
Can benull .
String is automatically closed.expectedLength
- Specify a positive number if the length of the input is known, or-1 if unknown.- Returns:
- The contents of the reader as a string, or
null if the reader wasnull . - Throws:
IOException
- If a problem occurred trying to read from the reader.
-
read
Reads the contents of an input stream into a string.Assumes UTF-8 encoding.
- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.- Returns:
- The contents of the reader as a string, or
null if the input stream wasnull . - Throws:
IOException
- If a problem occurred trying to read from the input stream.
-
read
Reads the contents of an input stream into a string.Assumes UTF-8 encoding.
- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.onException
- Consumer of anyI/O exceptions
.- Returns:
- The contents of the reader as a string, or
null if the input stream wasnull .
-
read
Reads the contents of an input stream into a string using the specified charset.- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.cs
- The charset of the contents of the input stream.- Returns:
- The contents of the reader as a string or
null if input stream wasnull . - Throws:
IOException
- If a problem occurred trying to read from the input stream.
-
read
Reads the contents of an input stream into a string using the specified charset.- Parameters:
in
- The input stream.
Can benull .
Stream is automatically closed.cs
- The charset of the contents of the input stream.onException
- Consumer of anyI/O exceptions
.- Returns:
- The contents of the reader as a string or
null if input stream wasnull .
-
readBytes
Reads the specified input stream into the specified byte array.- Parameters:
in
- The input stream to read.
Can benull .
Stream is automatically closed.- Returns:
- A byte array containing the contents. Never
null . - Throws:
IOException
- Thrown by underlying stream.
-
readBytes
Reads the specified input stream into the specified byte array.- Parameters:
in
- The input stream to read.
Can benull .
Stream is not automatically closed.maxBytes
- The maximum number of bytes or-1 to read the entire stream.- Returns:
- A byte array containing the contents. Never
null . - Throws:
IOException
- Thrown by underlying stream.
-
readBytes
Read the specified file into a byte array.- Parameters:
in
- The file to read into a byte array.- Returns:
- The contents of the file as a byte array.
- Throws:
IOException
- Thrown by underlying stream.
-
readBytes
Read the specified file into a byte array.- Parameters:
in
- The file to read into a byte array.maxBytes
- The maximum number of bytes to read, or-1 to read all bytes.- Returns:
- The contents of the file as a byte array.
- Throws:
IOException
- Thrown by underlying stream.
-
readBytes
Reads the specified input stream into the specified byte array.- Parameters:
in
- The input stream to read.
Can benull .
Stream is automatically closed.- Returns:
- A byte array containing the contents. Never
null . - Throws:
IOException
- Thrown by underlying stream.
-
toBufferedReader
Wraps the specified reader in a buffered reader.- Parameters:
r
- The reader being wrapped.- Returns:
- The reader wrapped in a
BufferedReader
, or the originalReader
if it's already a buffered reader.
-
count
Counts the number of bytes in the input stream and then closes the stream.- Parameters:
is
- The input stream to read from.- Returns:
- The number of bytes read.
- Throws:
IOException
- Thrown by underlying stream.
-
count
Counts the number of characters in the reader and then closes the reader.- Parameters:
r
- The reader to read from.- Returns:
- The number of characters read.
- Throws:
IOException
- Thrown by underlying stream.
-
closeQuietly
Close input stream and ignore any exceptions.No-op if input stream is
null .- Parameters:
is
- The input stream to close.
-
closeQuietly
Close output stream and ignore any exceptions.No-op if output stream is
null .- Parameters:
os
- The output stream to close.
-
closeQuietly
Close reader and ignore any exceptions.No-op if reader is
null .- Parameters:
r
- The reader to close.
-
closeQuietly
Close writer and ignore any exceptions.No-op if writer is
null .- Parameters:
w
- The writer to close.
-
closeQuietly
Quietly close all specified input streams, output streams, readers, and writers.- Parameters:
o
- The list of all objects to quietly close.
-
flush
Flushes multiple output streams and writers in a single call.- Parameters:
o
- The objects to flush.null entries are ignored.- Throws:
IOException
- Thrown by underlying stream.
-
close
Close all specified input streams, output streams, readers, and writers.- Parameters:
o
- The list of all objects to close.null entries are ignored.- Throws:
IOException
- Thrown by underlying stream.
-
loadSystemResourceAsString
Loads a text file from either the file system or classpath.- Parameters:
name
- The file name.paths
- The paths to search.- Returns:
- The file contents, or
null if not found. - Throws:
IOException
- Thrown by underlying stream.
-