Class IoUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final InputStreamReusable empty input stream.static final ReaderReusable empty reader.static final CharsetUTF-8 charset -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCleans up thread-local buffer caches for the current thread.static 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 voidReads and discards the remainder of a reader or input stream, ignoring any exceptions.static voidFlushes multiple output streams and writers in a single call.static byte[]inflate(byte[] compressed, boolean nowrap, long maxOutputBytes, int maxRatio) Inflates (decompresses) the specified compressed bytes with a hard ceiling on the total output size and an effective output-to-input ratio guard.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 bytes to output stream.static longpipe(InputStream in, OutputStream out) Pipes input stream to output stream.static longpipe(InputStream in, OutputStream out, long maxBytes) Pipes input stream to output stream with max bytes.static longpipe(InputStream in, OutputStream out, Consumer<IOException> onException) Pipes input stream to output stream with exception handler.static longpipe(InputStream in, Writer out) Pipes input stream to writer.static longpipe(InputStream in, Writer out, Consumer<IOException> onException) Pipes input stream to writer with exception handler.static longPipes reader to file.static longpipe(Reader in, OutputStream out) Pipes reader to output stream.static longpipe(Reader in, OutputStream out, Consumer<IOException> onException) Pipes reader to output stream with exception handler.static longPipes reader to writer.static longpipe(Reader in, Writer out, Consumer<IOException> onException) Pipes reader to writer with exception handler.static longPipes the contents of the specifiedReader to the specifiedWriter a line at a time.static final voidprintLines(String[] lines) Prints all the specified lines to the log with line numbers.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, int maxBytes) Reads the contents of an input stream into a string, reading up to the specified maximum number of bytes.static Stringread(InputStream in, int maxBytes, Charset cs) Reads the contents of an input stream into a string using the specified charset, reading up to the specified maximum number of bytes.static Stringread(InputStream in, long maxBytes) Reads the contents of an input stream into a string, reading up to the specified maximum number of bytes.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
-
UTF8
UTF-8 charset -
EMPTY_INPUT_STREAM
Reusable empty input stream. -
EMPTY_READER
Reusable empty reader.
-
-
Constructor Details
-
IoUtils
protected IoUtils()Constructor.
-
-
Method Details
-
cleanupThreadLocals
Cleans up thread-local buffer caches for the current thread.This method should be called when a thread is finished using the buffer caches to prevent memory leaks in thread pool environments where threads are reused.
-
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.
-
drainQuietly
Reads and discards the remainder of a reader or input stream, ignoring any exceptions.Reads until end-of-stream using a 256-unit buffer. Any
IOExceptionstops the drain silently. No-op if the object isnull or is neither aReadernor anInputStream. The stream is not closed.- Parameters:
in- The reader or input stream to drain.
-
closeQuietly
Close input stream and ignore any exceptions.No-op if input stream is
null .- Parameters:
is- The input stream to close.
-
closeQuietly
Quietly close all specified input streams, output streams, readers, and writers.- Parameters:
o- The list of all objects to quietly 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.
-
count
Counts the number of bytes in the input stream and then closes the stream.- Parameters:
is- The input stream to read from. Can benull (returns0 ).- 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. Can benull (returns0 ).- Returns:
- The number of characters read.
- Throws:
IOException- Thrown by underlying stream.
-
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.
-
loadSystemResourceAsString
Loads a text file from either the file system or classpath.- Parameters:
name- The file name. Must not benull .paths- The paths to search.- Returns:
- The file contents, or
null if not found. - Throws:
IOException- Thrown by underlying stream.
-
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 (returns0 ).
Reader is automatically closed.out- The file to write the output to.
Can benull (returns0 ).
Writer is flushed but not automatically closed.- Returns:
- The number of characters piped.
- Throws:
IOException- Thrown by underlying 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. Must not benull when the input is non-null .- 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 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 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. Must not benull when the input stream is non-null .- 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. Must not benull when the input stream is non-null .onException- Consumer of anyI/O exceptions. Must not benull (invoked on I/O error).- Returns:
- The contents of the reader as a string or
null if input stream wasnull .
-
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. Must not benull (invoked on I/O error).- 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, reading up to the specified maximum number of bytes.Assumes UTF-8 encoding.
- Parameters:
in- The input stream.
Can benull .
Stream is automatically closed.maxBytes- The maximum number of bytes to read, or-1 to read all bytes.- Returns:
- The contents of the input stream 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 using the specified charset, reading up to the specified maximum number of bytes.- Parameters:
in- The input stream.
Can benull .
Stream is automatically closed.maxBytes- The maximum number of bytes to read, or-1 to read all bytes.cs- The charset of the contents of the input stream. Must not benull when the input stream is non-null .- Returns:
- The contents of the input stream 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, reading up to the specified maximum number of bytes.Assumes UTF-8 encoding.
- Parameters:
in- The input stream.
Can benull .
Stream is automatically closed.maxBytes- The maximum number of bytes to read, or-1 to read all bytes.- Returns:
- The contents of the input stream as a string, or
null if the input stream wasnull . - Throws:
IOException- If a problem occurred trying to read from the input 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:ReaderInputStreamFile- byte array.
- Returns:
- The input converted to a string, or
null if the input wasnull . - Throws:
IOException- If thrown from output stream.IllegalArgumentException- If the input is non-null but not one of the supported types.
-
read
Reads the contents of a path into a string.Assumes default character encoding.
- Parameters:
in- The path to read.
Can benull .- 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
-
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. Must not benull (invoked on I/O error).- Returns:
- The contents of the reader as a string, or
null if the reader wasnull .
-
read
Reads the specified input into aStringuntil 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.
-
readBytes
Read the specified file into a byte array.- Parameters:
in- The file to read into a byte array.
Can benull (returns an empty array).- Returns:
- The contents of the file as a byte array. 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.
Can benull (returns an empty 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. 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 (returns an empty array).
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 (returns an empty array).
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
Reads the specified input stream into the specified byte array.- Parameters:
in- The input stream to read.
Can benull (returns an empty array).
Stream is automatically closed.- Returns:
- A byte array containing the contents. Never
null . - Throws:
IOException- Thrown by underlying stream.
-
inflate
public static byte[] inflate(byte[] compressed, boolean nowrap, long maxOutputBytes, int maxRatio) throws IOException Inflates (decompresses) the specified compressed bytes with a hard ceiling on the total output size and an effective output-to-input ratio guard.Building on the bounded-copy family in this class (such as
readBytes(InputStream,int)andpipe(InputStream,OutputStream,long)), this method decompresses untrusted input without letting a small compressed payload expand into an unbounded allocation. Two independent limits are enforced during inflation, so a violating stream is aborted before its full output is ever materialized:- Absolute cap. If the running output size would exceed
maxOutputBytes , anIOExceptionis thrown. - Ratio guard. If the running output size would exceed
maxRatio times the compressed input length, anIOExceptionis thrown — this catches a highly-compressible payload even when the absolute cap is generous.
- Parameters:
compressed- The compressed input bytes.
Can benull (returns an empty array).nowrap- Passtrue for a raw (headerless) DEFLATE stream, orfalse for a zlib-wrapped stream.
Must match the format used to compress the input.maxOutputBytes- The maximum number of decompressed bytes to allow.
Values<= 0 disable the absolute cap (mirroring themaxLength convention used elsewhere); the ratio guard, if enabled, still applies.maxRatio- The maximum allowed ratio of output size to compressed input size.
Values<= 0 disable the ratio guard.- Returns:
- The decompressed bytes. Never
null . - Throws:
IOException- If the input is not valid for the selected format, or if either the absolute cap or the ratio guard is exceeded.- Since:
- 10.0.0
- Absolute cap. If the running output size would exceed
-
toBufferedReader
Wraps the specified reader in a buffered reader.- Parameters:
r- The reader being wrapped. Can benull (returnsnull ).- Returns:
- The reader wrapped in a
BufferedReader, or the originalReaderif it's already a buffered reader, ornull if the argument wasnull .
-
pipe
Pipes bytes to output stream.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes input stream to output stream.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes input stream to output stream with exception handler.
Returns0 if eitherin orout isnull .onException must not benull (invoked on I/O error). -
pipe
Pipes input stream to output stream with max bytes.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes input stream to writer.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes input stream to writer with exception handler.
Returns0 if eitherin orout isnull .onException must not benull (invoked on I/O error). -
pipe
Pipes reader to file.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes reader to output stream.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes reader to output stream with exception handler.
Returns0 if eitherin orout isnull .onException must not benull (invoked on I/O error). -
pipe
Pipes reader to writer.
Returns0 if eitherin orout isnull .- Throws:
IOException
-
pipe
Pipes reader to writer with exception handler.
Returns0 if eitherin orout isnull .onException must not benull (invoked on I/O error). -
printLines
Prints all the specified lines to the log with line numbers.- Parameters:
lines- The lines to print. Must not benull (individualnull entries render as"null" ).
-