Package org.apache.juneau.common.utils
Class IOUtils
java.lang.Object
org.apache.juneau.common.utils.IOUtils
Various I/O related utility methods.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final InputStreamReusable empty input stream.static final ReaderReusable empty reader.static final CharsetUTF-8 charset
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidClose all specified input streams, output streams, readers, and writers.static voidClose input stream and ignore any exceptions.static voidClose output stream and ignore any exceptions.static voidClose reader and ignore any exceptions.static voidClose writer and ignore any exceptions.static voidcloseQuietly(Object... o) Quietly close all specified input streams, output streams, readers, and writers.static longcount(InputStream is) Counts the number of bytes in the input stream and then closes the stream.static longCounts the number of characters in the reader and then closes the reader.static voidFlushes multiple output streams and writers in a single call.static StringloadSystemResourceAsString(String name, String... paths) Loads a text file from either the file system or classpath.static longpipe(byte[] in, OutputStream out, int maxBytes) Pipes the specified byte array to the specified output stream.static longpipe(InputStream in, OutputStream out) Pipes the specified input stream to the specified output stream.static longpipe(InputStream in, OutputStream out, long maxBytes) Pipes the specified input stream to the specified output stream.static longpipe(InputStream in, OutputStream out, Consumer<IOException> onException) Pipes the specified input stream to the specified output stream.static longpipe(InputStream in, Writer out) Pipes the contents of the specified input stream to the writer.static longpipe(InputStream in, Writer out, Consumer<IOException> onException) Pipes the contents of the specified input stream to the writer.static longPipes the contents of the specifiedReader to the specified file.static longpipe(Reader in, OutputStream out) Pipes the specified reader to the specified output stream.static longpipe(Reader in, OutputStream out, Consumer<IOException> onException) Pipes the specified reader to the specified output stream.static longPipes the contents of the specifiedReader to the specifiedWriter .static longpipe(Reader in, Writer out, Consumer<IOException> onException) Pipes the contents of the specifiedReader to the specifiedWriter .static longPipes the contents of the specifiedReader to the specifiedWriter a line at a time.static Stringread(byte[] in) Reads the specified byte array containing UTF-8 into a string.static StringReads the specified byte array into a string.static StringReads the contents of a file into a string.static Stringread(InputStream in) Reads the contents of an input stream into a string.static Stringread(InputStream in, Charset cs) Reads the contents of an input stream into a string using the specified charset.static Stringread(InputStream in, Charset cs, Consumer<IOException> onException) Reads the contents of an input stream into a string using the specified charset.static Stringread(InputStream in, Consumer<IOException> onException) Reads the contents of an input stream into a string.static StringReads the contents of a reader into a string.static StringReads the specified input into aStringuntil the end of the input is reached.static Stringread(Reader in, Consumer<IOException> onException) Reads the contents of a reader into a string.static StringPipes the specified object to the specified output stream.static StringReads the contents of a path into a string.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 ReaderWraps the specified reader in a buffered reader.
- 
Field Details- 
UTF8UTF-8 charset
- 
EMPTY_INPUT_STREAMReusable empty input stream.
- 
EMPTY_READERReusable empty reader.
 
- 
- 
Constructor Details- 
IOUtilsprotected IOUtils()Constructor.
 
- 
- 
Method Details- 
pipePipes the contents of the specifiedReader to the specified file.- Parameters:
- in- The reader to pipe from.
 Can be- null .
 Reader is automatically closed.
- out- The file to write the output to.
 Can be- null .
- Returns:
- The number of characters piped.
- Throws:
- IOException- Thrown by underlying stream.
 
- 
pipePipes the contents of the specifiedReader to the specifiedWriter .- Parameters:
- in- The reader to pipe from.
 Can be- null .
 Reader is automatically closed.
- out- The file to write the output to.
 Can be- null .
 Writer is flushed but not automatically closed.
- Returns:
- The number of characters piped.
- Throws:
- IOException- Thrown by underlying stream.
 
- 
pipePipes the contents of the specifiedReader to the specifiedWriter .- Parameters:
- in- The reader to pipe from.
 Can be- null .
 Reader is automatically closed.
- out- The file to write the output to.
 Can be- null .
 Writer is flushed but not automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The number of characters piped.
 
- 
pipeLinesPipes 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 be- null .
 Reader is automatically closed.
- out- The file to write the output to.
 Can be- null .
 Writer is flushed but not automatically closed.
- Returns:
- The number of characters piped.
- Throws:
- IOException- Thrown by underlying stream.
 
- 
pipePipes the contents of the specified input stream to the writer.- Parameters:
- in- The stream to pipe from.
 Can be- null .
 Streams is automatically closed.
- out- The writer to pipe to.
 Can be- null .
 Stream is not automatically closed.
- Returns:
- The number of bytes written.
- Throws:
- IOException- Thrown by underlying stream.
 
- 
pipePipes the contents of the specified input stream to the writer.- Parameters:
- in- The stream to pipe from.
 Can be- null .
 Streams is automatically closed.
- out- The writer to pipe to.
 Can be- null .
 Stream is not automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The number of bytes written.
 
- 
pipePipes the specified input stream to the specified output stream.Either stream is not automatically closed. - Parameters:
- in- The input stream.
 Can be- null .
 Stream is automatically closed.
- out- The output stream.
 Can be- null .
 Stream is not automatically closed.
- Returns:
- The number of bytes written.
- Throws:
- IOException- If thrown from either stream.
 
- 
pipePipes the specified input stream to the specified output stream.Either stream is not automatically closed. - Parameters:
- in- The input stream.
 Can be- null .
 Stream is automatically closed.
- out- The output stream.
 Can be- null .
 Stream is not automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The number of bytes written.
 
- 
pipePipes the specified input stream to the specified output stream.Either stream is not automatically closed. - Parameters:
- in- The input stream.
 Can be- null .
 Stream is not automatically closed.
- out- The output stream.
 Can be- null .
 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.
 
- 
pipePipes the specified reader to the specified output stream.- Parameters:
- in- The input reader.
 Can be- null .
 Stream is automatically closed.
- out- The output stream.
 Can be- null .
 Stream is not automatically closed.
- Returns:
- The number of bytes written.
- Throws:
- IOException- If thrown from output stream.
 
- 
pipePipes the specified reader to the specified output stream.- Parameters:
- in- The input reader.
 Can be- null .
 Stream is automatically closed.
- out- The output stream.
 Can be- null .
 Stream is not automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The number of bytes written.
 
- 
pipePipes the specified byte array to the specified output stream.- Parameters:
- in- The input byte array.
 Can be- null .
- out- The output stream.
 Can be- null .
 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.
 
- 
readPipes the specified object to the specified output stream.- Parameters:
- in- The input byte array.
 Can be- null 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.
 
- 
readReads the specified byte array containing UTF-8 into a string.- Parameters:
- in- The input.
 Can be- null .
- Returns:
- The new string, or null if the input was null.
 
- 
readReads the specified byte array into a string.- Parameters:
- in- The input.
 Can be- null .
- charset- The character set to use for decoding.
- Returns:
- The new string, or null if the input was null.
 
- 
readReads the contents of a file into a string.Assumes default character encoding. - Parameters:
- in- The file to read.
 Can be- null .
- 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.
 
- 
readReads the contents of a path into a string.Assumes default character encoding. - Parameters:
- in- The path to read.
 Can be- null .
- Returns:
- The contents of the reader as a string, or null if path does not exist.
- Throws:
- IOException- If a problem occurred trying to read from the reader.
- Since:
- 9.1.0
 
- 
readReads the contents of a reader into a string.- Parameters:
- in- The input reader.
 Can be- null .
 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.
 
- 
readReads the contents of a reader into a string.- Parameters:
- in- The input reader.
 Can be- null .
 Stream is automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The contents of the reader as a string, or null if the reader wasnull .
 
- 
readReads the specified input into aStringuntil the end of the input is reached.- Parameters:
- in- The input reader.
 Can be- null .
 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.
 
- 
readReads the contents of an input stream into a string.Assumes UTF-8 encoding. - Parameters:
- in- The input stream.
 Can be- null .
 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.
 
- 
readReads the contents of an input stream into a string.Assumes UTF-8 encoding. - Parameters:
- in- The input stream.
 Can be- null .
 Stream is automatically closed.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The contents of the reader as a string, or null if the input stream wasnull .
 
- 
readReads the contents of an input stream into a string using the specified charset.- Parameters:
- in- The input stream.
 Can be- null .
 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.
 
- 
readReads the contents of an input stream into a string using the specified charset.- Parameters:
- in- The input stream.
 Can be- null .
 Stream is automatically closed.
- cs- The charset of the contents of the input stream.
- onException- Consumer of any- I/O exceptions.
- Returns:
- The contents of the reader as a string or null if input stream wasnull .
 
- 
readBytesReads the specified input stream into the specified byte array.- Parameters:
- in- The input stream to read.
 Can be- null .
 Stream is automatically closed.
- Returns:
- A byte array containing the contents.  Never null .
- Throws:
- IOException- Thrown by underlying stream.
 
- 
readBytesReads the specified input stream into the specified byte array.- Parameters:
- in- The input stream to read.
 Can be- null .
 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.
 
- 
readBytesRead 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.
 
- 
readBytesRead 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.
 
- 
readBytesReads the specified input stream into the specified byte array.- Parameters:
- in- The input stream to read.
 Can be- null .
 Stream is automatically closed.
- Returns:
- A byte array containing the contents.  Never null .
- Throws:
- IOException- Thrown by underlying stream.
 
- 
toBufferedReaderWraps the specified reader in a buffered reader.- Parameters:
- r- The reader being wrapped.
- Returns:
- The reader wrapped in a BufferedReader, or the originalReaderif it's already a buffered reader.
 
- 
countCounts 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.
 
- 
countCounts 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.
 
- 
closeQuietlyClose input stream and ignore any exceptions.No-op if input stream is null .- Parameters:
- is- The input stream to close.
 
- 
closeQuietlyClose output stream and ignore any exceptions.No-op if output stream is null .- Parameters:
- os- The output stream to close.
 
- 
closeQuietlyClose reader and ignore any exceptions.No-op if reader is null .- Parameters:
- r- The reader to close.
 
- 
closeQuietlyClose writer and ignore any exceptions.No-op if writer is null .- Parameters:
- w- The writer to close.
 
- 
closeQuietlyQuietly close all specified input streams, output streams, readers, and writers.- Parameters:
- o- The list of all objects to quietly close.
 
- 
flushFlushes 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.
 
- 
closeClose 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.
 
- 
loadSystemResourceAsStringLoads 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.
 
 
-